Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
if history:
|
| 11 |
messages.extend(history)
|
| 12 |
-
|
|
|
|
| 13 |
messages.append({"role": "user", "content": message})
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
messages,
|
| 17 |
-
max_tokens=100
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
chatbot = gr.ChatInterface(respond
|
| 23 |
|
| 24 |
chatbot.launch()
|
|
|
|
| 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()
|