FROM nvidia/cuda:12.4.1-cudnn-devel-ubuntu22.04

RUN apt-get update && \
    apt-get install -y python3 python3-pip python3-dev && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install PyTorch with CUDA 12.4 wheels (cu121 index tops out at torch 2.5.1;
# torch>=2.6 requires cu124 or higher).
RUN pip3 install "torch>=2.6" --index-url https://download.pytorch.org/whl/cu124

COPY ./requirements.txt /app
RUN pip3 install --no-cache-dir -r requirements.txt

# Build flash-attn from source against the installed torch/CUDA versions.
# --no-build-isolation uses the torch already in the image (not an isolated env).
# MAX_JOBS limits parallel compilation to avoid OOM during build.
RUN MAX_JOBS=4 pip3 install flash-attn --no-build-isolation

COPY . /app

CMD ["gunicorn", "--bind", "0.0.0.0:80", "--timeout", "120", "--workers", "1", "emb_bge_flask:app"]
