FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04

ENV DEBIAN_FRONTEND=noninteractive \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONUNBUFFERED=1 \
    VENV_PATH=/opt/venv

ENV PATH="${VENV_PATH}/bin:${PATH}"

WORKDIR /piper

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      build-essential \
      espeak-ng \
      git \
      ninja-build \
      python3 \
      python3-dev \
      python3-venv \
 && rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 https://github.com/OHF-voice/piper1-gpl.git .

RUN python3 -m venv ${VENV_PATH}

RUN pip install --upgrade pip wheel setuptools \
 && pip install --no-cache-dir numpy cython cmake scikit-build onnx onnxruntime-gpu

RUN echo "torch==2.4.*" > /tmp/constraints.txt \
 && echo "torchvision==0.19.*" >> /tmp/constraints.txt \
 && echo "torchaudio==2.4.*" >> /tmp/constraints.txt

RUN pip install --no-cache-dir torch==2.4.* torchvision==0.19.* torchaudio==2.4.* \
    --index-url https://download.pytorch.org/whl/cu121

RUN pip install --no-cache-dir -e '.[train]' -c /tmp/constraints.txt

RUN ./build_monotonic_align.sh \
 && python3 setup.py build_ext --inplace

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
