File size: 2,146 Bytes
59ccd2c
239890d
 
 
afcdd52
239890d
 
 
 
 
 
 
 
 
afcdd52
 
 
 
 
59ccd2c
 
 
 
 
 
afcdd52
59ccd2c
 
 
 
afcdd52
59ccd2c
afcdd52
 
239890d
afcdd52
59ccd2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239890d
59ccd2c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import torch
import transformers

# Model setup
model_id = "GoToCompany/gemma2-9b-cpt-sahabatai-v1-instruct"

pipeline = transformers.pipeline(
    "text-generation",
    model=model_id,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device_map="auto",
)

terminators = [
    pipeline.tokenizer.eos_token_id,
    pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]

# Chatbot Functionality
def chatbot(messages):
    """
    Handles user interactions and returns the model's response.
    Args:
        messages (list): List of messages with roles ('user' or 'assistant') and content.

    Returns:
        list: Updated conversation with the assistant's response.
    """
    # Prepare the conversation for the model
    outputs = pipeline(
        messages,
        max_new_tokens=256,
        eos_token_id=terminators,
    )
    # Extract and format the assistant's response
    assistant_response = outputs[0]["generated_text"] if outputs else "I'm sorry, I couldn't generate a response."
    messages.append({"role": "assistant", "content": assistant_response})
    return messages

# Gradio Chat Interface
with gr.Blocks() as demo:
    gr.Markdown("# 🤗 Gemma2 Chatbot")
    gr.Markdown("A chatbot that understands Javanese and Sundanese, powered by `GoToCompany/gemma2`.")

    chat_history = gr.Chatbot(label="Gemma2 Chatbot")
    user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
    send_button = gr.Button("Send")

    def respond(chat_history, user_message):
        # Add user message to chat history
        chat_history.append(("user", user_message))

        # Generate assistant's response
        conversation = [{"role": role, "content": content} for role, content in chat_history]
        response = chatbot(conversation)

        # Add assistant's response to chat history
        assistant_message = response[-1]["content"]
        chat_history.append(("assistant", assistant_message))
        return chat_history, ""

    send_button.click(respond, inputs=[chat_history, user_input], outputs=[chat_history, user_input])

# Launch the app
demo.launch()