File size: 3,202 Bytes
088fe97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# ── Base image ────────────────────────────────────────────────────────────────
FROM python:3.11-slim

# ── System dependencies (ffmpeg required by Whisper) ─────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg \
    git \
    curl \
    build-essential \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# ── Create non-root user (HF Spaces requirement) ──────────────────────────────
RUN useradd -m -u 1000 appuser

# ── Working directory ─────────────────────────────────────────────────────────
WORKDIR /app

# ── Install Python dependencies first (Docker layer cache) ───────────────────
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# ── Copy application code ─────────────────────────────────────────────────────
COPY --chown=appuser:appuser . .

# ── Persistent storage dirs (mounted at /data by HF Spaces paid tier) ─────────
RUN mkdir -p /data/.huggingface /data/.cache/huggingface/hub /tmp/audio_cache && \
    chown -R appuser:appuser /data /tmp/audio_cache

# ── HF cache environment variables ───────────────────────────────────────────
# All model downloads go to /data so they survive Space restarts
ENV HF_HOME=/data/.huggingface
ENV HUGGINGFACE_HUB_CACHE=/data/.huggingface/hub
ENV TRANSFORMERS_CACHE=/data/.huggingface/hub
ENV XDG_CACHE_HOME=/data/.cache
# Whisper also respects this
ENV WHISPER_CACHE=/data/.huggingface/whisper

# ── App config ────────────────────────────────────────────────────────────────
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
# HF Spaces routes external traffic to port 7860
ENV PORT=7860

# ── Switch to non-root user ───────────────────────────────────────────────────
USER appuser

# ── Expose port ───────────────────────────────────────────────────────────────
EXPOSE 7860

# ── Start Flask via Gunicorn (production-grade WSGI server) ──────────────────
CMD ["gunicorn", \
     "--bind", "0.0.0.0:7860", \
     "--workers", "1", \
     "--threads", "4", \
     "--timeout", "120", \
     "--worker-class", "gthread", \
     "--log-level", "info", \
     "app:app"]