# -runtime is enough: voxcpm ships pure-Python + prebuilt wheels (no CUDA extensions to compile).
FROM nvidia/cuda:12.9.0-runtime-ubuntu24.04

# Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# System deps: ffmpeg shared libs are required by torchcodec (a voxcpm dependency);
# libsndfile1 for soundfile I/O; git for pip VCS installs if any.
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 \
    python3-pip \
    python3-dev \
    git \
    ffmpeg \
    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

WORKDIR /app

# Upgrade pip so it prefers prebuilt manylinux wheels. --ignore-installed is required on
# Ubuntu 24.04: the Debian-installed pip has no RECORD file and cannot be uninstalled.
RUN pip install --no-cache-dir --upgrade --ignore-installed pip setuptools wheel

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

# torchcodec is pinned to the release paired with torch 2.8 (torchcodec pins exact torch
# minors); installing it first stops `pip install voxcpm` from pulling a mismatched one.
RUN pip install --no-cache-dir "torchcodec==0.7.*"

# voxcpm (VoxCPM/VoxCPM2 inference package; brings transformers, datasets, einops, wetext,
# modelscope, funasr, librosa, soundfile). This container does TTS generation ONLY — ASR/WER
# scoring runs in the separate `tts-eval` image.
RUN pip install --no-cache-dir voxcpm

# tqdm for the eval loop progress bar (datasets + soundfile come in via voxcpm deps).
RUN pip install --no-cache-dir datasets tqdm soundfile

# Bake the VoxCPM2 weights (~2B params, ungated Apache-2.0, no HF token needed) into the
# image so the eval runs offline. run_eval.py loads from this dir (--checkpoint_path).
RUN python3 -c "from huggingface_hub import snapshot_download; snapshot_download('openbmb/VoxCPM2', local_dir='/opt/models/VoxCPM2')"

# 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 (e.g. run_eval.sh).
EXPOSE 7860
CMD ["-c", "python3 -m http.server 7860"]
