FROM nvidia/cuda:12.9.0-runtime-ubuntu24.04

ENV DEBIAN_FRONTEND=noninteractive

# System deps (libsndfile for soundfile audio decoding; ffmpeg for torchcodec)
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 \
    python3-pip \
    python3-dev \
    git \
    libsndfile1 \
    ffmpeg \
    && rm -rf /var/lib/apt/lists/*

RUN ln -sf /usr/bin/python3 /usr/bin/python
ENV PIP_BREAK_SYSTEM_PACKAGES=1
WORKDIR /app

# PyTorch ecosystem (cu128 wheels)
RUN pip install --no-cache-dir \
    torch==2.8.0 \
    torchaudio==2.8.0 \
    torchcodec==0.6.0 \
    --index-url https://download.pytorch.org/whl/cu128

# Pin transformers to the version the model's trust_remote_code was validated with
# (Qwen3 + Qwen3-Omni-MoE audio encoder). NOTE: do NOT loosen to >=4.57.1 — that
# pulls transformers 5.x, which changes the modeling/init API (e.g. tie_weights
# recompute_mapping) and breaks loading.
RUN pip install --no-cache-dir \
    "transformers==4.57.1" \
    evaluate \
    datasets \
    librosa \
    soundfile \
    jiwer \
    num2words \
    regex

# Force soundfile backend for datasets audio decoding (avoids torchcodec/FFmpeg issues)
ENV HF_AUDIO_DECODER_BACKEND=soundfile

RUN pip install --no-cache-dir --upgrade "kaldialign>=0.12.0" \
    && python -c "from kaldialign import batch_error_rate; print('kaldialign OK')"

# Copy the Space contents (run_eval.py, chat_template_default.py, normalizer/)
COPY . /app

ENTRYPOINT ["bash"]

# Keep-alive CMD so the Space runtime stays healthy. HF Jobs overrides this.
EXPOSE 7860
CMD ["-c", "python3 -m http.server 7860"]
