# GPU-enabled variant of Dockerfile, used only by docker-compose.gpu.yml to override
# the `worker` service on a machine with an NVIDIA GPU. Everything else (backend,
# frontend, postgres, redis) stays on the regular CPU image from Dockerfile — only the
# worker (which is what actually runs fine-tuning jobs) needs CUDA.
#
# Base image ships a torch build already matched to its bundled CUDA/cuDNN version —
# deliberately not installing torch ourselves (see requirements-cuda.txt) to avoid
# breaking that match. Bump this tag if a newer CUDA toolkit is needed for a newer GPU
# generation than whatever this was pinned against.
FROM pytorch/pytorch:2.6.0-cuda12.4-cudnn9-runtime

WORKDIR /app

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    && rm -rf /var/lib/apt/lists/*

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

# GGUF conversion (used by every fine-tuning backend, not just CUDA's) needs its own
# isolated venv — llama.cpp's converter pins an older numpy/transformers that would
# otherwise conflict with unsloth/transformers's needs in the main environment above.
# Same reasoning, same fix, as the native macOS .venv-gguf setup documented in the
# README — just baked into the image build here instead of a manual one-time step.
RUN git clone --depth 1 https://github.com/ggml-org/llama.cpp vendor-llama-cpp-convert && \
    python3 -m venv .venv-gguf && \
    .venv-gguf/bin/pip install --no-cache-dir -r vendor-llama-cpp-convert/requirements/requirements-convert_hf_to_gguf.txt

COPY . .
RUN mkdir -p data/uploads data/backups data/finetuning logs

COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh /app/app/services/finetune_scripts/*.py

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["celery", "-A", "app.core.celery_app", "worker", "--loglevel=info"]
