annakcarls commited on
Commit
82a3302
·
verified ·
1 Parent(s): 0abba31

Stream the response try it

Browse files
Files changed (1) hide show
  1. app.py +22 -11
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 giving advice."}]
8
 
9
  if history:
10
  messages.extend(history)
11
 
12
  messages.append({"role": "user", "content": message})
13
-
14
- response = client.chat_completion(
 
 
 
 
15
  messages,
16
- max_tokens=150, # Increased for longer responses
17
- temperature=0.7, # Medium randomness
18
- top_p=0.9 # Keeps responses reasonably diverse
19
- )
20
-
21
- return response['choices'][0]['message']['content'].strip()
 
 
 
 
 
 
 
22
 
23
  chatbot = gr.ChatInterface(
24
  respond,
25
  type="messages",
26
- title="Dynamic Chatbot",
27
- description="A friendly chatbot with more dynamic and creative responses!"
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()