|
|
""" |
|
|
Monkey patch for Gradio to handle boolean JSON schemas |
|
|
This fixes the TypeError: argument of type 'bool' is not iterable |
|
|
""" |
|
|
|
|
|
def apply_gradio_patch(): |
|
|
"""Apply the patch to handle boolean schemas in gradio_client""" |
|
|
try: |
|
|
import gradio_client.utils |
|
|
|
|
|
|
|
|
if hasattr(gradio_client.utils, '_json_schema_to_python_type'): |
|
|
|
|
|
_original_json_schema_to_python_type = gradio_client.utils._json_schema_to_python_type |
|
|
|
|
|
def patched_json_schema_to_python_type(schema, defs=None): |
|
|
"""Patched version that handles boolean schemas""" |
|
|
|
|
|
if isinstance(schema, bool): |
|
|
return "Any" |
|
|
|
|
|
|
|
|
return _original_json_schema_to_python_type(schema, defs) |
|
|
|
|
|
|
|
|
gradio_client.utils._json_schema_to_python_type = patched_json_schema_to_python_type |
|
|
print("✅ Applied Gradio boolean schema patch") |
|
|
|
|
|
|
|
|
if hasattr(gradio_client.utils, 'get_type'): |
|
|
_original_get_type = gradio_client.utils.get_type |
|
|
|
|
|
def patched_get_type(schema, is_input=None): |
|
|
"""Patched version of get_type that handles boolean schemas""" |
|
|
|
|
|
if isinstance(schema, bool): |
|
|
return "Any" |
|
|
|
|
|
|
|
|
if not isinstance(schema, dict): |
|
|
return "Any" |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
if is_input is not None: |
|
|
return _original_get_type(schema, is_input) |
|
|
else: |
|
|
return _original_get_type(schema) |
|
|
except TypeError: |
|
|
|
|
|
return _original_get_type(schema) |
|
|
|
|
|
|
|
|
gradio_client.utils.get_type = patched_get_type |
|
|
print("✅ Applied Gradio get_type patch") |
|
|
|
|
|
return True |
|
|
|
|
|
except ImportError: |
|
|
print("⚠️ gradio_client not found, patch not needed") |
|
|
return False |
|
|
except Exception as e: |
|
|
print(f"⚠️ Failed to apply Gradio patch: {e}") |
|
|
return False |