# cuDNN variant of the base image: onnxruntime's CUDA execution provider needs
# cuDNN 9, which the plain `-runtime` tag does not ship.
FROM nvidia/cuda:12.9.0-cudnn-runtime-ubuntu24.04

# Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Install Python and system dependencies.
# Deliberately NO ffmpeg here, unlike the sibling images: `datasets==3.6.0`
# decodes audio through soundfile/libsndfile (the torchcodec `AudioDecoder`
# path only arrives in datasets 4.x), so ffmpeg is never on the audio path.
# Pulling it costs ~240 extra packages / 176 MB of downloads / 634 MB installed
# — by far the slowest step of this build. If you ever unpin datasets to 4.x,
# add `ffmpeg` back here and `torchcodec` below.
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 \
    python3-pip \
    python3-dev \
    git \
    libsndfile1 \
    && rm -rf /var/lib/apt/lists/*

# Set Python alias (Ubuntu 24.04 ships Python 3.12)
RUN ln -sf /usr/bin/python3 /usr/bin/python

# Allow pip to install packages system-wide in the container (PEP 668)
ENV PIP_BREAK_SYSTEM_PACKAGES=1

# Set working directory
WORKDIR /app

# PyTorch (cu128 wheels for CUDA 12.8+/12.9 compat). torch loads
# IndicConformer's TorchScript preprocessor (assets/preprocessor.ts); the rest
# of the model is ONNX. torchaudio/torchcodec are omitted — nothing in the
# model code or this eval path imports them.
#
# The CUDA build is required even though ONNX Runtime does the heavy lifting:
# the model picks its device with `torch.cuda.is_available()`, and on False it
# also drops onnxruntime to CPUExecutionProvider. A CPU-only torch would
# silently disable GPU inference.
RUN pip install --no-cache-dir \
    torch==2.8.0 \
    --index-url https://download.pytorch.org/whl/cu128

# Install common requirements (torch already installed above, pip will skip it)
RUN pip install --no-cache-dir \
    transformers==4.57.3 \
    huggingface_hub \
    evaluate \
    datasets==3.6.0 \
    librosa \
    jiwer \
    num2words \
    kaldialign \
    voi_oiwer

# onnxruntime-gpu runs the encoder / CTC / RNNT graphs.
#
# NOTE: the model card asks for `onnxruntime==1.20.1`, `onnx==1.20.1` and
# `onnxruntime-gpu==1.20.1`. None of those versions exist on PyPI (onnxruntime-gpu
# jumps 1.20.0 -> 1.20.2; onnx has no 1.20.x at all), so that line cannot be
# followed literally. We use 1.22.0, whose wheels are built for CUDA 12.8/cuDNN 9
# and so match this base image; ONNX Runtime reads older opsets fine, so the
# exported graphs are unaffected. If it misbehaves, the nearest-to-card fallback
# is `onnxruntime-gpu==1.20.2` (built against CUDA 12.4-12.6, relies on CUDA
# minor-version compatibility here).
# `onnx` itself is not needed at runtime — the model code imports only onnxruntime.
RUN pip install --no-cache-dir \
    onnxruntime-gpu==1.22.0 \
    soundfile

# datasets 3.6.0 already decodes via soundfile (and resamples with librosa, which
# is why librosa stays above); this is a no-op there, kept so the image still
# avoids the torchcodec/FFmpeg path if datasets is ever unpinned to 4.x.
ENV HF_AUDIO_DECODER_BACKEND=soundfile

# The model repo is gated and loads via trust_remote_code; the job must supply an
# HF_TOKEN whose account has accepted the model's terms. `from_pretrained` pulls
# the full ~2.5 GB snapshot (encoder + CTC/RNNT + all 22 per-language joint heads).

# Copy the full repository
COPY . /app

# Default entrypoint
ENTRYPOINT ["bash"]

# Keep-alive CMD so the Space runtime stays healthy. HF Jobs and `docker run`
# override this with their own command.
EXPOSE 7860
CMD ["-c", "python3 -m http.server 7860"]
