Ace-119 commited on
Commit
90d9cac
Β·
1 Parent(s): 492f6b1

Fix HF deployment: Dockerfile + supervisor config + README metadata

Browse files
Files changed (2) hide show
  1. Dockerfile +16 -22
  2. supervisord.conf +9 -21
Dockerfile CHANGED
@@ -1,9 +1,9 @@
1
  # ── Base image ──────────────────────────────────────────────────────────────
2
- # Python 3.11 slim keeps the image lean while matching the dev environment.
3
- RUN useradd -m -u 1000 user
4
- USER user
5
  FROM python:3.11-slim
6
 
 
 
 
7
  # ── System dependencies ──────────────────────────────────────────────────────
8
  RUN apt-get update && apt-get install -y --no-install-recommends \
9
  build-essential \
@@ -15,42 +15,36 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
15
  WORKDIR /app
16
 
17
  # ── Python dependencies ──────────────────────────────────────────────────────
18
- # Copy requirements first so Docker caches this layer unless deps change.
19
  COPY requirements.txt .
20
  RUN pip install --no-cache-dir --upgrade pip \
21
- && pip install --no-cache-dir -r requirements.txt
 
22
 
23
  # ── Application code ─────────────────────────────────────────────────────────
24
  COPY . .
25
 
26
- # ── Download model checkpoint from HuggingFace Hub ───────────────────────────
27
- # Runs at build time so the container starts instantly (no cold-download delay).
28
- # If HF_TOKEN is set as a build secret the download works for private repos too.
29
  RUN python scripts/download_model.py
30
 
 
 
 
 
 
 
31
  # ── Environment ──────────────────────────────────────────────────────────────
32
- # Tell the Streamlit UI where to find the FastAPI backend (same container).
33
  ENV API_URL=http://localhost:8000
34
- # Streamlit must listen on 0.0.0.0:7860 for Spaces to route traffic correctly.
35
  ENV STREAMLIT_SERVER_PORT=7860
36
  ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0
37
- # Disable Streamlit's browser-open behaviour (no browser inside a container).
38
  ENV STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
39
- # Keep Python output unbuffered so logs appear in real time.
40
  ENV PYTHONUNBUFFERED=1
41
- # Use /app/data as the SQLite database location (writable inside the container).
42
  ENV STRESS_DB_PATH=/app/stress_detection.db
43
 
44
- # ── Expose Streamlit port ────────────────────────────────────────────────────
45
- # HuggingFace Spaces routes all external traffic to port 7860.
46
  EXPOSE 7860
47
 
48
- # ── Process manager ──────────────────────────────────────────────────────────
49
- # supervisord runs FastAPI (port 8000) and Streamlit (port 7860) as two
50
- # supervised processes inside one container β€” the standard pattern for
51
- # HuggingFace Spaces Docker deployments that need a backend + frontend.
52
- RUN pip install --no-cache-dir supervisor
53
-
54
  COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
55
 
