Melika Kheirieh commited on
Commit
dd9ee64
·
1 Parent(s): 329daab

fix(runtime): use start.py entrypoint to keep FastAPI & Gradio alive on Hugging Face

Browse files
Files changed (2) hide show
  1. Dockerfile +3 -3
  2. start.py +9 -2
Dockerfile CHANGED
@@ -45,12 +45,12 @@ ENV GRADIO_SERVER_NAME=0.0.0.0 \
45
  GRADIO_SERVER_PORT=7860 \
46
  USE_MOCK=1
47
 
48
- # Healthcheck points to Gradio app
49
  HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
50
  CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:7860', timeout=2)"
51
 
52
- # Hugging Face exposes 7860
53
  EXPOSE 7860
54
 
55
- # Run both FastAPI (backend) and Gradio (frontend)
56
  CMD ["python", "start.py"]
 
45
  GRADIO_SERVER_PORT=7860 \
46
  USE_MOCK=1
47
 
48
+ # Healthcheck ensures Gradio frontend is alive
49
  HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
50
  CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:7860', timeout=2)"
51
 
52
+ # Hugging Face Spaces expect public app on port 7860
53
  EXPOSE 7860
54
 
55
+ # Unified startup script
56
  CMD ["python", "start.py"]
start.py CHANGED
@@ -4,6 +4,7 @@ import os
4
 
5
 
6
  def run_fastapi():
 
7
  subprocess.run(
8
  [
9
  "uvicorn",
@@ -15,15 +16,21 @@ def run_fastapi():
15
  "--proxy-headers",
16
  "--workers",
17
  str(os.getenv("UVICORN_WORKERS", 1)),
18
- ]
 
19
  )
20
 
21
 
22
  def run_gradio():
23
- pass # noqa: just import to launch
 
 
24
 
25
 
26
  if __name__ == "__main__":
 
27
  t = threading.Thread(target=run_fastapi, daemon=True)
28
  t.start()
 
 
29
  run_gradio()
 
4
 
5
 
6
  def run_fastapi():
7
+ """Run FastAPI backend on port 8000."""
8
  subprocess.run(
9
  [
10
  "uvicorn",
 
16
  "--proxy-headers",
17
  "--workers",
18
  str(os.getenv("UVICORN_WORKERS", 1)),
19
+ ],
20
+ check=True,
21
  )
22
 
23
 
24
  def run_gradio():
25
+ """Launch Gradio UI (demo/app.py)."""
26
+ import demo.app # noqa: F401
27
+ # This module runs demo.launch() on import
28
 
29
 
30
  if __name__ == "__main__":
31
+ # Start FastAPI in background thread
32
  t = threading.Thread(target=run_fastapi, daemon=True)
33
  t.start()
34
+
35
+ # Run Gradio in foreground (keeps container alive)
36
  run_gradio()