FROM pytorch/pytorch:1.10.0-cuda11.3-cudnn8-runtime as build

RUN apt-get update && apt-get install -y curl
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /usr/local/bin/rustup-init.sh
RUN chmod +x /usr/local/bin/rustup-init.sh
RUN /usr/local/bin/rustup-init.sh -y --default-toolchain none

ENV PATH="$PATH:/root/.cargo/bin"

RUN /root/.cargo/bin/rustup toolchain install stable --profile minimal --no-self-update

RUN /root/.cargo/bin/rustup --version
RUN /root/.cargo/bin/cargo --version

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential python3-venv && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN python3 -m venv /opt/myenv
ENV PATH="/opt/myenv/bin:$PATH"

WORKDIR /app
COPY . /app

RUN pip install --no-cache-dir Flask matplotlib scikit-learn transformers pandas sentence_transformers && \
    pip install --no-cache-dir --upgrade setuptools wheel && \
    rm -rf /root/.cache/pip

FROM pytorch/pytorch:1.10.0-cuda11.3-cudnn8-runtime

COPY --from=build /opt/myenv /opt/myenv

ENV PATH="/opt/myenv/bin:$PATH"

WORKDIR /app
COPY . /app

RUN chown -R www-data:www-data /app && chmod -R 755 /app

EXPOSE 5000

CMD ["/opt/myenv/bin/python", "/app/api.py"]