tc043 Codex commited on
Commit
eae9568
·
1 Parent(s): 3eaf8cd

feat: implement event handler wrappers and add test suite for Gradio components and app integration

Browse files
Files changed (5) hide show
  1. app.py +24 -9
  2. test_app.py +6 -0
  3. test_gradio.py +4 -0
  4. test_lambda.py +10 -0
  5. test_schema.py +7 -0
app.py CHANGED
@@ -40,12 +40,12 @@ async def run_minicpm(func, *args, **kwargs):
40
  MAX_TRACE_EVENTS = 50
41
  MAX_HISTORY_ITEMS = 20
42
 
43
- def append_trace(state: dict, event: dict) -> None:
44
  state["trace"].append(event)
45
  if len(state["trace"]) > MAX_TRACE_EVENTS:
46
  state["trace"] = state["trace"][-MAX_TRACE_EVENTS:]
47
 
48
- def append_history(state: dict, displayed_task: str, raw_task: str) -> None:
49
  state["history"].append(displayed_task)
50
  state["raw_history"].append(raw_task)
51
  if len(state["history"]) > MAX_HISTORY_ITEMS:
@@ -252,7 +252,9 @@ def generate_task_html(task: str, animate=True) -> str:
252
  class_str = "fade-in" if animate else ""
253
  return f"<div id='current-task' class='{class_str}'>{task}</div>"
254
 
255
- async def process_step(state: dict, action: str, goal: str = None, style: str = None, last_task: str = None):
 
 
256
  # Initialization
257
  if action == "start":
258
  state["goal"] = goal
@@ -315,7 +317,7 @@ async def process_step(state: dict, action: str, goal: str = None, style: str =
315
  return state, generate_task_html(styled_task), generate_breadcrumbs_html(state["history"][-3:]), gr.update(visible=True)
316
 
317
  with gr.Blocks(css=custom_css, title="Step-Zero") as app:
318
- session_state = gr.State({})
319
 
320
  with gr.Column(visible=True) as screen_start:
321
  gr.HTML("<h1 class='text-4xl font-bold mb-8 text-center mt-20'>What is paralyzing you?</h1>")
@@ -356,12 +358,25 @@ with gr.Blocks(css=custom_css, title="Step-Zero") as app:
356
 
357
  # --- Event Handlers ---
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  # Start Session
360
  start_btn.click(
361
  fn=show_loading,
362
  outputs=[screen_start, screen_task, task_display, controls_row]
363
  ).then(
364
- fn=lambda state, goal, style: asyncio.run(process_step(state, "start", goal=goal, style=style)),
365
  inputs=[session_state, goal_input, style_radio],
366
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
367
  )
@@ -370,7 +385,7 @@ with gr.Blocks(css=custom_css, title="Step-Zero") as app:
370
  fn=show_loading,
371
  outputs=[screen_start, screen_task, task_display, controls_row]
372
  ).then(
373
- fn=lambda state, goal, style: asyncio.run(process_step(state, "start", goal=goal, style=style)),
374
  inputs=[session_state, goal_input, style_radio],
375
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
376
  )
@@ -380,7 +395,7 @@ with gr.Blocks(css=custom_css, title="Step-Zero") as app:
380
  fn=show_loading_step,
381
  outputs=[task_display, controls_row]
382
  ).then(
383
- fn=lambda state: asyncio.run(process_step(state, "done", last_task=state["current_task"])),
384
  inputs=[session_state],
385
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
386
  )
@@ -390,7 +405,7 @@ with gr.Blocks(css=custom_css, title="Step-Zero") as app:
390
  fn=show_loading_step,
391
  outputs=[task_display, controls_row]
392
  ).then(
393
- fn=lambda state: asyncio.run(process_step(state, "skip")),
394
  inputs=[session_state],
395
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
396
  )
@@ -400,7 +415,7 @@ with gr.Blocks(css=custom_css, title="Step-Zero") as app:
400
  fn=show_loading_step,
401
  outputs=[task_display, controls_row]
402
  ).then(
403
- fn=lambda state: asyncio.run(process_step(state, "too_hard")),
404
  inputs=[session_state],
405
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
406
  )
 
