File size: 3,127 Bytes
06947bf
 
6c8c149
 
2ed172a
 
06947bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c8c149
 
2ed172a
 
6c8c149
 
 
2ed172a
6c8c149
 
 
 
2ed172a
6c8c149
 
 
 
 
 
 
 
 
 
06947bf
 
 
 
 
 
 
 
667bfcf
 
2ed172a
667bfcf
2ed172a
 
 
6c8c149
667bfcf
2ed172a
667bfcf
2ed172a
 
6c8c149
667bfcf
 
 
2ed172a
667bfcf
2ed172a
 
6c8c149
 
 
667bfcf
 
 
6c8c149
667bfcf
 
2ed172a
667bfcf
2ed172a
06947bf
2ed172a
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# patch_gradio_jsonschema.py
"""
Robust runtime monkeypatch for gradio_client JSON Schema handling.
Import this BEFORE importing gradio or gradio_client.
This version uses *args/**kwargs wrappers to be compatible with varying signatures
and avoids merge-conflict artefacts.
"""

try:
    import gradio_client.utils as _gc_utils
except Exception:
    _gc_utils = None

def _is_bool_schema(x):
    return isinstance(x, bool)

def _bool_to_safe_schema(b: bool):
    if b:
        return {}
    else:
        return {"type": "___boolean_schema_false___"}

def _wrap_function_coercing_schema(orig):
    """
    Wraps functions that accept a schema as first positional arg.
    Coerces boolean schemas into safe dicts before calling the original.
    Works for functions with signature like f(schema), f(schema, defs), etc.
    """
    def wrapper(*args, **kwargs):
        # If first positional arg is a boolean schema, replace it
        if len(args) >= 1 and _is_bool_schema(args[0]):
            new_first = _bool_to_safe_schema(args[0])
            new_args = (new_first,) + args[1:]
            return orig(*new_args, **kwargs)
        # If schema passed as keyword arg
        if "schema" in kwargs and _is_bool_schema(kwargs["schema"]):
            kw = dict(kwargs)
            kw["schema"] = _bool_to_safe_schema(kwargs["schema"])
            return orig(*args, **kw)
        return orig(*args, **kwargs)
    try:
        wrapper.__name__ = getattr(orig, "__name__", "wrapped")
        wrapper.__doc__ = getattr(orig, "__doc__", "")
    except Exception:
        pass
    return wrapper

def apply_patch():
    global _gc_utils
    if _gc_utils is None:
        try:
            import gradio_client.utils as _gc_utils
        except Exception:
            return False

    # Patch get_type if present
    if hasattr(_gc_utils, "get_type"):
        # preserve original if not already saved
        if not hasattr(_gc_utils, "_orig_get_type"):
            _gc_utils._orig_get_type = _gc_utils.get_type
        _gc_utils.get_type = _wrap_function_coercing_schema(_gc_utils.get_type)

    # Patch internal _json_schema_to_python_type if present
    if hasattr(_gc_utils, "_json_schema_to_python_type"):
        if not hasattr(_gc_utils, "_orig__json_schema_to_python_type"):
            _gc_utils._orig__json_schema_to_python_type = _gc_utils._json_schema_to_python_type
        _gc_utils._json_schema_to_python_type = _wrap_function_coercing_schema(
            _gc_utils._json_schema_to_python_type
        )

    # Patch public helper json_schema_to_python_type if present
    if hasattr(_gc_utils, "json_schema_to_python_type"):
        if not hasattr(_gc_utils, "_orig_json_schema_to_python_type"):
            _gc_utils._orig_json_schema_to_python_type = _gc_utils.json_schema_to_python_type
        _gc_utils.json_schema_to_python_type = _wrap_function_coercing_schema(
            _gc_utils.json_schema_to_python_type
        )

    return True

# apply at import time
apply_patch()

# small diagnostic print visible in runtime logs
try:
    print("patch_gradio_jsonschema applied (clean)")
except Exception:
    pass