# Qwen3.8-27B-UD-Q4_K_M.gguf served through llama.cpp (llama-cpp-python).
#
# llama-cpp-python ships as source only: pip compiles llama.cpp during install, and
# it gets a CUDA backend ONLY if nvcc is on the image and -DGGML_CUDA=on is set.
# A plain python base image therefore produces a CPU-only build that accepts
# LLM_GPU_LAYERS and then ignores it - the card sits idle. Hence the CUDA devel base.
#
# CMAKE_CUDA_ARCHITECTURES=86 targets the RTX 3090/3080 (compute capability 8.6).
# Add more as needed, e.g. "86;89" to also cover the RTX 4090.
# Expect a long build: compiling the CUDA kernels takes tens of minutes.
#
# For a deliberately CPU-only image, use "FROM python:3.12" and drop CMAKE_ARGS.
#
# The 16.5 GB GGUF is not baked into the image. The service downloads
# LLM_MODEL_FILE from LLM_MODEL on first boot, so mount a Hub cache
# (-e HF_HOME=/models -v ./models:/models) to keep it across container recreation.

FROM nvidia/cuda:12.6.3-devel-ubuntu24.04

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        python3 python3-venv python3-dev build-essential cmake git \
    && rm -rf /var/lib/apt/lists/*

# Ubuntu marks its system Python as externally managed, so build inside a venv.
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

WORKDIR /app

COPY ./requirements.txt /app
ENV CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=86"
ENV FORCE_CMAKE=1
RUN pip install --no-cache-dir --upgrade -r requirements.txt

COPY . /app

# Needed when the container is started without --gpus; compose also reserves the device.
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility

# Loading a 27B quant takes a while: keep the worker count at 1 and the timeout high.
CMD ["gunicorn", "--bind", "0.0.0.0:80", "--timeout", "600", "--workers", "1", "llm_qwen3_8_quant_flask:app"]
