File size: 2,174 Bytes
a8da4e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Patch for gradio_client.utils._json_schema_to_python_type function
to handle boolean schema values properly.

This patch adds a check for boolean schema values before trying to access them as dictionaries.
"""

import importlib
import logging

logger = logging.getLogger(__name__)

def apply_patch():
    """Apply the monkey patch to fix the TypeError in gradio_client.utils._json_schema_to_python_type."""
    try:
        # Import the module
        import gradio_client.utils as utils
        
        # Store the original function
        original_func = utils._json_schema_to_python_type
        
        # Define the patched function
        def patched_json_schema_to_python_type(schema, defs=None):
            """Patched version that handles boolean schemas."""
            if schema is None:
                return "None"
            
            # Handle boolean schema values
            if isinstance(schema, bool):
                return str(schema).lower()
                
            # Continue with the original function for non-boolean schemas
            return original_func(schema, defs)
        
        # Apply the patch
        utils._json_schema_to_python_type = patched_json_schema_to_python_type
        
        # Also patch the get_type function
        original_get_type = utils.get_type
        
        def patched_get_type(schema):
            """Patched version of get_type that handles boolean schemas."""
            if isinstance(schema, bool):
                return "bool"
            return original_get_type(schema)
        
        utils.get_type = patched_get_type
        
        logger.info("Successfully applied patch to gradio_client.utils._json_schema_to_python_type")
        return True
    except Exception as e:
        logger.error(f"Failed to apply patch: {e}")
        import traceback
        logger.debug(traceback.format_exc())
        return False

if __name__ == "__main__":
    # Set up logging
    logging.basicConfig(level=logging.DEBUG)
    
    # Apply the patch
    if apply_patch():
        print("Patch applied successfully.")
    else:
        print("Failed to apply patch.")