File size: 2,821 Bytes
90f0ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb78609
90f0ef5
 
 
 
 
 
 
 
 
 
bb78609
 
 
 
 
 
 
 
 
 
90f0ef5
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
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