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

# Prevent interactive prompts during install
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    ffmpeg \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python dependencies (PyTorch version, with pip cache for faster rebuilds)
COPY requirements.txt /app/
RUN --mount=type=cache,target=/root/.cache/pip \
    pip3 install -r requirements.txt

# Clone TransNetV2 repo for the PyTorch model code only (no LFS needed)
RUN GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/soCzech/TransNetV2.git /app/TransNetV2

# Download weights from HuggingFace (more reliable than GitHub LFS)
RUN python3 -c "from huggingface_hub import hf_hub_download; hf_hub_download('Sn4kehead/TransNetV2', 'transnetv2-pytorch-weights.pth', local_dir='/app/weights')"

COPY app.py /app/

# Default environment variables
ENV TRANSNET_THRESHOLD=0.5
ENV TRANSNET_MIN_SCENE_LENGTH=1.0
ENV TRANSNET_WEIGHTS=/app/weights/transnetv2-pytorch-weights.pth
ENV PYTHONPATH=/app/TransNetV2/inference-pytorch

EXPOSE 5005

HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:5005/health || exit 1

CMD ["python3", "app.py"]
