vipan-kumar's picture
Add Hugging Face Spaces deployment: Dockerfile + entrypoint + README YAML
a48b1ab
Raw
History Blame Contribute Delete
2.7 kB
# =====================================================================
# Combined Dockerfile for Hugging Face Spaces (Docker space)
# β€’ Builds the React frontend (Vite, npm)
# β€’ Copies the dist/ into backend/app/static/
# β€’ Mounts it under FastAPI so a single container serves both
# β€’ Listens on port 7860 (HF Spaces requirement)
#
# To deploy:
# 1. Create a "Docker" Space at https://huggingface.co/new-space
# 2. Push this repo to that Space's git remote (gh auth + git push)
# 3. The Space builds and runs this image automatically
# =====================================================================
# ───── Stage 1: build the frontend ───────────────────────────────────
FROM node:20-alpine AS frontend
WORKDIR /fe
COPY frontend/package.json frontend/package-lock.json* ./
RUN npm install --no-audit --no-fund
COPY frontend ./
# Bake in the API base β€” same origin as the served frontend
ENV VITE_API_BASE_URL=""
ENV VITE_WS_BASE_URL=""
RUN npm run build
# Result: /fe/dist contains index.html + assets/
# ───── Stage 2: backend runtime ──────────────────────────────────────
FROM python:3.11-slim AS runtime
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
HF_HOME=/data/.cache/huggingface \
TRANSFORMERS_CACHE=/data/.cache/huggingface \
PORT=7860
WORKDIR /app
# Native deps for soundfile / ffmpeg
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential libsndfile1 ffmpeg curl \
&& rm -rf /var/lib/apt/lists/*
COPY backend/requirements.txt /app/requirements.txt
RUN pip install --upgrade pip && pip install -r /app/requirements.txt
COPY backend/app /app/app
COPY backend/checkpoints /app/checkpoints
COPY data /data_seed
# Copy the built frontend into a static/ folder the backend can serve
COPY --from=frontend /fe/dist /app/static
# HF Spaces requires writable /data
RUN mkdir -p /data/.cache/huggingface /app/tmp_uploads /data/sample_audios \
&& cp -rn /data_seed/sample_audios/* /data/sample_audios/ 2>/dev/null || true
EXPOSE 7860
# Healthcheck (HF Spaces uses this to gauge readiness)
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -fsS http://localhost:7860/api/health || exit 1
# Note: app.main:app is unmodified β€” we mount /app/static via a small
# inline shim that the entrypoint applies before Uvicorn boots.
COPY hf_spaces_entrypoint.py /app/hf_spaces_entrypoint.py
CMD ["python", "/app/hf_spaces_entrypoint.py"]