Spaces:
Sleeping
Sleeping
Create tour guide chatbot and add theme
Browse files
app.py
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
# this client will handle making requests to the model to generate responses
|
| 5 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
+
|
| 7 |
+
def respond(message, history):
|
| 8 |
+
|
| 9 |
+
system_message = "You are a friendly tour guide chatbot. You always respond with an upbeat attitude!"
|
| 10 |
+
|
| 11 |
+
# initialize a list of dictionaries to store the messages
|
| 12 |
+
messages = [{"role": "system",
|
| 13 |
+
"content": system_message}]
|
| 14 |
+
|
| 15 |
+
# add all previous messages to the messages list
|
| 16 |
+
if history:
|
| 17 |
+
messages.extend(history)
|
| 18 |
+
|
| 19 |
+
# add the current user’s message to the messages list
|
| 20 |
+
messages.append({"role": "user", "content": message})
|
| 21 |
+
|
| 22 |
+
# makes the chat completion API call,
|
| 23 |
+
# sending the messages and other parameters to the model
|
| 24 |
+
# implements streaming, where one word/token appears at a time
|
| 25 |
+
response = ""
|
| 26 |
+
|
| 27 |
+
# iterate through each message in the method
|
| 28 |
+
for message in client.chat_completion(
|
| 29 |
+
messages,
|
| 30 |
+
max_tokens=200,
|
| 31 |
+
temperature=0.9,
|
| 32 |
+
stream=True
|
| 33 |
+
):
|
| 34 |
+
# add the tokens to the output content
|
| 35 |
+
token = message.choices[0].delta.content # capture the most recent token
|
| 36 |
+
response += token # Add it to the response
|
| 37 |
+
yield response # yield the response
|
| 38 |
+
|
| 39 |
+
chatbot = gr.ChatInterface(respond, type="messages", theme='d8ahazard/material_design_rd')
|
| 40 |
+
|
| 41 |
+
chatbot.launch(debug=True)
|