Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,76 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history
|
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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
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 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "TheDrummer/Gemmasutra-Mini-2B-v1"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
+
# Ensure model runs on CPU (default for Hugging Face Spaces free tier)
|
11 |
+
device = torch.device("cpu")
|
12 |
+
model.to(device)
|
13 |
|
14 |
+
# Chatbot function
|
15 |
+
def chat_with_model(user_input, history):
|
16 |
+
# Format history and input into a single prompt
|
17 |
+
if history is None:
|
18 |
+
history = []
|
19 |
+
|
20 |
+
# Build conversation context
|
21 |
+
prompt = ""
|
22 |
+
for h in history:
|
23 |
+
prompt += f"User: {h[0]}\nBot: {h[1]}\n"
|
24 |
+
prompt += f"User: {user_input}\nBot: "
|
25 |
|
26 |
+
# Tokenize input
|
27 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
28 |
+
|
29 |
+
# Generate response
|
30 |
+
outputs = model.generate(
|
31 |
+
**inputs,
|
32 |
+
max_new_tokens=150, # Limit response length
|
33 |
+
do_sample=True, # Enable sampling for varied responses
|
34 |
+
temperature=0.7, # Control creativity
|
35 |
+
top_p=0.9, # Nucleus sampling
|
36 |
+
pad_token_id=tokenizer.eos_token_id # Handle padding
|
37 |
+
)
|
38 |
+
|
39 |
+
# Decode response
|
40 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
|
42 |
+
# Extract just the bot's reply (after the last "Bot: ")
|
43 |
+
bot_response = response.split("Bot: ")[-1].strip()
|
44 |
+
|
45 |
+
# Update history
|
46 |
+
history.append((user_input, bot_response))
|
47 |
+
return bot_response, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Gradio Interface
|
50 |
+
with gr.Blocks(title="Grok-like Chatbot") as iface:
|
51 |
+
gr.Markdown("## Chat with Gemmasutra-Mini-2B-v1")
|
52 |
+
chatbot = gr.Chatbot(label="Conversation")
|
53 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type here...")
|
54 |
+
submit_btn = gr.Button("Send")
|
55 |
+
|
56 |
+
# State to maintain conversation history
|
57 |
+
state = gr.State(value=[])
|
58 |
+
|
59 |
+
def submit_message(user_input, history):
|
60 |
+
response, updated_history = chat_with_model(user_input, history)
|
61 |
+
return response, updated_history, updated_history, ""
|
62 |
+
|
63 |
+
# Connect button and enter key to submit
|
64 |
+
submit_btn.click(
|
65 |
+
fn=submit_message,
|
66 |
+
inputs=[msg, state],
|
67 |
+
outputs=[msg, state, chatbot, msg] # Clear input after submission
|
68 |
+
)
|
69 |
+
msg.submit(
|
70 |
+
fn=submit_message,
|
71 |
+
inputs=[msg, state],
|
72 |
+
outputs=[msg, state, chatbot, msg]
|
73 |
+
)
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
+
iface.launch()
|