# Stage-1 image for the Ming-3.0-tts-tiny leaderboard backend (ming-tts-tiny/run_eval.py).
# SEPARATE from the 0.5B image (bezzam/evals-ming-tts): tiny needs cu118 / torch 2.7.1 and the
# ming_tiny runtime (requirements-tiny.txt), not cu128 / torch 2.8 / ming_tts.
#
# DRAFT — not yet built/pushed. Base image, torchcodec pin and the build smoke test need a real
# build on the GPU host before the first job.
FROM nvidia/cuda:11.8.0-runtime-ubuntu22.04

ENV DEBIAN_FRONTEND=noninteractive

# Python 3.10 (Ubuntu 22.04 default; requirements-tiny.txt targets 3.10.15). libsndfile1 for
# soundfile; ffmpeg for torchaudio/torchcodec audio I/O; gcc for Triton's launcher.
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 python3-pip python3-dev gcc libc6-dev libsndfile1 ffmpeg git \
    && rm -rf /var/lib/apt/lists/*
RUN ln -sf /usr/bin/python3 /usr/bin/python
ENV PIP_BREAK_SYSTEM_PACKAGES=1
WORKDIR /app

# Ming-3.0-tts inference code, pinned (provides ming_tiny). The tiny checkpoint has no standalone
# inference code on the Hub. c88c023 = latest main at 2026-11-19 (first commit shipping ming_tiny +
# requirements-tiny.txt + infer_tiny.py).
ARG MING_COMMIT=c88c023ea6ccc08fd1e7a1d289cb6246c21deff1
RUN git clone https://github.com/inclusionAI/Ming-3.0-tts.git /opt/Ming-3.0-tts && \
    git -C /opt/Ming-3.0-tts checkout ${MING_COMMIT}

# Tiny runtime pins (requirements-tiny.txt): torch/torchaudio 2.7.1+cu118, transformers 4.46.3,
# accelerate 1.14.0, x-transformers 2.7.4, fla-core 0.4.2, triton 3.3.1, einx, etc. pynini (FST
# text normalization) is intentionally skipped — run_eval.py passes text_normalization=False.
RUN pip install --no-cache-dir -r /opt/Ming-3.0-tts/requirements-tiny.txt

# Leaderboard extras on top of the model pins (not in requirements-tiny.txt): datasets reads the
# prompt_audio column (soundfile is pinned by the reqs; datasets 3.6.0 also needs librosa to decode
# the Audio feature), tqdm for the loop.
# datasets==3.6.0 keeps huggingface_hub <1.0 (newer datasets pulls hub 2.x, which breaks
# transformers 4.46.3's "huggingface-hub<1.0" check); huggingface_hub==0.36.2 and numpy==1.26.4 are
# re-asserted so nothing in this layer bumps them past the requirements-tiny.txt pins.
RUN pip install --no-cache-dir "datasets==3.6.0" tqdm librosa "numpy==1.26.4" "huggingface_hub==0.36.2"
# Guard: the whole import stack (incl. datasets Audio decoding via soundfile/librosa) must work with
# the pinned hub/numpy — the two failure modes that broke the first two builds.
RUN python -c "import transformers, huggingface_hub, soundfile, librosa, numpy; from datasets.features.audio import Audio; print('transformers', transformers.__version__, 'hub', huggingface_hub.__version__, 'numpy', numpy.__version__)"

ENV PYTHONPATH=/opt/Ming-3.0-tts

# Build-time smoke test: a broken dependency fails the BUILD, not a GPU job.
RUN cd /opt/Ming-3.0-tts && python -c "import ming_tiny; from ming_tiny.api import MingTinyTTS; print('ming_tiny OK')"

# Weights are pulled from the Hub (inclusionAI/Ming-3.0-tts-tiny, ~16 GB of shards) into the HF
# cache at first run — NOT baked in.

COPY . /app
ENTRYPOINT ["bash"]
EXPOSE 7860
CMD ["-c", "python3 -m http.server 7860"]
