convitom commited on
Commit
2bc33ee
·
1 Parent(s): 3671ffb
Files changed (2) hide show
  1. demo/_gradio_compat.py +53 -0
  2. demo/app.py +1 -0
demo/_gradio_compat.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ _gradio_compat.py
3
+ -----------------
4
+ Work around a known gradio_client bug that crashes the demo's API-info /
5
+ schema endpoint:
6
+
7
+ TypeError: argument of type 'bool' is not iterable
8
+ ... gradio_client/utils.py, get_type: `if "const" in schema`
9
+
10
+ It triggers when a component's JSON schema contains a boolean
11
+ `additionalProperties` (gr.Chatbot(type="messages") + gr.State produce one).
12
+ gradio_client recurses into that bool and treats it as a dict.
13
+
14
+ Importing this module patches the two offending functions to short-circuit on
15
+ bool schemas, returning "Any". Safe across gradio 4.x / 5.x — it only adds a
16
+ guard before the original logic runs.
17
+
18
+ Usage: `import demo._gradio_compat # noqa: F401` BEFORE building/launching the
19
+ Blocks app (already done in demo/app.py).
20
+ """
21
+
22
+ def _apply():
23
+ try:
24
+ import gradio_client.utils as gcu
25
+ except Exception:
26
+ return # gradio_client not importable — nothing to patch.
27
+
28
+ # Guard _json_schema_to_python_type against bool schemas.
29
+ if not getattr(gcu, "_cxr_patched_js", False):
30
+ _orig_js = gcu._json_schema_to_python_type
31
+
32
+ def _json_schema_to_python_type(schema, defs=None):
33
+ if isinstance(schema, bool):
34
+ return "Any"
35
+ return _orig_js(schema, defs)
36
+
37
+ gcu._json_schema_to_python_type = _json_schema_to_python_type
38
+ gcu._cxr_patched_js = True
39
+
40
+ # Guard get_type against non-dict schemas (the actual crash site).
41
+ if not getattr(gcu, "_cxr_patched_gt", False):
42
+ _orig_gt = gcu.get_type
43
+
44
+ def get_type(schema):
45
+ if not isinstance(schema, dict):
46
+ return "Any"
47
+ return _orig_gt(schema)
48
+
49
+ gcu.get_type = get_type
50
+ gcu._cxr_patched_gt = True
51
+
52
+
53
+ _apply()
demo/app.py CHANGED
@@ -98,6 +98,7 @@ def resolve_checkpoint(args) -> str:
98
 
99
 
100
  def build_ui(bot: CXRChatbot, max_new_tokens: int):
 
101
  import gradio as gr
102
 
103
  with gr.Blocks(title="CXR-HIEU Demo", theme=gr.themes.Soft()) as demo:
 
98
 
99
 
100
  def build_ui(bot: CXRChatbot, max_new_tokens: int):
101
+ import demo._gradio_compat # noqa: F401 (fix gradio_client schema crash)
102
  import gradio as gr
103
 
104
  with gr.Blocks(title="CXR-HIEU Demo", theme=gr.themes.Soft()) as demo: