FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime

ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV HF_HOME=/app/models
ENV MODELSCOPE_CACHE=/app/models/modelscope

WORKDIR /opt/CosyVoice

RUN apt-get update -y && apt-get install -y \
    git \
    unzip \
    git-lfs \
    g++ \
    ffmpeg \
    libsndfile1 \
    sox \
    libsox-dev \
    && rm -rf /var/lib/apt/lists/*

RUN git lfs install

# 升级 pip
RUN pip3 install --no-cache-dir --upgrade "pip<24"

# 关键：写一个 constraints 文件，约束 build 时用老版 setuptools
RUN echo "setuptools<70" > /tmp/build-constraints.txt && \
    echo "wheel" >> /tmp/build-constraints.txt
ENV PIP_CONSTRAINT=/tmp/build-constraints.txt

RUN git clone --recursive https://github.com/FunAudioLLM/CosyVoice.git

# 删 tensorrt（1080 Ti 不支持）+ 装依赖
RUN cd CosyVoice && \
    sed -i '/tensorrt-cu12/d' requirements.txt && \
    pip3 install --no-cache-dir -r requirements.txt

# 装 API 框架
RUN pip3 install --no-cache-dir \
    "fastapi" \
    "uvicorn[standard]" \
    "python-multipart"

# 关键：卸载 deepspeed，避免 transformers 自动 import 时报 nvcc not found
# CosyVoice 推理不需要 deepspeed
RUN pip3 uninstall -y deepspeed


# 取消约束（避免影响运行时）
ENV PIP_CONSTRAINT=

ENV PYTHONPATH=/opt/CosyVoice/CosyVoice:/opt/CosyVoice/CosyVoice/third_party/Matcha-TTS

WORKDIR /app

COPY app/ /app/

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]