Spaces:
Running on Zero

openhands commited on
Commit
c10ebe5
·
1 Parent(s): 9d0011c

fix: serve the API from Gradio's own app so ZeroGPU accepts the Space

Browse files
Files changed (1) hide show
  1. app.py +38 -22
app.py CHANGED
@@ -14,6 +14,17 @@ escape its own workspace.
14
 
15
  from __future__ import annotations
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  import asyncio
18
  import hmac
19
  import json
@@ -636,27 +647,32 @@ def _diagnose() -> None:
636
  print("[diag]", command[0], "failed:", error)
637
 
638
 
639
- if __name__ == "__main__":
640
- from gradio import http_server
 
 
 
 
 
 
 
 
 
 
 
 
641
 
 
 
 
 
 
 
 
 
 
642
  _diagnose()
643
- # The Space supervisor owns 7860 on ZeroGPU and proxies to the next free
644
- # port; GRADIO_NUM_PORTS=1 disables Gradio's own scan, so probe manually.
645
- requested = 7860
646
- for offset in range(20):
647
- if port_is_free(requested + offset):
648
- requested += offset
649
- break
650
- # Gradio's SpacesReloader expects `app.blocks`; the FastAPI app we built
651
- # with mount_gradio_app does not carry it, so expose the Blocks manually.
652
- setattr(app, "blocks", demo) # noqa: B010
653
- print("[start] requesting port", requested, flush=True)
654
- server_name, bound_port, _url, server = http_server.start_server(
655
- app=app, server_name="0.0.0.0", server_port=requested
656
- )
657
- print(f"[start] EXELYN sandbox listening on {server_name}:{bound_port}", flush=True)
658
- try:
659
- while True:
660
- time.sleep(3600)
661
- except KeyboardInterrupt: # pragma: no cover
662
- server.close()
 
14
 
15
  from __future__ import annotations
16
 
17
+ import os as _os
18
+
19
+ # ZeroGPU only proxies the port Gradio picks, and GRADIO_NUM_PORTS defaults to 1
20
+ # on Spaces; widen the scan before gradio reads it.
21
+ _os.environ.setdefault("GRADIO_NUM_PORTS", "20")
22
+
23
+ try: # ZeroGPU runtime helper; absent on plain hosts
24
+ import spaces # noqa: F401
25
+ except Exception: # noqa: BLE001
26
+ spaces = None # type: ignore[assignment]
27
+
28
  import asyncio
29
  import hmac
30
  import json
 
647
  print("[diag]", command[0], "failed:", error)
648
 
649
 
650
+ def _prepend_sandbox_routes(target: FastAPI) -> None:
651
+ """Puts /api/* and /preview/* ahead of Gradio's catch-all mount."""
652
+ ours = [
653
+ route
654
+ for route in app.router.routes
655
+ if str(getattr(route, "path", "")).startswith(("/api/", "/preview/"))
656
+ ]
657
+ target.router.routes[0:0] = ours
658
+
659
+
660
+ def _patch_gradio_app() -> None:
661
+ from gradio.routes import App as GradioApp
662
+
663
+ original = GradioApp.create_app
664
 
665
+ def create_app(blocks, *args, **kwargs): # type: ignore[no-untyped-def]
666
+ gradio_app = original(blocks, *args, **kwargs)
667
+ _prepend_sandbox_routes(gradio_app)
668
+ return gradio_app
669
+
670
+ GradioApp.create_app = create_app # type: ignore[method-assign]
671
+
672
+
673
+ if __name__ == "__main__":
674
  _diagnose()
675
+ _patch_gradio_app()
676
+ # demo.launch() runs the ZeroGPU handshake and lets Gradio pick the first
677
+ # free port behind the Space supervisor, then serves the patched app.
678
+ demo.launch(server_name="0.0.0.0", quiet=False)