56
- CMD ["/usr/local/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
 
 
1
  # ── Base image ──────────────────────────────────────────────────────────────
 
 
 
2
  FROM python:3.11-slim
3
 
4
+ # ── Create non-root user (HF best practice) ─────────────────────────────────
5
+ RUN useradd -m -u 1000 user
6
+
7
  # ── System dependencies ──────────────────────────────────────────────────────
8
  RUN apt-get update && apt-get install -y --no-install-recommends \
9
  build-essential \
 
15
  WORKDIR /app
16
 
17
  # ── Python dependencies ──────────────────────────────────────────────────────
 
18
  COPY requirements.txt .
19
  RUN pip install --no-cache-dir --upgrade pip \
20
+ && pip install --no-cache-dir -r requirements.txt \
21
+ && pip install --no-cache-dir supervisor
22
 
23
  # ── Application code ─────────────────────────────────────────────────────────
24
  COPY . .
25
 
26
+ # ── Download model checkpoint ────────────────────────────────────────────────
 
 
27
  RUN python scripts/download_model.py
28
 
29
+ # ── Permissions (IMPORTANT for HF) ───────────────────────────────────────────
30
+ RUN chown -R user:user /app
31
+
32
+ # Switch to non-root AFTER setup
33
+ USER user
34
+
35
  # ── Environment ──────────────────────────────────────────────────────────────
 
36
  ENV API_URL=http://localhost:8000
 
37
  ENV STREAMLIT_SERVER_PORT=7860
38
  ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0
 
39
  ENV STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
 
40
  ENV PYTHONUNBUFFERED=1
 
41
  ENV STRESS_DB_PATH=/app/stress_detection.db
42
 
43
+ # ── Expose port ──────────────────────────────────────────────────────────────
 
44
  EXPOSE 7860
45
 
46
+ # ── Supervisor config ────────────────────────────────────────────────────────
 
 
 
 
 
47
  COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
48
 
49
+ # ── Start services ───────────────────────────────────────────────────────────
50
+ CMD ["/usr/local/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
supervisord.conf CHANGED
@@ -1,11 +1,9 @@
1
  [supervisord]
2
  nodaemon=true
3
- logfile=/var/log/supervisord.log
4
- logfile_maxbytes=10MB
5
  loglevel=info
6
- pidfile=/var/run/supervisord.pid
7
 
8
- ; ── FastAPI backend ──────────────────────────────────────────────────────────
9
  [program:fastapi]
10
  command=uvicorn api.main:app --host 0.0.0.0 --port 8000 --workers 1 --log-level warning
11
  directory=/app
@@ -13,28 +11,18 @@ autostart=true
13
  autorestart=true
14
  startretries=3
15
  startsecs=5
16
- stdout_logfile=/var/log/fastapi.log
17
- stderr_logfile=/var/log/fastapi_err.log
18
- stdout_logfile_maxbytes=5MB
19
- stderr_logfile_maxbytes=5MB
20
  priority=1
21
 
22
- ; ── Streamlit UI ─────────────────────────────────────────────────────────────
23
- ; Starts 5 seconds after FastAPI so the /health endpoint is ready
24
- ; before the UI tries to call it.
25
  [program:streamlit]
26
- command=streamlit run ui/app.py
27
- --server.port=7860
28
- --server.address=0.0.0.0
29
- --server.headless=true
30
- --browser.gatherUsageStats=false
31
  directory=/app
32
  autostart=true
33
  autorestart=true
34
  startretries=3
35
  startsecs=5
36
- stdout_logfile=/var/log/streamlit.log
37
- stderr_logfile=/var/log/streamlit_err.log
38
- stdout_logfile_maxbytes=5MB
39
- stderr_logfile_maxbytes=5MB
40
- priority=2
 
1
  [supervisord]
2
  nodaemon=true
3
+ logfile=/dev/null
 
4
  loglevel=info
 
5
 
6
+ ; ── FastAPI backend ─────────────────────────────────────────
7
  [program:fastapi]
8
  command=uvicorn api.main:app --host 0.0.0.0 --port 8000 --workers 1 --log-level warning
9
  directory=/app
 
11
  autorestart=true
12
  startretries=3
13
  startsecs=5
14
+ stdout_logfile=/dev/stdout
15
+ stderr_logfile=/dev/stderr
 
 
16
  priority=1
17
 
18
+ ; ── Streamlit UI ────────────────────────────────────────────
 
 
19
  [program:streamlit]
20
+ command=streamlit run ui/app.py --server.port=7860 --server.address=0.0.0.0 --server.headless=true --browser.gatherUsageStats=false
 
 
 
 
21
  directory=/app
22
  autostart=true
23
  autorestart=true
24
  startretries=3
25
  startsecs=5
26
+ stdout_logfile=/dev/stdout
27
+ stderr_logfile=/dev/stderr
28
+ priority=2