Spaces:
Sleeping
Sleeping
Add AI chatbot functionality from answer key
Browse files
app.py
CHANGED
|
@@ -1,8 +1,32 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
chatbot =
|
| 7 |
|
| 8 |
chatbot.launch()
|
|
|
|
| 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 |
+
# initialize a list of dictionaries to store the messages
|
| 10 |
+
messages = [{"role": "system",
|
| 11 |
+
"content": "You are a friendly chatbot."}]
|
| 12 |
|
| 13 |
+
# add all previous messages to the messages list
|
| 14 |
+
if history:
|
| 15 |
+
messages.extend(history)
|
| 16 |
+
|
| 17 |
+
# add the current user’s message to the messages list
|
| 18 |
+
messages.append({"role": "user", "content": "message"})
|
| 19 |
+
|
| 20 |
+
# makes the chat completion API call,
|
| 21 |
+
# sending the messages and other parameters to the model
|
| 22 |
+
response = client.chat_completion(
|
| 23 |
+
messages,
|
| 24 |
+
max_tokens = 100
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# extract and return the chatbot’s response
|
| 28 |
+
return response['choices'][0]['message']['content'].strip()
|
| 29 |
|
| 30 |
+
chatbot = gr.ChatInterface(respond, type="messages")
|
| 31 |
|
| 32 |
chatbot.launch()
|