jedick commited on
Commit
b1d78ef
·
1 Parent(s): 284ee47

Implement custom timer

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +64 -14
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐨
4
  colorFrom: yellow
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.38.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: yellow
5
  colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 6.15.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -2,20 +2,22 @@ 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
 
@@ -23,16 +25,62 @@ def interact_with_agent(prompt, messages=[]):
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
- )
31
- input = gr.Textbox(
32
- "Start the simulation with two assistant messages after 2-second delays",
33
- autofocus=True,
34
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  input.submit(
 
 
 
 
 
36
  # Update chatbot UI with user message immediately
37
  str_to_message,
38
  input,
@@ -42,9 +90,11 @@ with gr.Blocks() as demo:
42
  interact_with_agent,
43
  [input, chatbot],
44
  chatbot,
45
- )
46
- gr.Markdown(
47
- "Reported as [Gradio issue #11637](https://github.com/gradio-app/gradio/issues/11637)"
 
 
48
  )
49
 
50
  demo.launch()
 
2
  import time
3
 
4
  # Inspired by: https://www.gradio.app/guides/agents-and-tool-usage
5
+ # Custom timer solution described by Claude Sonnet 4.6:
6
+ # https://claude.ai/share/d7adc776-05f2-4c2b-a93a-9cf6089dd7be
7
 
8
 
9
  def interact_with_agent(prompt, messages=[]):
10
  """Simulate generation of two assistant messages after delays"""
11
+ time.sleep(4)
12
  messages.append(
13
  gr.ChatMessage(
14
+ role="assistant", content="First message gr.Chatbot() timer stops here"
15
  )
16
  )
17
  yield messages
18
+ time.sleep(4)
19
  messages.append(
20
+ gr.ChatMessage(role="assistant", content="Second message Custom timer stops here")
21
  )
22
  yield messages
23
 
 
25
  def str_to_message(content, role="user"):
26
  return [gr.ChatMessage(role=role, content=content)]
27
 
28
+ timer_html = """
29
+ <div id="agent-timer" style="font-family: monospace;"></div>
30
+ """
31
+
32
+ timer_js = """
33
+ let startTime = null;
34
+ let interval = null;
35
+ const el = element.querySelector('#agent-timer');
36
+
37
+ el.addEventListener('agent-timer:start', () => {
38
+ startTime = Date.now();
39
+ el.textContent = '⏱ 0.0s';
40
+ interval = setInterval(() => {
41
+ const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
42
+ el.textContent = '⏱ ' + elapsed + 's';
43
+ }, 100);
44
+ });
45
+
46
+ el.addEventListener('agent-timer:stop', () => {
47
+ if (interval) {
48
+ clearInterval(interval);
49
+ interval = null;
50
+ const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
51
+ el.textContent = '✓ ' + elapsed + 's';
52
+ }
53
+ });
54
+ """
55
 
56
  with gr.Blocks() as demo:
57
+ with gr.Row():
58
+ with gr.Column():
59
+ gr.Markdown("## Stop Gradio chatbot timer at the last (not first) yielded agent message")
60
+ input = gr.Textbox(
61
+ "Start the simulation with two assistant messages after 4-second delays",
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: Wrote to support [Gradio issue #11637](https://github.com/gradio-app/gradio/issues/11637) (Gradio 5.38.2)
72
+ - 2026-05-30: Implemented custom timer using [gr.HTML](https://huggingface.co/blog/gradio-html-one-shot-apps) (Gradio 6.15.2)
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,
 
90
  interact_with_agent,
91
  [input, chatbot],
92
  chatbot,
93
+ ).then(
94
+ # Stop custom timer
95
+ lambda: None, None, None,
96
+ js="() => document.getElementById('agent-timer')"
97
+ "?.dispatchEvent(new CustomEvent('agent-timer:stop'))",
98
  )
99
 
100
  demo.launch()