spacedout-bits commited on
Commit
8e613ea
·
verified ·
1 Parent(s): f8d827b

Let Gradio own its port on Spaces so frontend assets load

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -446,12 +446,19 @@ def serve_via_gradio() -> None:
446
  with _launch_lock:
447
  rt = build_runtime()
448
 
449
- demo.launch(
450
- server_name="0.0.0.0",
451
- server_port=int(os.environ.get("APP_PORT") or 7860),
452
- prevent_thread_lock=True, # returns so we can attach routes
453
- show_api=False,
454
- )
 
 
 
 
 
 
 
455
 
456
  # demo.app only exists once launched.
457
  demo.app.state.rt = rt
 
446
  with _launch_lock:
447
  rt = build_runtime()
448
 
449
+ # On a Space, let Gradio use its own env-driven defaults (GRADIO_SERVER_PORT
450
+ # and the Space root path). Forcing a port breaks the frontend asset URLs
451
+ # behind HF's proxy: the CSS/JS 404, no JavaScript runs, and the page never
452
+ # leaves its initial server-rendered state. That looks exactly like "the
453
+ # dashboard shows nothing" -- demo.load never fires, so the sign-in token in
454
+ # the query string is never read.
455
+ launch_kwargs: dict = {"prevent_thread_lock": True, "show_api": False}
456
+ if port := os.environ.get("APP_PORT"):
457
+ # Self-hosting / local dev, where nothing sets GRADIO_SERVER_PORT.
458
+ launch_kwargs["server_name"] = "0.0.0.0"
459
+ launch_kwargs["server_port"] = int(port)
460
+
461
+ demo.launch(**launch_kwargs) # returns so we can attach routes
462
 
463
  # demo.app only exists once launched.
464
  demo.app.state.rt = rt