FeatureLab / app.py
VitalyVorobyev's picture
simple launch on Spaces
b94eb6b
"""
Entrypoint for both local and Hugging Face Spaces.
- Exposes `app` (FastAPI) for programmatic/ASGI usage.
- Exposes `demo` (Gradio Blocks) so Spaces with sdk: gradio can serve it directly.
- Only starts Uvicorn when executed locally, not on Spaces.
"""
import os
from backend.py.app.main import app # noqa: F401 (FastAPI app with Gradio mounted at "/")
from backend.py.app.gradio_demo.ui import build_demo
# Provide a top-level Gradio Blocks for Hugging Face Spaces (sdk: gradio)
demo = build_demo()
def _running_on_spaces() -> bool:
# Heuristics for HF Spaces runtime; avoid starting our own server there
return bool(
os.getenv("SPACE_ID")
or os.getenv("HF_SPACE_ID")
or os.getenv("GRADIO_SERVER_PORT")
or os.getenv("SYSTEM") == "spaces"
)
if __name__ == "__main__":
if _running_on_spaces():
demo.launch()
else:
import uvicorn
host = os.getenv("HOST", "127.0.0.1")
port = int(os.getenv("PORT", "7860"))
uvicorn.run("app:app", host=host, port=port, reload=False)