Spaces:
Paused
Paused
Update patch_gradio_jsonschema.py
Browse files- patch_gradio_jsonschema.py +19 -59
patch_gradio_jsonschema.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 1 |
# patch_gradio_jsonschema.py
|
| 2 |
"""
|
| 3 |
-
<<<<<<< HEAD
|
| 4 |
-
Runtime monkeypatch to make gradio_client JSON Schema handling robust to boolean schemas.
|
| 5 |
-
Import this BEFORE importing gradio or gradio_client (so do it at the top of your entrypoint).
|
| 6 |
-
=======
|
| 7 |
Robust runtime monkeypatch for gradio_client JSON Schema handling.
|
| 8 |
Import this BEFORE importing gradio or gradio_client.
|
| 9 |
-
This version uses *args/**kwargs wrappers to be compatible with varying signatures
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
|
| 13 |
try:
|
|
@@ -24,44 +20,29 @@ def _bool_to_safe_schema(b: bool):
|
|
| 24 |
else:
|
| 25 |
return {"type": "___boolean_schema_false___"}
|
| 26 |
|
| 27 |
-
<<<<<<< HEAD
|
| 28 |
-
def _wrap_get_type(orig):
|
| 29 |
-
def wrapper(schema):
|
| 30 |
-
if _is_bool_schema(schema):
|
| 31 |
-
schema = _bool_to_safe_schema(schema)
|
| 32 |
-
return orig(schema)
|
| 33 |
-
return wrapper
|
| 34 |
-
|
| 35 |
-
def _wrap_json_schema_to_python_type(orig):
|
| 36 |
-
def wrapper(schema, defs=None):
|
| 37 |
-
if _is_bool_schema(schema):
|
| 38 |
-
schema = _bool_to_safe_schema(schema)
|
| 39 |
-
return orig(schema, defs)
|
| 40 |
-
=======
|
| 41 |
def _wrap_function_coercing_schema(orig):
|
| 42 |
"""
|
| 43 |
-
Wraps functions that
|
| 44 |
-
|
| 45 |
Works for functions with signature like f(schema), f(schema, defs), etc.
|
| 46 |
"""
|
| 47 |
def wrapper(*args, **kwargs):
|
|
|
|
| 48 |
if len(args) >= 1 and _is_bool_schema(args[0]):
|
| 49 |
new_first = _bool_to_safe_schema(args[0])
|
| 50 |
new_args = (new_first,) + args[1:]
|
| 51 |
return orig(*new_args, **kwargs)
|
| 52 |
-
#
|
| 53 |
if "schema" in kwargs and _is_bool_schema(kwargs["schema"]):
|
| 54 |
kw = dict(kwargs)
|
| 55 |
kw["schema"] = _bool_to_safe_schema(kwargs["schema"])
|
| 56 |
return orig(*args, **kw)
|
| 57 |
return orig(*args, **kwargs)
|
| 58 |
-
# copy metadata if available
|
| 59 |
try:
|
| 60 |
wrapper.__name__ = getattr(orig, "__name__", "wrapped")
|
| 61 |
wrapper.__doc__ = getattr(orig, "__doc__", "")
|
| 62 |
except Exception:
|
| 63 |
pass
|
| 64 |
-
>>>>>>> 6c8c1495789d779c0c6e4ad7f97c161874800b94
|
| 65 |
return wrapper
|
| 66 |
|
| 67 |
def apply_patch():
|
|
@@ -70,47 +51,27 @@ def apply_patch():
|
|
| 70 |
try:
|
| 71 |
import gradio_client.utils as _gc_utils
|
| 72 |
except Exception:
|
| 73 |
-
<<<<<<< HEAD
|
| 74 |
-
return False
|
| 75 |
-
|
| 76 |
-
if hasattr(_gc_utils, "get_type"):
|
| 77 |
-
_gc_utils.get_type = _wrap_get_type(_gc_utils.get_type)
|
| 78 |
-
|
| 79 |
-
if hasattr(_gc_utils, "_json_schema_to_python_type"):
|
| 80 |
-
_gc_utils._json_schema_to_python_type = _wrap_json_schema_to_python_type(
|
| 81 |
-
_gc_utils._json_schema_to_python_type
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
if hasattr(_gc_utils, "json_schema_to_python_type"):
|
| 85 |
-
orig_public = _gc_utils.json_schema_to_python_type
|
| 86 |
-
def public_wrapper(schema, defs=None):
|
| 87 |
-
if _is_bool_schema(schema):
|
| 88 |
-
schema = _bool_to_safe_schema(schema)
|
| 89 |
-
return orig_public(schema, defs)
|
| 90 |
-
_gc_utils.json_schema_to_python_type = public_wrapper
|
| 91 |
-
|
| 92 |
-
return True
|
| 93 |
-
|
| 94 |
-
apply_patch()
|
| 95 |
-
# diagnostic print
|
| 96 |
-
try:
|
| 97 |
-
print("patch_gradio_jsonschema applied")
|
| 98 |
-
=======
|
| 99 |
-
# nothing to patch yet
|
| 100 |
return False
|
| 101 |
|
| 102 |
-
# Patch get_type if present
|
| 103 |
if hasattr(_gc_utils, "get_type"):
|
|
|
|
|
|
|
|
|
|
| 104 |
_gc_utils.get_type = _wrap_function_coercing_schema(_gc_utils.get_type)
|
| 105 |
|
| 106 |
-
# Patch internal
|
| 107 |
if hasattr(_gc_utils, "_json_schema_to_python_type"):
|
|
|
|
|
|
|
| 108 |
_gc_utils._json_schema_to_python_type = _wrap_function_coercing_schema(
|
| 109 |
_gc_utils._json_schema_to_python_type
|
| 110 |
)
|
| 111 |
|
| 112 |
-
# Patch public helper if present
|
| 113 |
if hasattr(_gc_utils, "json_schema_to_python_type"):
|
|
|
|
|
|
|
| 114 |
_gc_utils.json_schema_to_python_type = _wrap_function_coercing_schema(
|
| 115 |
_gc_utils.json_schema_to_python_type
|
| 116 |
)
|
|
@@ -120,9 +81,8 @@ try:
|
|
| 120 |
# apply at import time
|
| 121 |
apply_patch()
|
| 122 |
|
| 123 |
-
# diagnostic
|
| 124 |
try:
|
| 125 |
-
print("patch_gradio_jsonschema applied (
|
| 126 |
-
>>>>>>> 6c8c1495789d779c0c6e4ad7f97c161874800b94
|
| 127 |
except Exception:
|
| 128 |
-
pass
|
|
|
|
| 1 |
# patch_gradio_jsonschema.py
|
| 2 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
Robust runtime monkeypatch for gradio_client JSON Schema handling.
|
| 4 |
Import this BEFORE importing gradio or gradio_client.
|
| 5 |
+
This version uses *args/**kwargs wrappers to be compatible with varying signatures
|
| 6 |
+
and avoids merge-conflict artefacts.
|
| 7 |
"""
|
| 8 |
|
| 9 |
try:
|
|
|
|
| 20 |
else:
|
| 21 |
return {"type": "___boolean_schema_false___"}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def _wrap_function_coercing_schema(orig):
|
| 24 |
"""
|
| 25 |
+
Wraps functions that accept a schema as first positional arg.
|
| 26 |
+
Coerces boolean schemas into safe dicts before calling the original.
|
| 27 |
Works for functions with signature like f(schema), f(schema, defs), etc.
|
| 28 |
"""
|
| 29 |
def wrapper(*args, **kwargs):
|
| 30 |
+
# If first positional arg is a boolean schema, replace it
|
| 31 |
if len(args) >= 1 and _is_bool_schema(args[0]):
|
| 32 |
new_first = _bool_to_safe_schema(args[0])
|
| 33 |
new_args = (new_first,) + args[1:]
|
| 34 |
return orig(*new_args, **kwargs)
|
| 35 |
+
# If schema passed as keyword arg
|
| 36 |
if "schema" in kwargs and _is_bool_schema(kwargs["schema"]):
|
| 37 |
kw = dict(kwargs)
|
| 38 |
kw["schema"] = _bool_to_safe_schema(kwargs["schema"])
|
| 39 |
return orig(*args, **kw)
|
| 40 |
return orig(*args, **kwargs)
|
|
|
|
| 41 |
try:
|
| 42 |
wrapper.__name__ = getattr(orig, "__name__", "wrapped")
|
| 43 |
wrapper.__doc__ = getattr(orig, "__doc__", "")
|
| 44 |
except Exception:
|
| 45 |
pass
|
|
|
|
| 46 |
return wrapper
|
| 47 |
|
| 48 |
def apply_patch():
|
|
|
|
| 51 |
try:
|
| 52 |
import gradio_client.utils as _gc_utils
|
| 53 |
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return False
|
| 55 |
|
| 56 |
+
# Patch get_type if present
|
| 57 |
if hasattr(_gc_utils, "get_type"):
|
| 58 |
+
# preserve original if not already saved
|
| 59 |
+
if not hasattr(_gc_utils, "_orig_get_type"):
|
| 60 |
+
_gc_utils._orig_get_type = _gc_utils.get_type
|
| 61 |
_gc_utils.get_type = _wrap_function_coercing_schema(_gc_utils.get_type)
|
| 62 |
|
| 63 |
+
# Patch internal _json_schema_to_python_type if present
|
| 64 |
if hasattr(_gc_utils, "_json_schema_to_python_type"):
|
| 65 |
+
if not hasattr(_gc_utils, "_orig__json_schema_to_python_type"):
|
| 66 |
+
_gc_utils._orig__json_schema_to_python_type = _gc_utils._json_schema_to_python_type
|
| 67 |
_gc_utils._json_schema_to_python_type = _wrap_function_coercing_schema(
|
| 68 |
_gc_utils._json_schema_to_python_type
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Patch public helper json_schema_to_python_type if present
|
| 72 |
if hasattr(_gc_utils, "json_schema_to_python_type"):
|
| 73 |
+
if not hasattr(_gc_utils, "_orig_json_schema_to_python_type"):
|
| 74 |
+
_gc_utils._orig_json_schema_to_python_type = _gc_utils.json_schema_to_python_type
|
| 75 |
_gc_utils.json_schema_to_python_type = _wrap_function_coercing_schema(
|
| 76 |
_gc_utils.json_schema_to_python_type
|
| 77 |
)
|
|
|
|
| 81 |
# apply at import time
|
| 82 |
apply_patch()
|
| 83 |
|
| 84 |
+
# small diagnostic print visible in runtime logs
|
| 85 |
try:
|
| 86 |
+
print("patch_gradio_jsonschema applied (clean)")
|
|
|
|
| 87 |
except Exception:
|
| 88 |
+
pass
|