Cornelius commited on
Commit
c775d45
Β·
1 Parent(s): b4b1e11

Upgrade to Gradio 5.9.0 and add monkey-patch to fix schema bug

Browse files
Files changed (5) hide show
  1. README.md +1 -1
  2. README_HF_SPACE.md +1 -1
  3. app.py +57 -3
  4. requirements.txt +1 -1
  5. requirements_hf.txt +1 -1
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸŽ“
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.44.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.9.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
README_HF_SPACE.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸŽ“
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
- sdk_version: 4.44.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: blue
5
  colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.9.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -9,6 +9,60 @@ import os
9
  import subprocess
10
  from pathlib import Path
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Add project paths
13
  sys.path.insert(0, str(Path(__file__).parent))
14
  sys.path.insert(0, str(Path(__file__).parent / "teacher_agent_dev"))
@@ -213,7 +267,7 @@ with gr.Blocks(title="MentorFlow - Strategy Comparison") as demo:
213
  """)
214
 
215
  if __name__ == "__main__":
216
- # Disable API generation to avoid Gradio 4.44.x schema bug
217
- # This bypasses the TypeError: argument of type 'bool' is not iterable
218
- demo.launch(show_api=False)
219
 
 
9
  import subprocess
10
  from pathlib import Path
11
 
12
+ # Monkey-patch to fix Gradio 4.44.x schema generation bug
13
+ # Prevents TypeError: argument of type 'bool' is not iterable
14
+ import sys
15
+
16
+ # Patch BEFORE importing gradio to ensure it takes effect
17
+ def _patch_gradio_schema_bug():
18
+ """Patch Gradio's buggy schema generation."""
19
+ try:
20
+ from gradio_client import utils as gradio_client_utils
21
+
22
+ # Patch get_type - the main buggy function
23
+ if hasattr(gradio_client_utils, 'get_type'):
24
+ _original_get_type = gradio_client_utils.get_type
25
+
26
+ def _patched_get_type(schema):
27
+ """Handle bool schemas that cause the bug."""
28
+ # Bug fix: schema is sometimes a bool
29
+ if isinstance(schema, bool):
30
+ return "bool"
31
+ if schema is None:
32
+ return "Any"
33
+ # Must be dict to check membership
34
+ if not isinstance(schema, dict):
35
+ return "Any"
36
+ try:
37
+ return _original_get_type(schema)
38
+ except TypeError as e:
39
+ if "is not iterable" in str(e):
40
+ return "Any"
41
+ raise
42
+
43
+ gradio_client_utils.get_type = _patched_get_type
44
+
45
+ # Also patch the wrapper function that calls get_type
46
+ if hasattr(gradio_client_utils, '_json_schema_to_python_type'):
47
+ _original_json_to_type = gradio_client_utils._json_schema_to_python_type
48
+
49
+ def _patched_json_to_type(schema, defs=None):
50
+ """Catch errors in schema conversion."""
51
+ try:
52
+ return _original_json_to_type(schema, defs)
53
+ except (TypeError, AttributeError) as e:
54
+ if "is not iterable" in str(e) or "bool" in str(type(e)):
55
+ return "Any"
56
+ raise
57
+
58
+ gradio_client_utils._json_schema_to_python_type = _patched_json_to_type
59
+
60
+ except (ImportError, AttributeError):
61
+ pass
62
+
63
+ # Apply patch immediately
64
+ _patch_gradio_schema_bug()
65
+
66
  # Add project paths
67
  sys.path.insert(0, str(Path(__file__).parent))
68
  sys.path.insert(0, str(Path(__file__).parent / "teacher_agent_dev"))
 
267
  """)
268
 
269
  if __name__ == "__main__":
270
+ # For Hugging Face Spaces
271
+ # Monkey-patch above should fix schema bug, but upgrade to Gradio 5.x is recommended
272
+ demo.launch()
273
 
requirements.txt CHANGED
@@ -16,7 +16,7 @@ seaborn>=0.12.0
16
  tqdm>=4.65.0
17
 
18
  # Gradio for web interface (updated for security fixes)
19
- gradio>=4.44.1
20
 
21
  # Additional utilities
22
  scipy>=1.10.0
 
16
  tqdm>=4.65.0
17
 
18
  # Gradio for web interface (updated for security fixes)
19
+ gradio>=5.9.0
20
 
21
  # Additional utilities
22
  scipy>=1.10.0
requirements_hf.txt CHANGED
@@ -16,7 +16,7 @@ seaborn>=0.12.0
16
  tqdm>=4.65.0
17
 
18
  # Gradio for web interface (updated for security fixes)
19
- gradio>=4.44.1
20
 
21
  # Additional utilities
22
  scipy>=1.10.0
 
16
  tqdm>=4.65.0
17
 
18
  # Gradio for web interface (updated for security fixes)
19
+ gradio>=5.9.0
20
 
21
  # Additional utilities
22
  scipy>=1.10.0