Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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 |
-
#
|
13 |
-
|
14 |
-
messages.
|
|
|
15 |
|
16 |
-
#
|
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
|
29 |
-
stream=True
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
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()
|