feng-x commited on
Commit
406f5e2
·
verified ·
1 Parent(s): e09def3

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -29,6 +29,38 @@ import numpy as np
29
  import spaces # type: ignore
30
  import gradio as gr
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  ROOT_DIR = Path(__file__).resolve().parent
33
  sys.path.insert(0, str(ROOT_DIR))
34
 
@@ -420,4 +452,10 @@ demo = build_demo()
420
 
421
 
422
  if __name__ == "__main__":
423
- demo.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")))
 
 
 
 
 
 
 
29
  import spaces # type: ignore
30
  import gradio as gr
31
 
32
+ # --------------------------------------------------------------------------- #
33
+ # Monkey-patch for a known Gradio 4.44 / gradio_client bug: when the API-info
34
+ # endpoint builds a schema for an output that includes `additionalProperties:
35
+ # True` (a bool, not a dict), `gradio_client.utils.get_type()` tries
36
+ # `"const" in schema` and raises `TypeError: argument of type 'bool' is not
37
+ # iterable`. Our `gr.JSON` output lands in that code path on first page load
38
+ # and crashes the whole Space. Wrap `get_type` so any non-dict schema resolves
39
+ # to the permissive "Any" type. Has no effect on well-typed schemas.
40
+ # --------------------------------------------------------------------------- #
41
+ try:
42
+ import gradio_client.utils as _gc_utils # noqa: E402
43
+
44
+ _orig_get_type = _gc_utils.get_type
45
+
46
+ def _safe_get_type(schema): # type: ignore[no-redef]
47
+ if not isinstance(schema, dict):
48
+ return "Any"
49
+ return _orig_get_type(schema)
50
+
51
+ _gc_utils.get_type = _safe_get_type # type: ignore[assignment]
52
+
53
+ _orig_json_schema_to_python_type = _gc_utils._json_schema_to_python_type
54
+
55
+ def _safe_json_schema_to_python_type(schema, defs=None): # type: ignore[no-redef]
56
+ if not isinstance(schema, dict):
57
+ return "Any"
58
+ return _orig_json_schema_to_python_type(schema, defs)
59
+
60
+ _gc_utils._json_schema_to_python_type = _safe_json_schema_to_python_type # type: ignore[assignment]
61
+ except Exception as _exc: # noqa: BLE001
62
+ print(f"[v5] gradio_client get_type patch skipped: {_exc}")
63
+
64
  ROOT_DIR = Path(__file__).resolve().parent
65
  sys.path.insert(0, str(ROOT_DIR))
66
 
 
452
 
453
 
454
  if __name__ == "__main__":
455
+ # On HF Gradio Spaces (including ZeroGPU) the platform runs `python app.py`
456
+ # and expects `demo.launch()` with no explicit server_name/port — the
457
+ # `spaces/zero/gradio.py` launch wrapper binds the port itself. Passing
458
+ # `server_name="0.0.0.0"` triggers the "localhost not accessible" self-
459
+ # check inside Gradio and crashes startup. Locally, `demo.launch()` still
460
+ # serves on 127.0.0.1:7860 by default.
461
+ demo.queue().launch(show_api=False)