livetranslator / Dockerfile
mohrashid's picture
Update Dockerfile
aeff0cd verified
# Multi-stage build for Hugging Face Spaces (Docker)
# --- Frontend build stage ---
FROM node:22-alpine AS frontend
WORKDIR /frontend
COPY package.json package-lock.json* pnpm-lock.yaml* yarn.lock* ./
# Prefer npm; if lockfile missing, npm will still install using package.json
RUN npm ci || npm install
COPY . .
RUN npm run build
# --- Backend runtime stage ---
FROM python:3.11-slim AS runtime
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
HF_HOME=/root/.cache/huggingface
WORKDIR /app
# System deps (git and curl handy for troubleshooting; remove if you want smaller image)
RUN apt-get update && apt-get install -y --no-install-recommends \
git curl ffmpeg && \
rm -rf /var/lib/apt/lists/*
# Install Python deps
COPY server/requirements.txt ./server/requirements.txt
RUN python -m pip install --upgrade pip && \
pip install --no-cache-dir -r server/requirements.txt
# Copy backend and built frontend
COPY server ./server
COPY --from=frontend /frontend/dist ./dist
# Preload all direct MarianMT models for instant demo reliability
RUN python3 -c "from transformers import pipeline; models=['Helsinki-NLP/opus-mt-en-hi','Helsinki-NLP/opus-mt-hi-en','Helsinki-NLP/opus-mt-en-zh','Helsinki-NLP/opus-mt-zh-en','Helsinki-NLP/opus-mt-en-id','Helsinki-NLP/opus-mt-id-en','Helsinki-NLP/opus-mt-en-ar','Helsinki-NLP/opus-mt-ar-en','Helsinki-NLP/opus-mt-en-ur','Helsinki-NLP/opus-mt-ur-en']; [pipeline('translation', model=m, device=-1) for m in models]"
# Optional: install STT dependencies (Whisper) to enable /transcribe when STT_ENABLED=1
RUN pip install --no-cache-dir -r server/requirements-stt.txt || true
# Default port for HF Spaces is provided via PORT env
ENV PORT=7860
EXPOSE 7860
# Use 0.0.0.0 to be externally reachable in container
CMD ["/bin/sh", "-lc", "python -m uvicorn server.app:app --host 0.0.0.0 --port ${PORT:-7860}"]