FROM python:3.11-slim

RUN apt-get update && apt-get install -y \
    libsndfile1 \
    ffmpeg \
    git \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Pre-download the SpeechBrain model at build time
RUN python -c "\
from speechbrain.inference.interfaces import foreign_class; \
foreign_class( \
    source='speechbrain/emotion-recognition-wav2vec2-IEMOCAP', \
    pymodule_file='custom_interface.py', \
    classname='CustomEncoderWav2vec2Classifier', \
    savedir='/app/models', \
    run_opts={'device': 'cpu'}, \
); \
print('Model downloaded successfully.')"

COPY . .

EXPOSE 8001

HEALTHCHECK --interval=30s --timeout=10s --start-period=90s \
    CMD python -c "import httpx; httpx.get('http://localhost:8001/health').raise_for_status()"

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8001", "--workers", "1"]
