photogen1 / gradio_patch.py
stateofw's picture
Fix Gradio 4.44.0 compatibility in gradio_patch.py
bb78609
"""
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
# Check if the patch is needed by looking for the problematic function
if hasattr(gradio_client.utils, '_json_schema_to_python_type'):
# Store the original function
_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"""
# Handle boolean schemas (valid in JSON Schema spec)
if isinstance(schema, bool):
return "Any"
# For all other cases, use the original function
return _original_json_schema_to_python_type(schema, defs)
# Apply the patch
gradio_client.utils._json_schema_to_python_type = patched_json_schema_to_python_type
print("✅ Applied Gradio boolean schema patch")
# Also patch get_type if it exists
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"""
# Handle boolean schemas
if isinstance(schema, bool):
return "Any"
# Ensure schema is a dict before checking for 'const'
if not isinstance(schema, dict):
return "Any"
# Use original function for dict schemas
# Check if original function accepts is_input parameter
try:
# Try with both parameters first
if is_input is not None:
return _original_get_type(schema, is_input)
else:
return _original_get_type(schema)
except TypeError:
# If it fails, try with just schema parameter
return _original_get_type(schema)
# Apply the patch
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