# =============================================================================
# Dockerfile – Hugging Face Pipeline Starter
# =============================================================================
# Multi-stage build:
#   Stage 1 (base)  – system packages + Python deps
#   Stage 2 (app)   – application code only
#
# Build:
#   docker build -t hf-pipeline-starter .
#
# Run API:
#   docker run -p 8000:8000 hf-pipeline-starter
#
# Run with a different model:
#   docker run -p 8000:8000 -e HF_MODEL_ID=facebook/bart-large-cnn \
#              -e HF_TASK=summarization hf-pipeline-starter
#
# Mount a custom cache dir to avoid re-downloading weights each run:
#   docker run -p 8000:8000 \
#     -v $(pwd)/model_cache:/root/.cache/huggingface \
#     hf-pipeline-starter
# =============================================================================

# ---------------------------------------------------------------------------
# Stage 1: dependencies
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS base

# System packages needed for some audio/vision models.
# Uncomment the lines you need for your chosen model family.
RUN apt-get update && apt-get install -y --no-install-recommends \
        git \
        curl \
    # ffmpeg   \   # required for audio tasks (automatic-speech-recognition)
    # libsndfile1 \  # required by soundfile / librosa
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python deps before copying code so Docker cache is reused when
# only source files change.
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# ---------------------------------------------------------------------------
# Stage 2: application
# ---------------------------------------------------------------------------
FROM base AS app

WORKDIR /app

# Copy project source
COPY config/ config/
COPY src/    src/
COPY api/    api/
COPY app/    app/
COPY scripts/ scripts/
COPY examples/ examples/

# Environment defaults – override at runtime via -e flags or docker compose.
ENV HF_MODEL_ID=""
ENV HF_TASK=""
ENV HF_DEVICE="cpu"
ENV HF_HOME="/root/.cache/huggingface"
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1

# Expose both API and Gradio UI ports.
EXPOSE 8000 7860

# Default command: start the FastAPI service.
# Override with `docker run … python scripts/run.py --ui` for the Gradio UI.
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
