GitHub Actions commited on
Commit
c70fa57
·
1 Parent(s): 6b2d40b

fix: serve webagent via GRADIO_ALLOWED_PATHS env var (works on HF Space)

Browse files

The previous allowed_paths in if __name__ == '__main__' was not applied
when HF Space calls demo.launch() without the allowed_paths parameter.
Gradio's launch() falls back to GRADIO_ALLOWED_PATHS env var, so setting
it at module load time ensures the /file= endpoint works on all launchers.

Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -392,23 +392,21 @@ _ui = _build_ui(
392
  node_id=_node.node_id,
393
  community_id=_node.community_id,
394
  )
395
- demo = _ui.build()
396
 
397
- # Mount the browser agent as static files at /webagent/ so the 'a' key modal works
398
- try:
399
- import os
400
- from pathlib import Path
401
- from fastapi.staticfiles import StaticFiles
 
 
 
 
402
 
403
- _webagent_dir = Path(__file__).parent / "webagent"
404
- if _webagent_dir.exists():
405
- demo.app.mount("/webagent", StaticFiles(directory=str(_webagent_dir)), name="webagent")
406
- except Exception:
407
- pass
408
 
409
  if __name__ == "__main__":
410
- import os as _os
411
- _webagent_dir = _os.path.join(_os.path.dirname(_os.path.abspath(__file__)), "webagent")
412
  demo.launch(
413
- allowed_paths=[_webagent_dir] if _os.path.exists(_webagent_dir) else [],
414
  )
 
 
392
  node_id=_node.node_id,
393
  community_id=_node.community_id,
394
  )
 
395
 
396
+ # Set GRADIO_ALLOWED_PATHS so the /file= endpoint serves the webagent on any launcher
397
+ # (HF Space calls demo.launch() without allowed_paths; the env-var is the fallback).
398
+ _webagent_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "webagent")
399
+ if os.path.exists(_webagent_dir):
400
+ _existing = os.environ.get("GRADIO_ALLOWED_PATHS", "")
401
+ _paths = [p for p in _existing.split(",") if p.strip()] if _existing else []
402
+ if _webagent_dir not in _paths:
403
+ _paths.append(_webagent_dir)
404
+ os.environ["GRADIO_ALLOWED_PATHS"] = ",".join(_paths)
405
 
406
+ demo = _ui.build()
 
 
 
 
407
 
408
  if __name__ == "__main__":
 
 
409
  demo.launch(
410
+ allowed_paths=[_webagent_dir] if os.path.exists(_webagent_dir) else [],
411
  )
412
+