unity4ar commited on
Commit
4f670ea
·
verified ·
1 Parent(s): fbd952d

Force demo.launch to bind 0.0.0.0:$PORT on HF Spaces (CLI hot-reload ignores GRADIO_SERVER_NAME)

Browse files
Files changed (1) hide show
  1. app.py +17 -0
app.py CHANGED
@@ -1857,6 +1857,23 @@ def _game_log_text(state: GameState) -> str:
1857
  # manual launch behind __main__ so `python app.py` works as before.
1858
  demo = build_app()
1859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1860
 
1861
  if __name__ == "__main__":
1862
  # Bind address/port are env-overridable so the same entrypoint works locally
 
1857
  # manual launch behind __main__ so `python app.py` works as before.
1858
  demo = build_app()
1859
 
1860
+ # The HF Gradio SDK runs `gradio app.py` in hot-reload mode, which calls
1861
+ # `demo.launch()` with hardcoded server_name=127.0.0.1 and ignores the
1862
+ # GRADIO_SERVER_NAME env var. On HF Spaces the reverse proxy expects the app
1863
+ # on 0.0.0.0:7860, so loopback is unreachable. Wrap launch() to force the
1864
+ # correct bind regardless of what the CLI passes in.
1865
+ if os.getenv("SPACE_ID"):
1866
+ _project_root = str(PROJECT_ROOT)
1867
+ _orig_launch = demo.launch
1868
+
1869
+ def _hf_forced_launch(**kwargs):
1870
+ kwargs["server_name"] = "0.0.0.0"
1871
+ kwargs["server_port"] = int(os.getenv("PORT") or "7860")
1872
+ kwargs.setdefault("allowed_paths", [_project_root])
1873
+ return _orig_launch(**kwargs)
1874
+
1875
+ demo.launch = _hf_forced_launch
1876
+
1877
 
1878
  if __name__ == "__main__":
1879
  # Bind address/port are env-overridable so the same entrypoint works locally