kaitwithkwk commited on
Commit
8eb3105
·
verified ·
1 Parent(s): e4e4172

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -22
app.py CHANGED
@@ -1,38 +1,32 @@
1
- # build on your original chatbot from the previous lesson
2
- # a basic chatbot from the previous lesson is below -- edit it to incorporate the changes described above
3
-
4
  import gradio as gr
5
- from huggingface_hub import InferenceClient #imports huggingface models
6
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
  def respond(message, history):
10
  messages = [{"role": "system", "content": "I am a kind chatbot."}]
11
 
12
- # add all previous messages to the messages list
13
- if history:
14
- messages.extend(history)
 
15
 
16
- # add the current user's message to the messages list
17
  messages.append({"role": "user", "content": message})
18
 
19
- # makes the chat completion API call,
20
- # sending the messages and other parameters to the model
21
- # implements streaming, where one word/token appears at a time
22
  response = ""
23
-
24
- # iterate through each message in the method
25
- for message in client.chat_completion(
26
  messages,
27
  max_tokens=100,
28
- temperature=.1,
29
- stream=True):
30
-
31
- # add the tokens to the output content
32
- token = message.choices[0].delta.content # capture the most recent toke
33
- response += token # Add it to the response
34
- yield response # yield the response:
 
35
 
36
  chatbot = gr.ChatInterface(respond)
37
 
38
- chatbot.launch()
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
5
 
6
  def respond(message, history):
7
  messages = [{"role": "system", "content": "I am a kind chatbot."}]
8
 
9
+ # Add all previous messages to the messages list
10
+ for human, assistant in history:
11
+ messages.append({"role": "user", "content": human})
12
+ messages.append({"role": "assistant", "content": assistant})
13
 
14
+ # Add the current user's message to the messages list
15
  messages.append({"role": "user", "content": message})
16
 
 
 
 
17
  response = ""
18
+ for chunk in client.chat_completion(
 
 
19
  messages,
20
  max_tokens=100,
21
+ temperature=0.1,
22
+ stream=True
23
+ ):
24
+ token = chunk.choices[0].delta.content
25
+ response += token
26
+ # Filter out any unwanted tokens like </s> or <|endoftext|>, [USER], [/ASS]
27
+ cleaned_response = response.replace("</s>", "").replace("<|endoftext|>", "").replace("[USER]", "").replace("[/ASS]", "").strip()
28
+ yield cleaned_response
29
 
30
  chatbot = gr.ChatInterface(respond)
31
 
32
+ chatbot.launch()