40
  MAX_TRACE_EVENTS = 50
41
  MAX_HISTORY_ITEMS = 20
42
 
43
+ def append_trace(state, event: dict) -> None:
44
  state["trace"].append(event)
45
  if len(state["trace"]) > MAX_TRACE_EVENTS:
46
  state["trace"] = state["trace"][-MAX_TRACE_EVENTS:]
47
 
48
+ def append_history(state, displayed_task: str, raw_task: str) -> None:
49
  state["history"].append(displayed_task)
50
  state["raw_history"].append(raw_task)
51
  if len(state["history"]) > MAX_HISTORY_ITEMS:
 
252
  class_str = "fade-in" if animate else ""
253
  return f"<div id='current-task' class='{class_str}'>{task}</div>"
254
 
255
+ async def process_step(state, action: str, goal: str = None, style: str = None, last_task: str = None):
256
+ if not state:
257
+ state = {}
258
  # Initialization
259
  if action == "start":
260
  state["goal"] = goal
 
317
  return state, generate_task_html(styled_task), generate_breadcrumbs_html(state["history"][-3:]), gr.update(visible=True)
318
 
319
  with gr.Blocks(css=custom_css, title="Step-Zero") as app:
320
+ session_state = gr.State()
321
 
322
  with gr.Column(visible=True) as screen_start:
323
  gr.HTML("<h1 class='text-4xl font-bold mb-8 text-center mt-20'>What is paralyzing you?</h1>")
 
358
 
359
  # --- Event Handlers ---
360
 
361
+ async def handle_start(state, goal, style):
362
+ return await process_step(state, "start", goal=goal, style=style)
363
+
364
+ async def handle_done(state):
365
+ if not state: state = {}
366
+ return await process_step(state, "done", last_task=state.get("current_task"))
367
+
368
+ async def handle_skip(state):
369
+ return await process_step(state, "skip")
370
+
371
+ async def handle_too_hard(state):
372
+ return await process_step(state, "too_hard")
373
+
374
  # Start Session
375
  start_btn.click(
376
  fn=show_loading,
377
  outputs=[screen_start, screen_task, task_display, controls_row]
378
  ).then(
379
+ fn=handle_start,
380
  inputs=[session_state, goal_input, style_radio],
381
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
382
  )
 
385
  fn=show_loading,
386
  outputs=[screen_start, screen_task, task_display, controls_row]
387
  ).then(
388
+ fn=handle_start,
389
  inputs=[session_state, goal_input, style_radio],
390
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
391
  )
 
395
  fn=show_loading_step,
396
  outputs=[task_display, controls_row]
397
  ).then(
398
+ fn=handle_done,
399
  inputs=[session_state],
400
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
401
  )
 
405
  fn=show_loading_step,
406
  outputs=[task_display, controls_row]
407
  ).then(
408
+ fn=handle_skip,
409
  inputs=[session_state],
410
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
411
  )
 
415
  fn=show_loading_step,
416
  outputs=[task_display, controls_row]
417
  ).then(
418
+ fn=handle_too_hard,
419
  inputs=[session_state],
420
  outputs=[session_state, task_display, breadcrumbs_display, controls_row]
421
  )
test_app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ try:
2
+ import app
3
+ app.app.get_api_info()
4
+ except Exception as e:
5
+ import traceback
6
+ traceback.print_exc()
test_gradio.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ import gradio as gr
2
+ with gr.Blocks() as app:
3
+ session_state = gr.State({})
4
+ app.launch(server_name="0.0.0.0", server_port=7860, share=False)
test_lambda.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def test():
4
+ pass
5
+
6
+ with gr.Blocks() as app:
7
+ b = gr.Button()
8
+ b.click(fn=lambda x: x, inputs=[b], outputs=[b])
9
+
10
+ app.get_api_info()
test_schema.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio_client import utils
3
+ schema = {"type": "object", "additionalProperties": False}
4
+ try:
5
+ print(utils.json_schema_to_python_type(schema))
6
+ except Exception as e:
7
+ print("ERROR:", type(e), str(e))