# dots.tts OpenAI-compatible TTS server (thin FastAPI over DotsTtsRuntime).
# GPU access is via `runtime: nvidia` at run time (torch ships its own CUDA
# runtime; no CUDA toolkit / nvcc needed to build — the model uses no custom
# compiled kernels, confirmed on the irv-ml1 venv).
FROM python:3.11-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
        libsndfile1 ffmpeg git curl ca-certificates \
    && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir uv

WORKDIR /app

# Pin torch + deps to dots.tts upstream recommended constraints (the same set the
# irv-ml1 venv installed against). ADD caches on the URL contents.
ADD https://raw.githubusercontent.com/rednote-hilab/dots.tts/main/constraints/recommended.txt /tmp/rec.txt
RUN uv pip install --system -c /tmp/rec.txt \
        dots.tts soundfile fastapi "uvicorn[standard]"

# C compiler for the RUNTIME (not build): optimize=True drives torch.compile /
# inductor / triton, which JIT-compile kernels via gcc on model load. Without it
# the runtime dies with "Failed to find C compiler". Placed after the pip layer
# so it doesn't invalidate the expensive torch install cache.
RUN apt-get update && apt-get install -y --no-install-recommends build-essential \
    && rm -rf /var/lib/apt/lists/*

COPY app.py /app/app.py

# Persist the inductor compile cache on the mounted (rw) HF cache so kernel
# JIT doesn't re-run on every container restart (~70s warmup otherwise).
ENV HF_HOME=/hf_cache DOTS_PORT=8198 CC=gcc CXX=g++ TORCHINDUCTOR_CACHE_DIR=/hf_cache/inductor
EXPOSE 8198
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8198"]
