|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
|
|
|
|
model_name = "google/gemma-3-1b-it" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
def respond(user_input, chat_history): |
|
""" |
|
Generates the chatbot's response. |
|
|
|
It builds a conversation string from the chat history, |
|
appends the latest user input, and generates text using the model. |
|
""" |
|
conversation = "" |
|
|
|
for user_msg, bot_msg in chat_history: |
|
conversation += f"User: {user_msg}\nBot: {bot_msg}\n" |
|
conversation += f"User: {user_input}\nBot: " |
|
|
|
|
|
inputs = tokenizer.encode(conversation, return_tensors="pt") |
|
outputs = model.generate( |
|
inputs, |
|
max_length=inputs.shape[1] + 100, |
|
do_sample=True, |
|
temperature=0.7, |
|
pad_token_id=tokenizer.eos_token_id |
|
) |
|
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
bot_reply = generated_text[len(conversation):].split("User:", 1)[0].strip() |
|
chat_history.append((user_input, bot_reply)) |
|
return "", chat_history |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Chatbot powered by google/gemma-3-1b-it") |
|
chatbot = gr.Chatbot() |
|
state = gr.State([]) |
|
txt = gr.Textbox(show_label=True, placeholder="Type a message here...", label="Your Message") |
|
|
|
|
|
txt.submit(respond, inputs=[txt, state], outputs=[txt, chatbot]) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|