Spaces:
Sleeping
Sleeping
jedick
commited on
Commit
·
ba52610
1
Parent(s):
2fea07c
Add app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
|
4 |
+
# Inspired by: https://www.gradio.app/guides/agents-and-tool-usage
|
5 |
+
|
6 |
+
|
7 |
+
def interact_with_agent(prompt, messages=[]):
|
8 |
+
"""Simulate generation of two assistant messages after delays"""
|
9 |
+
time.sleep(2)
|
10 |
+
messages.append(
|
11 |
+
gr.ChatMessage(
|
12 |
+
role="assistant", content="Progress timer stops here (or becomes invisible)"
|
13 |
+
)
|
14 |
+
)
|
15 |
+
yield messages
|
16 |
+
time.sleep(2)
|
17 |
+
messages.append(
|
18 |
+
gr.ChatMessage(role="assistant", content="But I want it to stop here")
|
19 |
+
)
|
20 |
+
yield messages
|
21 |
+
|
22 |
+
|
23 |
+
def str_to_message(content, role="user"):
|
24 |
+
return [gr.ChatMessage(role=role, content=content)]
|
25 |
+
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
chatbot = gr.Chatbot(
|
29 |
+
type="messages",
|
30 |
+
label="How to display cumulative runtime for all assistant messages?",
|
31 |
+
)
|
32 |
+
input = gr.Textbox(
|
33 |
+
"Start the simulation with two assistant messages after 2-second delays",
|
34 |
+
autofocus=True,
|
35 |
+
)
|
36 |
+
input.submit(
|
37 |
+
# Update chatbot UI with user message immediately
|
38 |
+
str_to_message,
|
39 |
+
input,
|
40 |
+
chatbot,
|
41 |
+
).then(
|
42 |
+
# Update chatbot UI with assistant messages
|
43 |
+
interact_with_agent,
|
44 |
+
[input, chatbot],
|
45 |
+
chatbot,
|
46 |
+
show_progress="minimal",
|
47 |
+
)
|
48 |
+
|
49 |
+
demo.launch()
|