# =============================================================================
# Dockerfile – DemIA Hugging Face Speech Recognition Starter
# =============================================================================
# Multi-stage build:
#   base    – Python + system audio dependencies
#   deps    – Python packages
#   runtime – final lean image
#
# Build:
#   docker build -t demia-asr .
#
# Run API:
#   docker run --rm -p 8000:8000 demia-asr api
#
# Run Gradio UI:
#   docker run --rm -p 7860:7860 demia-asr ui
#
# Transcribe a file mounted into the container:
#   docker run --rm -v $(pwd)/audio:/audio demia-asr \
#       transcribe /audio/speech.wav
#
# Override the model:
#   docker run --rm -e HF_MODEL_ID=openai/whisper-small -p 8000:8000 demia-asr api
# =============================================================================

# ---------------------------------------------------------------------------
# Stage 1 – base system
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS base

# Avoid interactive prompts during apt-get.
ENV DEBIAN_FRONTEND=noninteractive

# Install system-level audio dependencies required by librosa / soundfile.
RUN apt-get update && apt-get install -y --no-install-recommends \
        ffmpeg \
        libsndfile1 \
        libgomp1 \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user for security.
RUN useradd --create-home --shell /bin/bash appuser

WORKDIR /app

# ---------------------------------------------------------------------------
# Stage 2 – Python dependencies
# ---------------------------------------------------------------------------
FROM base AS deps

# Copy only requirements first to leverage layer caching.
COPY requirements.txt .

# Install CPU-only PyTorch to keep the image smaller.
# For GPU support, override with:
#   --build-arg TORCH_INDEX=https://download.pytorch.org/whl/cu121
ARG TORCH_INDEX=https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir torch torchaudio --index-url "${TORCH_INDEX}" \
    && pip install --no-cache-dir -r requirements.txt

# ---------------------------------------------------------------------------
# Stage 3 – runtime image
# ---------------------------------------------------------------------------
FROM base AS runtime

# Copy installed packages from the deps stage.
COPY --from=deps /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=deps /usr/local/bin /usr/local/bin

# Copy application source.
COPY --chown=appuser:appuser . /app

# Switch to non-root user.
USER appuser

# ---------------------------------------------------------------------------
# Environment defaults (overridable at `docker run` time)
# ---------------------------------------------------------------------------
ENV HF_MODEL_ID=openai/whisper-tiny \
    HF_DEVICE=cpu \
    HF_LANGUAGE="" \
    HF_ASR_TASK=transcribe \
    # Store model weights inside the image's writable layer (or mount a volume).
    HF_HOME=/app/.cache/huggingface \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Create cache directory (user-writable).
RUN mkdir -p /app/.cache/huggingface

# Expose API port and Gradio port.
EXPOSE 8000 7860

# ---------------------------------------------------------------------------
# Entrypoint
# ---------------------------------------------------------------------------
# Default: start the FastAPI server.
# Override CMD to run other sub-commands:
#   docker run demia-asr transcribe /audio/speech.wav
#   docker run demia-asr ui
ENTRYPOINT ["python", "scripts/run.py"]
CMD ["api"]
