Spaces:
Sleeping
Sleeping
File size: 3,769 Bytes
75b1002 4365e04 75b1002 aa1e3d1 77ff078 aa1e3d1 9072c5e 75b1002 aa1e3d1 77ff078 4365e04 aa1e3d1 4365e04 77ff078 aa1e3d1 4365e04 77ff078 7150edc 77ff078 aa1e3d1 75b1002 aa1e3d1 77ff078 9072c5e 75b1002 4365e04 75b1002 4365e04 aa1e3d1 75b1002 | 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #!/usr/bin/env python3
"""Hugging Face Space entrypoint for the RadGenome demo."""
import sys
import os
import gradio_client.utils as gradio_client_utils
import gradio.routes as gradio_routes
from demo_log import build_app, parse_args
def _patch_gradio_json_schema_bug():
"""
Gradio 4.44.x can crash when JSON schema nodes are bool values.
See: https://github.com/gradio-app/gradio/issues/11084
"""
original_get_type = gradio_client_utils.get_type
original_json_schema_to_python_type_top = gradio_client_utils.json_schema_to_python_type
original_json_schema_to_python_type = gradio_client_utils._json_schema_to_python_type
def safe_get_type(schema):
if isinstance(schema, bool):
return "any"
return original_get_type(schema)
def safe_json_schema_to_python_type(schema, defs):
if isinstance(schema, bool):
# JSON Schema allows boolean schemas; gradio_client 1.3.0 crashes on this.
return "Any"
return original_json_schema_to_python_type(schema, defs)
def safe_json_schema_to_python_type_top(schema):
try:
return original_json_schema_to_python_type_top(schema)
except Exception:
return "Any"
gradio_client_utils.get_type = safe_get_type
gradio_client_utils._json_schema_to_python_type = safe_json_schema_to_python_type
gradio_client_utils.json_schema_to_python_type = safe_json_schema_to_python_type_top
def _patch_template_response_compat():
"""
Handle Starlette TemplateResponse signature drift across environments.
"""
original_template_response = gradio_routes.templates.TemplateResponse
def safe_template_response(*args, **kwargs):
# Old-style positional call: TemplateResponse(name_str, context_dict)
# New Starlette interprets this as (request=name_str, name=context_dict),
# causing "unhashable type: 'dict'" when Jinja2 tries to cache on `name`.
if args and isinstance(args[0], str) and len(args) >= 2 and isinstance(args[1], dict):
name = args[0]
context = dict(args[1])
request = context.get("request")
try:
return original_template_response(request=request, name=name, context=context)
except Exception:
return original_template_response(name, context, *args[2:], **kwargs)
if not args and {"request", "name", "context"} <= set(kwargs):
request = kwargs["request"]
name = kwargs["name"]
context = dict(kwargs.get("context") or {})
context.setdefault("request", request)
try:
return original_template_response(request=request, name=name, context=context)
except Exception:
return original_template_response(name, context)
return original_template_response(*args, **kwargs)
gradio_routes.templates.TemplateResponse = safe_template_response
def _build_demo():
# Avoid argparse conflicts if the runtime injects extra CLI flags.
argv_backup = sys.argv
try:
sys.argv = [argv_backup[0]]
args = parse_args()
finally:
sys.argv = argv_backup
_patch_gradio_json_schema_bug()
_patch_template_response_compat()
demo = build_app(args)
return demo, args
demo, _args = _build_demo()
if __name__ == "__main__":
in_hf_space = bool(os.getenv("SPACE_ID") or os.getenv("HF_SPACE_ID"))
# In Spaces runtime, enforce share link creation to avoid localhost access errors.
launch_share = True if in_hf_space else _args.share
demo.launch(
share=launch_share,
server_name=_args.server_name or "0.0.0.0",
server_port=_args.server_port,
)
|