Spaces:
Sleeping
Sleeping
Stream the response try it
Browse files
app.py
CHANGED
|
@@ -4,27 +4,38 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
def respond(message, history):
|
| 7 |
-
messages = [{"role": "system", "content": "You are a friendly chatbot
|
| 8 |
|
| 9 |
if history:
|
| 10 |
messages.extend(history)
|
| 11 |
|
| 12 |
messages.append({"role": "user", "content": message})
|
| 13 |
-
|
| 14 |
-
response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
messages,
|
| 16 |
-
max_tokens=150,
|
| 17 |
-
temperature=0.7,
|
| 18 |
-
top_p=0.9
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
chatbot = gr.ChatInterface(
|
| 24 |
respond,
|
| 25 |
type="messages",
|
| 26 |
-
title="
|
| 27 |
-
description="
|
| 28 |
)
|
| 29 |
|
| 30 |
chatbot.launch()
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
def respond(message, history):
|
| 7 |
+
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 8 |
|
| 9 |
if history:
|
| 10 |
messages.extend(history)
|
| 11 |
|
| 12 |
messages.append({"role": "user", "content": message})
|
| 13 |
+
|
| 14 |
+
# Initialize the response as an empty string
|
| 15 |
+
response = ""
|
| 16 |
+
|
| 17 |
+
# Stream the response using yield and for loop
|
| 18 |
+
for message in client.chat_completion(
|
| 19 |
messages,
|
| 20 |
+
max_tokens=150,
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
top_p=0.9,
|
| 23 |
+
stream=True # Enable streaming
|
| 24 |
+
):
|
| 25 |
+
# Capture the most recent token
|
| 26 |
+
token = message['choices'][0]['delta']['content']
|
| 27 |
+
|
| 28 |
+
# Add the token to the response
|
| 29 |
+
response += token
|
| 30 |
+
|
| 31 |
+
# Yield the response to stream it progressively
|
| 32 |
+
yield response
|
| 33 |
|
| 34 |
chatbot = gr.ChatInterface(
|
| 35 |
respond,
|
| 36 |
type="messages",
|
| 37 |
+
title="Streaming Chatbot",
|
| 38 |
+
description="This chatbot streams responses as they are generated for a more dynamic experience!"
|
| 39 |
)
|
| 40 |
|
| 41 |
chatbot.launch()
|