#!/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, )