Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: blue
|
5 |
-
colorTo: pink
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: chatbot_streaming
|
3 |
+
app_file: run.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
+
sdk_version: 4.15.0
|
|
|
|
|
6 |
---
|
|
|
|
run.ipynb
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_streaming"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "import time\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot()\n", " msg = gr.Textbox()\n", " clear = gr.Button(\"Clear\")\n", "\n", " def user(user_message, history):\n", " return \"\", history + [[user_message, None]]\n", "\n", " def bot(history):\n", " bot_message = random.choice([\"How are you?\", \"I love you\", \"I'm very hungry\"])\n", " history[-1][1] = \"\"\n", " for character in bot_message:\n", " history[-1][1] += character\n", " time.sleep(0.05)\n", " yield history\n", "\n", " msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", " clear.click(lambda: None, None, chatbot, queue=False)\n", " \n", "demo.queue()\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
|
4 |
+
with gr.Blocks() as demo:
|
5 |
+
with gr.Row():
|
6 |
+
messages_per_second = gr.Number(100, label="Messages per second")
|
7 |
+
number_of_messages = gr.Number(200, label="Number of messages")
|
8 |
+
|
9 |
+
chatbot = gr.Chatbot()
|
10 |
+
start = gr.Button("Start")
|
11 |
+
|
12 |
+
def bot(messages_per_second, number_of_messages):
|
13 |
+
history = [[f"Streaming {number_of_messages} msgs at {messages_per_second} msg/s ", ""]]
|
14 |
+
start_time = time.time()
|
15 |
+
for num in range(number_of_messages):
|
16 |
+
history[-1][1] += " " + str(num)
|
17 |
+
time.sleep(1 / messages_per_second)
|
18 |
+
yield history, None
|
19 |
+
end_time = time.time()
|
20 |
+
yield history, end_time - start_time
|
21 |
+
|
22 |
+
|
23 |
+
with gr.Row():
|
24 |
+
expected_duration = gr.Number(label="Expected Duration (w/o Overhead)")
|
25 |
+
backend_duration = gr.Number(label="Duration (for Backend)")
|
26 |
+
frontend_duration = gr.Number(label="Duration (for Frontend)")
|
27 |
+
|
28 |
+
start.click(fn=None, js="""() => {
|
29 |
+
window.start_time = performance.now();
|
30 |
+
}""")
|
31 |
+
start.click(lambda messages_per_second, number_of_messages: number_of_messages / messages_per_second, [messages_per_second, number_of_messages], [expected_duration])
|
32 |
+
start.click(bot, [messages_per_second, number_of_messages], [chatbot, backend_duration]).then(
|
33 |
+
fn=None,
|
34 |
+
outputs=frontend_duration,
|
35 |
+
js="""() => {
|
36 |
+
let now = performance.now();
|
37 |
+
return (now - window.start_time) / 1000;
|
38 |
+
}""")
|
39 |
+
|
40 |
+
|
41 |
+
demo.launch()
|