Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,46 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
with gr.Blocks() as demo:
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
|
15 |
-
history.append({"role": "assistant", "content": ""})
|
16 |
-
for character in bot_message:
|
17 |
-
history[-1]['content'] += character
|
18 |
-
time.sleep(0.05)
|
19 |
-
yield history
|
20 |
-
|
21 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
22 |
-
bot, chatbot, chatbot
|
23 |
)
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
import gradio as gr
|
3 |
+
|
4 |
+
client = InferenceClient()
|
5 |
+
|
6 |
+
def respond(
|
7 |
+
prompt: str,
|
8 |
+
history,
|
9 |
+
):
|
10 |
+
if not history:
|
11 |
+
history = [{"role": "system", "content": "You are a friendly chatbot"}]
|
12 |
+
history.append({"role": "user", "content": prompt})
|
13 |
+
|
14 |
+
yield history
|
15 |
+
|
16 |
+
response = {"role": "assistant", "content": ""}
|
17 |
+
for message in client.chat_completion(
|
18 |
+
history,
|
19 |
+
temperature=0.95,
|
20 |
+
top_p=0.9,
|
21 |
+
max_tokens=512,
|
22 |
+
stream=True,
|
23 |
+
model="HuggingFaceH4/zephyr-7b-beta"
|
24 |
+
):
|
25 |
+
response["content"] += message.choices[0].delta.content or ""
|
26 |
+
|
27 |
+
yield history + [response]
|
28 |
+
|
29 |
|
30 |
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown("# Chat with Hugging Face Zephyr 7b 🤗")
|
32 |
+
chatbot = gr.Chatbot(
|
33 |
+
label="Agent",
|
34 |
+
type="messages",
|
35 |
+
avatar_images=(
|
36 |
+
None,
|
37 |
+
"https://em-content.zobj.net/source/twitter/376/hugging-face_1f917.png",
|
38 |
+
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
)
|
40 |
+
prompt = gr.Textbox(max_lines=1, label="Chat Message")
|
41 |
+
prompt.submit(respond, [prompt, chatbot], [chatbot])
|
42 |
+
prompt.submit(lambda: "", None, [prompt])
|
43 |
+
|
44 |
|
45 |
+
if __name__ == "__main__":
|
46 |
+
demo.launch()
|