File size: 2,897 Bytes
5869d53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ─────────────────────────────────────────────────────────────────────────────
# Dockerfile for HuggingFace Spaces (Docker SDK)
#
# Hardware target: CPU Space (free tier) or GPU Space (T4-small recommended).
# Port 7860 is the HF Spaces standard.
#
# Build order:
#   1. System deps (libsndfile for soundfile, git for pip installs)
#   2. Python packages (cached layer)
#   3. App code (invalidates only on source changes)
#   4. Switch to non-root user (HF Spaces requirement)
# ─────────────────────────────────────────────────────────────────────────────

FROM python:3.11-slim

WORKDIR /app

# ── System dependencies ───────────────────────────────────────────────────── #
RUN apt-get update && apt-get install -y --no-install-recommends \
      libsndfile1 \
      libgomp1 \
      git \
    && rm -rf /var/lib/apt/lists/*

# ── Python dependencies (cached unless requirements.txt changes) ──────────── #
COPY requirements.txt .

# Install CPU-only torch first (saves ~1 GB over default CUDA wheel).
# Comment these two lines out and use the plain pip install if you have a GPU Space.
RUN pip install --no-cache-dir \
      torch==2.5.1+cpu torchaudio==2.5.1+cpu \
      --index-url https://download.pytorch.org/whl/cpu

RUN pip install --no-cache-dir -r requirements.txt

# ── Application code ─────────────────────────────────────────────────────── #
COPY . .

# ── Non-root user for HF Spaces compatibility ─────────────────────────────── #
RUN useradd -m -u 1000 appuser \
    && chown -R appuser:appuser /app
USER 1000

# ── Model cache lives in the user home so HF Spaces can persist it ────────── #
ENV HF_HOME=/home/appuser/.cache/huggingface
ENV PORT=7860

EXPOSE 7860

# ── Healthcheck ───────────────────────────────────────────────────────────── #
HEALTHCHECK --interval=60s --timeout=10s --start-period=300s \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/health')"

CMD ["python", "-m", "uvicorn", "app:app", \
     "--host", "0.0.0.0", \
     "--port", "7860", \
     "--log-level", "info", \
     "--ws-ping-interval", "30", \
     "--ws-ping-timeout", "60"]