Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,35 @@
|
|
| 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": "You are a friendly chatbot."}]
|
| 8 |
-
|
| 9 |
-
# if history:
|
| 10 |
-
# messages.extend(history)
|
| 11 |
-
|
| 12 |
-
# messages.append({"role": "user", "content": message})
|
| 13 |
-
|
| 14 |
-
# response = client.chat_completion(
|
| 15 |
-
# messages,
|
| 16 |
-
# max_tokens=100
|
| 17 |
-
# )
|
| 18 |
-
|
| 19 |
-
# return response['choices'][0]['message']['content'].strip()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# chatbot = gr.ChatInterface(respond, type="messages")
|
| 23 |
-
|
| 24 |
-
# chatbot.launch()
|
| 25 |
-
|
| 26 |
import gradio as gr
|
| 27 |
from huggingface_hub import InferenceClient
|
| 28 |
|
| 29 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
if history:
|
| 35 |
messages.extend(history)
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
messages.append({"role": "user", "content": message})
|
| 38 |
-
|
| 39 |
-
response = client.chat_completion(messages, max_tokens=100)
|
| 40 |
-
return response['choices'][0]['message']['content'].strip()
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return history, ""
|
| 50 |
-
|
| 51 |
-
bot_response = respond(message, history, user_name)
|
| 52 |
-
history.append([message, bot_response])
|
| 53 |
-
return history, ""
|
| 54 |
|
| 55 |
with gr.Blocks() as chatbot:
|
| 56 |
-
gr.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
with gr.Column():
|
| 62 |
-
name_input = gr.Textbox(label="Your Name")
|
| 63 |
-
start_btn = gr.Button("Start Chat")
|
| 64 |
-
|
| 65 |
-
with gr.Column():
|
| 66 |
-
chatbot = gr.Chatbot(height=400)
|
| 67 |
-
msg_input = gr.Textbox(label="Message", interactive=False)
|
| 68 |
-
send_btn = gr.Button("Send")
|
| 69 |
-
|
| 70 |
-
start_btn.click(
|
| 71 |
-
lambda name: [name, start_chat(name)[0], start_chat(name)[1]],
|
| 72 |
-
inputs=[name_input],
|
| 73 |
-
outputs=[user_name, chatbot, msg_input]
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
send_btn.click(
|
| 77 |
-
chat_fn,
|
| 78 |
-
inputs=[msg_input, chatbot, user_name],
|
| 79 |
-
outputs=[chatbot, msg_input]
|
| 80 |
-
)
|
| 81 |
|
| 82 |
chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
+
user_profile = {
|
| 7 |
+
"name": ""
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
def respond(message, history):
|
| 11 |
+
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 12 |
+
|
| 13 |
if history:
|
| 14 |
messages.extend(history)
|
| 15 |
+
else:
|
| 16 |
+
messages.append({"role": "assistant", "content": f"Hi, {user_profile['name']}! How can I help you today?"})
|
| 17 |
+
|
| 18 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
response = client.chat_completion(
|
| 21 |
+
messages,
|
| 22 |
+
max_tokens=100
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
print(messages)
|
| 26 |
+
return response['choices'][0]['message']['content'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
with gr.Blocks() as chatbot:
|
| 29 |
+
name = gr.Textbox(label="Name")
|
| 30 |
+
submit_btn = gr.Button("Submit")
|
| 31 |
+
submit_btn.click(respond, inputs=name, outputs=user_profile['name'])
|
| 32 |
+
|
| 33 |
+
chatbot = gr.ChatInterface(respond, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
chatbot.launch()
|