# Dockerfile.soprano
# Soprano TTS Server running on NVIDIA CUDA (GTX 1660)
# Python: 3.11.14 | pip: 25.3
FROM nvidia/cuda:11.8.0-runtime-ubuntu22.04

# Set timezone non-interactively
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC

# Install Python 3.11.14 and dependencies
RUN apt-get update && apt-get install -y \
    software-properties-common \
    && add-apt-repository ppa:deadsnakes/ppa \
    && apt-get update && apt-get install -y \
    python3.11=3.11.14-* \
    python3.11-venv \
    python3.11-dev \
    python3-pip \
    git \
    wget \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Set Python 3.11 as default
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1 && \
    update-alternatives --install /usr/bin/python python /usr/bin/python3.11 1

# Create app directory
WORKDIR /app

# Copy soprano source and server
COPY soprano/ /app/soprano/
COPY soprano_server.py /app/
COPY requirements-soprano.txt /app/

# Install Python dependencies - pin pip to 25.3
RUN pip3 install --no-cache-dir --upgrade pip==25.3

# Install PyTorch with CUDA 11.8 support
RUN pip3 install --no-cache-dir \
    torch==2.7.1+cu118 \
    torchvision==0.22.1+cu118 \
    torchaudio==2.7.1+cu118 \
    --index-url https://download.pytorch.org/whl/cu118

# Install requirements
RUN pip3 install --no-cache-dir -r /app/requirements-soprano.txt

# Install soprano from source with lmdeploy
WORKDIR /app/soprano
RUN pip3 install --no-cache-dir -e '.[lmdeploy]'

WORKDIR /app

# Expose ZMQ port (internal)
EXPOSE 5555

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD python3 -c "import zmq; ctx = zmq.Context(); s = ctx.socket(zmq.REQ); s.connect('tcp://localhost:5555'); s.close()" || exit 1

# Run soprano server
CMD ["python3", "soprano_server.py"]
