Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,32 +2,37 @@ from huggingface_hub import InferenceClient
|
|
| 2 |
import gradio as gr
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
client = InferenceClient("google/gemma-1.1-7b-it") # Replace with a smaller model if needed
|
| 7 |
|
| 8 |
-
# Define the response function
|
| 9 |
def respond(message, history):
|
| 10 |
messages = [{"role": "system", "content": "You are a mean chatbot."}]
|
| 11 |
|
| 12 |
-
# Convert history to
|
| 13 |
if history:
|
| 14 |
for user_msg, bot_msg in history:
|
| 15 |
messages.append({"role": "user", "content": user_msg})
|
| 16 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 17 |
-
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
response = ""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
chatbot = gr.ChatInterface(fn=respond, chatbot=gr.Chatbot(), title="Mean Chatbot")
|
| 31 |
|
| 32 |
-
# Launch the chatbot
|
| 33 |
chatbot.launch(debug=True)
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import random
|
| 4 |
|
| 5 |
+
client = InferenceClient("google/gemma-1.1-7b-it") # Use a smaller model for smoother results
|
|
|
|
| 6 |
|
|
|
|
| 7 |
def respond(message, history):
|
| 8 |
messages = [{"role": "system", "content": "You are a mean chatbot."}]
|
| 9 |
|
| 10 |
+
# Convert chat history to messages
|
| 11 |
if history:
|
| 12 |
for user_msg, bot_msg in history:
|
| 13 |
messages.append({"role": "user", "content": user_msg})
|
| 14 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 15 |
+
|
| 16 |
messages.append({"role": "user", "content": message})
|
| 17 |
|
| 18 |
response = ""
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
for chunk in client.chat_completion(messages, max_tokens=100, stream=True):
|
| 22 |
+
if chunk.choices and chunk.choices[0].delta:
|
| 23 |
+
token = chunk.choices[0].delta.get("content", "")
|
| 24 |
+
response += token
|
| 25 |
+
yield response # streaming response
|
| 26 |
+
|
| 27 |
+
except StopIteration:
|
| 28 |
+
pass # not strictly needed, but included for clarity
|
| 29 |
+
|
| 30 |
+
# Final yield to make sure a value is returned
|
| 31 |
+
if not response:
|
| 32 |
+
yield "..."
|
| 33 |
+
|
| 34 |
+
# Set up the Gradio interface
|
| 35 |
chatbot = gr.ChatInterface(fn=respond, chatbot=gr.Chatbot(), title="Mean Chatbot")
|
| 36 |
|
|
|
|
| 37 |
chatbot.launch(debug=True)
|
| 38 |
+
|