FROM python:3.10-slim

# System deps
RUN apt-get update && apt-get install -y --no-install-recommends \
    ffmpeg git build-essential \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user (HF Spaces requirement)
RUN useradd -m -u 1000 user
WORKDIR /home/user/app

# Install build deps first (setuptools needed to build numpy/pkuseg from source)
RUN pip install --no-cache-dir "setuptools<82" wheel
RUN pip install --no-cache-dir "numpy<1.26,>=1.24"

# Install CPU-only torch (no CUDA bloat)
RUN pip install --no-cache-dir torch torchaudio --index-url https://download.pytorch.org/whl/cpu

# Install providers separately (chatterbox has strict numpy pin)
RUN pip install --no-cache-dir chatterbox-tts
RUN pip install --no-cache-dir qwen-tts

# Install validation deps
RUN pip install --no-cache-dir faster-whisper resemblyzer scikit-learn librosa num2words word2number joblib

# Install gradio
RUN pip install --no-cache-dir "gradio[oauth]==6.9.0" "uvicorn>=0.14.0" spaces

# Copy only the app entry point
COPY app.py .

# Install rho-tts last so any Space commit busts this cache layer,
# ensuring the latest PyPI version is always fetched on rebuild.
RUN pip install --no-cache-dir --no-deps rho-tts

# Switch to non-root user
USER user

ENV GRADIO_SERVER_NAME=0.0.0.0
ENV GRADIO_SERVER_PORT=7860
EXPOSE 7860

CMD ["python", "-m", "rho_tts.ui"]
