FROM python:3.11-slim

ENV PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    HF_HOME=/models \
    NLTK_DATA=/models/nltk_data \
    HF_HUB_DISABLE_TELEMETRY=1

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

WORKDIR /app
RUN mkdir -p /models/nltk_data

# Blackwell(sm_120, RTX 5050/5070Ti) 지원 torch cu128 휠
# (파일 의존성이 없어 constraints/requirements 수정 시에도 캐시 유지)
RUN pip install torch==2.11.0 torchaudio==2.11.0 \
        --index-url https://download.pytorch.org/whl/cu128

COPY constraints.txt requirements.txt ./

# 나머지 의존성 (정확한 버전 고정)
RUN pip install -c constraints.txt -r requirements.txt

# MeloTTS(순수 파이썬) 벤더링: 검증된 호스트 사본을 그대로 설치
# (PyPI 미배포 0.1.2 + gradio/tensorboard 등 불필요 의존성 회피)
COPY vendor/melo /usr/local/lib/python3.11/site-packages/melo

# 한국어/영어 모델 및 부가 데이터 사전 다운로드 (첫 요청 지연 제거)
# (app/frontend COPY 보다 앞: 앱 코드만 바뀌어도 재-워밍업하지 않도록)
COPY warmup.py ./
RUN python warmup.py

COPY app ./app
COPY frontend ./frontend

# 기본 MeloTTS 단일 엔진 (자동 언어 감지: 한/영 혼합)
EXPOSE 8788
CMD ["uvicorn", "app.server:app", "--host", "0.0.0.0", "--port", "8788"]
