Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
|
4 |
+
# Load the model and tokenizer from Hugging Face
|
5 |
+
model_name = "google/gemma-3-1b-it"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
def respond(user_input, chat_history):
|
10 |
+
"""
|
11 |
+
Generates the chatbot's response.
|
12 |
+
|
13 |
+
It builds a conversation string from the chat history,
|
14 |
+
appends the latest user input, and generates text using the model.
|
15 |
+
"""
|
16 |
+
conversation = ""
|
17 |
+
# Build a conversational prompt from past messages
|
18 |
+
for user_msg, bot_msg in chat_history:
|
19 |
+
conversation += f"User: {user_msg}\nBot: {bot_msg}\n"
|
20 |
+
conversation += f"User: {user_input}\nBot: "
|
21 |
+
|
22 |
+
# Tokenize the conversation prompt and generate a response
|
23 |
+
inputs = tokenizer.encode(conversation, return_tensors="pt")
|
24 |
+
outputs = model.generate(
|
25 |
+
inputs,
|
26 |
+
max_length=inputs.shape[1] + 100, # adjust max_length as needed
|
27 |
+
do_sample=True,
|
28 |
+
temperature=0.7,
|
29 |
+
pad_token_id=tokenizer.eos_token_id
|
30 |
+
)
|
31 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
# The generated text includes the prompt text, so extract only the new reply.
|
34 |
+
bot_reply = generated_text[len(conversation):].split("User:", 1)[0].strip()
|
35 |
+
chat_history.append((user_input, bot_reply))
|
36 |
+
return "", chat_history # Clear input and return updated chat history
|
37 |
+
|
38 |
+
# Build the Gradio interface using Blocks for a conversational layout.
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
gr.Markdown("# Chatbot powered by google/gemma-3-1b-it")
|
41 |
+
chatbot = gr.Chatbot()
|
42 |
+
state = gr.State([]) # To keep track of the conversation history.
|
43 |
+
txt = gr.Textbox(show_label=True, placeholder="Type a message here...", label="Your Message")
|
44 |
+
|
45 |
+
# When the user submits a message, call the respond() function.
|
46 |
+
txt.submit(respond, inputs=[txt, state], outputs=[txt, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
demo.launch()
|