Melika Kheirieh commited on
Commit
4287d48
·
1 Parent(s): cdd9515

fix(docker): run FastAPI (app.main) + Gradio (demo.app) for HF deployment

Browse files
Files changed (2) hide show
  1. Dockerfile +10 -25
  2. demo/__init__.py +0 -0
Dockerfile CHANGED
@@ -15,7 +15,7 @@ COPY requirements.txt .
15
  RUN pip install --upgrade pip && \
16
  pip wheel --wheel-dir /wheels -r requirements.txt
17
 
18
- # ---------- Stage 2: Runtime image ----------
19
  FROM python:3.12-slim AS runtime
20
 
21
  ENV PIP_NO_CACHE_DIR=1 \
@@ -25,47 +25,32 @@ ENV PIP_NO_CACHE_DIR=1 \
25
 
26
  WORKDIR /app
27
 
28
- # HTTPS certs for outbound calls
29
  RUN apt-get update && apt-get install -y --no-install-recommends \
30
- ca-certificates \
31
- && rm -rf /var/lib/apt/lists/*
32
 
33
- # Create non-root user
34
  RUN useradd -m appuser
35
 
36
- # Install deps from wheels
37
  COPY --from=builder /wheels /wheels
38
  COPY requirements.txt .
39
  RUN pip install --no-cache-dir --find-links=/wheels -r requirements.txt && \
40
  rm -rf /wheels
41
 
42
- # App code
43
  COPY . .
44
 
45
- # Permissions
46
  RUN chown -R appuser:appuser /app
47
 
48
- # ---- HF expects the *public* web app on port 7860 ----
 
49
  ENV GRADIO_SERVER_NAME=0.0.0.0 \
50
  GRADIO_SERVER_PORT=7860 \
51
  USE_MOCK=1
52
 
53
- USER appuser
54
-
55
- # Healthcheck on Gradio UI port
56
  HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
57
- CMD python -c "import urllib.request, sys; \
58
- import urllib.error; \
59
- import time; \
60
- url='http://127.0.0.1:7860/'; \
61
- import urllib.request as u; \
62
- sys.exit(0) if u.urlopen(url, timeout=2).getcode() == 200 else sys.exit(1)"
63
-
64
- # Expose HF-facing port (Gradio)
65
  EXPOSE 7860
66
 
67
- # Run FastAPI on 8000 (internal) AND Gradio on 7860 (public)
68
- CMD ["sh", "-c", "\
69
- uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --workers ${UVICORN_WORKERS:-1} & \
70
- python -m demo.app \
71
- "]
 
15
  RUN pip install --upgrade pip && \
16
  pip wheel --wheel-dir /wheels -r requirements.txt
17
 
18
+ # ---------- Stage 2: Runtime ----------
19
  FROM python:3.12-slim AS runtime
20
 
21
  ENV PIP_NO_CACHE_DIR=1 \
 
25
 
26
  WORKDIR /app
27
 
 
28
  RUN apt-get update && apt-get install -y --no-install-recommends \
29
+ ca-certificates && rm -rf /var/lib/apt/lists/*
 
30
 
 
31
  RUN useradd -m appuser
32
 
 
33
  COPY --from=builder /wheels /wheels
34
  COPY requirements.txt .
35
  RUN pip install --no-cache-dir --find-links=/wheels -r requirements.txt && \
36
  rm -rf /wheels
37
 
 
38
  COPY . .
39
 
 
40
  RUN chown -R appuser:appuser /app
41
 
42
+ USER appuser
43
+
44
  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 ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --workers ${UVICORN_WORKERS:-1} & python -m demo.app"]
 
 
 
demo/__init__.py ADDED
File without changes