shaml / Dockerfile
ahad-m
Pre-download Diac model into image cache for HF Spaces
c562596
# Multi-stage Dockerfile for Hugging Face Spaces (with model pre-caching)
# =============================================================================
# Stage 1: Frontend build (Node + Vite)
# =============================================================================
FROM node:20-alpine AS frontend-build
WORKDIR /frontend
COPY frontend/package.json frontend/package-lock.json* ./
RUN npm ci
COPY frontend/ ./
ARG VITE_API_BASE_URL=/api
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
RUN npm run build
# =============================================================================
# Stage 2: Python backend (FastAPI + static files + pre-cached models)
# =============================================================================
FROM python:3.12-slim-bookworm
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONPATH=/app \
HF_HOME=/app/.cache/huggingface \
TRANSFORMERS_CACHE=/app/.cache/huggingface \
HF_HUB_CACHE=/app/.cache/huggingface/hub \
PIP_NO_CACHE_DIR=1
WORKDIR /app
# System dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
build-essential \
ffmpeg \
libsndfile1 \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies
COPY backend/requirements.txt ./backend/requirements.txt
RUN pip install --no-cache-dir -r backend/requirements.txt
# Copy backend code
COPY backend ./backend
COPY app ./app
COPY constants ./constants
# Pre-download the Diac model into the image cache (avoids first-boot download issues on HF)
RUN mkdir -p /app/.cache/huggingface/hub && \
python -c "from huggingface_hub import snapshot_download; \
snapshot_download('rufaelfekadu/diac-transformer-text-asr-tashkeela-clartts', \
cache_dir='/app/.cache/huggingface/hub')"
# Copy built frontend from Stage 1
COPY --from=frontend-build /frontend/dist ./static
# Make cache writable
RUN chmod -R 777 /app/.cache
EXPOSE 7860
CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "7860"]