Spaces:
Sleeping
Sleeping
Create patch_utils.py
Browse files- patch_utils.py +87 -0
patch_utils.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Complete patch for gradio_client/utils.py
|
| 2 |
+
|
| 3 |
+
def get_desc(schema):
|
| 4 |
+
"""Get a description from a JSON schema if available."""
|
| 5 |
+
description = schema.get("description", "")
|
| 6 |
+
if description:
|
| 7 |
+
return f" # {description}"
|
| 8 |
+
return ""
|
| 9 |
+
|
| 10 |
+
def get_type(schema):
|
| 11 |
+
"""Get the type of a JSON schema."""
|
| 12 |
+
# Handle non-dict schema
|
| 13 |
+
if not isinstance(schema, dict):
|
| 14 |
+
if isinstance(schema, bool):
|
| 15 |
+
return "bool"
|
| 16 |
+
return str(type(schema).__name__)
|
| 17 |
+
|
| 18 |
+
if "type" in schema:
|
| 19 |
+
return schema["type"]
|
| 20 |
+
|
| 21 |
+
for t in ["oneOf", "anyOf", "object", "array"]:
|
| 22 |
+
if t in schema:
|
| 23 |
+
return t
|
| 24 |
+
|
| 25 |
+
if "additionalProperties" in schema:
|
| 26 |
+
return "additionalProperties"
|
| 27 |
+
|
| 28 |
+
return "any"
|
| 29 |
+
|
| 30 |
+
def _json_schema_to_python_type(schema, defs=None):
|
| 31 |
+
"""Convert a JSON schema to a Python type."""
|
| 32 |
+
defs = defs or {}
|
| 33 |
+
if schema is None:
|
| 34 |
+
return "None"
|
| 35 |
+
|
| 36 |
+
# Check if schema is a dictionary before trying to use 'in' operator
|
| 37 |
+
if isinstance(schema, dict):
|
| 38 |
+
if "$ref" in schema:
|
| 39 |
+
return _json_schema_to_python_type(defs[schema["$ref"].split("/")[-1]], defs)
|
| 40 |
+
|
| 41 |
+
if "const" in schema:
|
| 42 |
+
return repr(schema["const"])
|
| 43 |
+
|
| 44 |
+
if "enum" in schema:
|
| 45 |
+
return " | ".join([repr(i) for i in schema["enum"]])
|
| 46 |
+
|
| 47 |
+
# Handle boolean schema (this was the cause of the original error)
|
| 48 |
+
if isinstance(schema, bool):
|
| 49 |
+
return "bool"
|
| 50 |
+
|
| 51 |
+
# Get type based on schema
|
| 52 |
+
type_ = get_type(schema)
|
| 53 |
+
|
| 54 |
+
# Handle different types
|
| 55 |
+
if type_ == "object":
|
| 56 |
+
des = [
|
| 57 |
+
f"{n}: {_json_schema_to_python_type(v, defs)}{get_desc(v)}"
|
| 58 |
+
for n, v in schema.get("properties", {}).items()
|
| 59 |
+
]
|
| 60 |
+
return f"dict[{', '.join(des)}]" if des else "dict"
|
| 61 |
+
|
| 62 |
+
if type_ == "array":
|
| 63 |
+
items = schema.get("items", {})
|
| 64 |
+
elements = _json_schema_to_python_type(items, defs)
|
| 65 |
+
return f"list[{elements}]"
|
| 66 |
+
|
| 67 |
+
if type_ == "anyOf" or type_ == "oneOf":
|
| 68 |
+
desc = " | ".join([_json_schema_to_python_type(i, defs) for i in schema[type_]])
|
| 69 |
+
return desc
|
| 70 |
+
|
| 71 |
+
if type_ == "string":
|
| 72 |
+
if "format" in schema and schema["format"] == "binary":
|
| 73 |
+
return "bytes"
|
| 74 |
+
return "str"
|
| 75 |
+
|
| 76 |
+
if type_ == "additionalProperties" and isinstance(schema.get("additionalProperties"), dict):
|
| 77 |
+
return f"str, {_json_schema_to_python_type(schema['additionalProperties'], defs)}"
|
| 78 |
+
|
| 79 |
+
return type_
|
| 80 |
+
|
| 81 |
+
# This is the main function called by gradio
|
| 82 |
+
def json_schema_to_python_type(schema):
|
| 83 |
+
"""Convert a JSON schema to a Python type representation."""
|
| 84 |
+
if not isinstance(schema, dict):
|
| 85 |
+
return str(type(schema).__name__)
|
| 86 |
+
type_ = _json_schema_to_python_type(schema, schema.get("$defs"))
|
| 87 |
+
return type_
|