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

# Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Install Python and system dependencies. libsndfile1 for soundfile (wav writing); ffmpeg for
# torchaudio/librosa audio I/O; gcc for Triton, which compiles its CUDA launcher with the system C
# compiler at first use. (Same base layers as bezzam/evals-lfm3-audio.)
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 \
    python3-pip \
    python3-dev \
    gcc \
    libc6-dev \
    libsndfile1 \
    ffmpeg \
    && 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
ENV PIP_BREAK_SYSTEM_PACKAGES=1

WORKDIR /app

# Install PyTorch (cu128 wheels for CUDA 12.8+/12.9 compat).
RUN pip install --no-cache-dir \
    torch==2.8.0 \
    torchaudio==2.8.0 \
    --index-url https://download.pytorch.org/whl/cu128

# Ming-3.0-tts inference code (the Hub checkpoint has no standalone inference code), pinned. Its
# 0.5B runtime pins (requirements.txt): transformers 4.46.3, accelerate 1.3.0, diffusers 0.33.0,
# x_transformers 2.9.2. pynini (text normalization) is skipped: the eval synthesizes the raw text,
# as the published benchmarks did. flash-attn is optional (falls back to SDPA).
ARG MING_COMMIT=9332fb9443325d4726ce02cefd4ec1c211df4079
RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/* && \
    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}
RUN pip install --no-cache-dir \
    "transformers==4.46.3" \
    tokenizers \
    "accelerate==1.3.0" \
    "diffusers==0.33.0" \
    "x_transformers==2.9.2" \
    onnxruntime \
    inflect \
    einops \
    loguru \
    packaging \
    datasets \
    soundfile \
    tqdm
# torchcodec 0.7 matches torch 2.8; `datasets` uses it to decode the prompt_audio column.
RUN pip install --no-cache-dir "torchcodec==0.7.0"
ENV PYTHONPATH=/opt/Ming-3.0-tts

# Build-time smoke test, so a broken dependency fails the BUILD rather than a GPU job.
RUN cd /opt/Ming-3.0-tts && python -c "from ming_tts import MingTTS; print('ming_tts OK')"

# Weights are NOT baked in: they are pulled from the Hub (inclusionAI/Ming-3.0-tts-0.5B, ungated)
# into the HF cache at first run.

# Copy the full repository
COPY . /app

# Default entrypoint
ENTRYPOINT ["bash"]

# Keep-alive CMD so the Space runtime stays healthy; `docker run` overrides it.
EXPOSE 7860
CMD ["-c", "python3 -m http.server 7860"]
