Spaces:
Sleeping
Sleeping
jedick commited on
Commit ·
a741ada
1
Parent(s): b1d78ef
Overlay custom timer on chatbot
Browse files
app.py
CHANGED
|
@@ -53,38 +53,54 @@ el.addEventListener('agent-timer:stop', () => {
|
|
| 53 |
});
|
| 54 |
"""
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
input = gr.Textbox(
|
| 61 |
-
"Start the
|
| 62 |
label="User message",
|
| 63 |
autofocus=True,
|
| 64 |
submit_btn=True,
|
| 65 |
)
|
| 66 |
-
gr.Markdown("### Custom timer")
|
| 67 |
-
timer_display = gr.HTML(value=timer_html, js_on_load=timer_js, container=True)
|
| 68 |
gr.Markdown(
|
| 69 |
"""
|
| 70 |
### App history
|
| 71 |
-
- 2025-07-25:
|
| 72 |
-
- 2026-05-30:
|
| 73 |
"""
|
| 74 |
)
|
| 75 |
with gr.Column():
|
| 76 |
chatbot = gr.Chatbot()
|
|
|
|
| 77 |
|
| 78 |
input.submit(
|
| 79 |
-
# Start custom timer
|
| 80 |
-
lambda: None, None, None,
|
| 81 |
-
js="() => document.getElementById('agent-timer')"
|
| 82 |
-
"?.dispatchEvent(new CustomEvent('agent-timer:start'))",
|
| 83 |
-
).then(
|
| 84 |
# Update chatbot UI with user message immediately
|
| 85 |
str_to_message,
|
| 86 |
input,
|
| 87 |
chatbot,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
).then(
|
| 89 |
# Update chatbot UI with assistant messages
|
| 90 |
interact_with_agent,
|
|
@@ -97,4 +113,4 @@ with gr.Blocks() as demo:
|
|
| 97 |
"?.dispatchEvent(new CustomEvent('agent-timer:stop'))",
|
| 98 |
)
|
| 99 |
|
| 100 |
-
demo.launch()
|
|
|
|
| 53 |
});
|
| 54 |
"""
|
| 55 |
|
| 56 |
+
chatbot_overlay_css = """
|
| 57 |
+
#timer-display {
|
| 58 |
+
position: absolute;
|
| 59 |
+
bottom: 0px;
|
| 60 |
+
}
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
with gr.Row():
|
| 65 |
with gr.Column():
|
| 66 |
+
gr.Markdown(
|
| 67 |
+
"""
|
| 68 |
+
## Gradio chatbot timer that stops at the last (not first) yielded agent message
|
| 69 |
+
|
| 70 |
+
**Submit any user message to launch a chatbot simulation that responds with two assistant messages with a 4-second delay before each one.**
|
| 71 |
+
|
| 72 |
+
- The built-in `gr.Chatbot` timer (*lower right*) stops after the first message.
|
| 73 |
+
- The custom timer (*lower left*) keeps going until the last message is yielded.
|
| 74 |
+
- The custom timer is implemented in JavaScript and CSS and depends on `gr.HTML` capabilities in Gradio 6.
|
| 75 |
+
"""
|
| 76 |
+
)
|
| 77 |
input = gr.Textbox(
|
| 78 |
+
"Start the timers!",
|
| 79 |
label="User message",
|
| 80 |
autofocus=True,
|
| 81 |
submit_btn=True,
|
| 82 |
)
|
|
|
|
|
|
|
| 83 |
gr.Markdown(
|
| 84 |
"""
|
| 85 |
### App history
|
| 86 |
+
- 2025-07-25: Create demo to support [Gradio issue #11637](https://github.com/gradio-app/gradio/issues/11637) (Gradio 5.38.2)
|
| 87 |
+
- 2026-05-30: Add custom timer using [gr.HTML](https://huggingface.co/blog/gradio-html-one-shot-apps) (Gradio 6.15.2)
|
| 88 |
"""
|
| 89 |
)
|
| 90 |
with gr.Column():
|
| 91 |
chatbot = gr.Chatbot()
|
| 92 |
+
gr.HTML(value=timer_html, js_on_load=timer_js, elem_id="timer-display")
|
| 93 |
|
| 94 |
input.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# Update chatbot UI with user message immediately
|
| 96 |
str_to_message,
|
| 97 |
input,
|
| 98 |
chatbot,
|
| 99 |
+
).then(
|
| 100 |
+
# Start custom timer
|
| 101 |
+
lambda: None, None, None,
|
| 102 |
+
js="() => document.getElementById('agent-timer')"
|
| 103 |
+
"?.dispatchEvent(new CustomEvent('agent-timer:start'))",
|
| 104 |
).then(
|
| 105 |
# Update chatbot UI with assistant messages
|
| 106 |
interact_with_agent,
|
|
|
|
| 113 |
"?.dispatchEvent(new CustomEvent('agent-timer:stop'))",
|
| 114 |
)
|
| 115 |
|
| 116 |
+
demo.launch(css=chatbot_overlay_css)
|