File size: 5,142 Bytes
8653aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1803be
8653aff
 
 
 
 
 
 
f1803be
 
 
 
8653aff
f1803be
 
 
8653aff
 
 
 
 
 
 
29909d6
 
 
 
8653aff
 
 
 
 
5b1b3f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8653aff
 
5b1b3f2
d6d0769
 
 
 
 
 
 
 
5b1b3f2
 
8653aff
 
ed450db
 
 
 
 
 
 
 
 
 
8653aff
 
 
d6b1333
 
 
 
 
8653aff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# syntax=docker/dockerfile:1.6
#
# Cakra CCTV YOLO Analytics — single-stage image suited for both
# Hugging Face Spaces (sdk: docker, app_port: 7860) and a generic
# VPS. ffmpeg is the only external runtime dependency beyond the
# Python packages in requirements.txt.

FROM python:3.12-slim

# --- system deps -----------------------------------------------------
# - ffmpeg     : HLS demux on the input + libx264 encode on the output
# - libgl1     : opencv-python-headless still pulls libGL.so for some
#                ops on Debian slim, even though we use the headless
#                build
# - libglib2.0 : opencv + supervision transitive dep
# - curl       : healthcheck convenience
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ffmpeg \
        libgl1 \
        libglib2.0-0 \
        curl \
        ca-certificates \
        git \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# --- python deps -----------------------------------------------------
# Copy requirements first to maximise layer caching on code-only edits.
COPY requirements.txt ./
# Install CPU-only torch/torchvision first — the default PyPI wheels
# bundle CUDA (~700 MB extra) which a Hugging Face CPU space can't use
# anyway. Subsequent `pip install -r requirements.txt` sees torch>=2.2
# is already satisfied and skips the redownload.
RUN pip install --no-cache-dir --upgrade pip \
    && pip install --no-cache-dir \
        --index-url https://download.pytorch.org/whl/cpu \
        torch torchvision \
    && pip install --no-cache-dir -r requirements.txt

# --- app source ------------------------------------------------------
COPY app ./app
COPY scripts ./scripts
COPY docs ./docs
COPY run.sh ./
# Profile overlays — loaded by app/config.py when CAKRA_PROFILE matches
# the filename suffix. Without these the deploy would silently fall back
# to the bare defaults even if `CAKRA_PROFILE=vps` is set in the env.
COPY .env.hfspace .env.vps ./

# Ensure run.sh is executable (git mode bits are sometimes lost during
# Hugging Face Spaces sync).
RUN chmod +x run.sh

# Bake the detection model so the first /connect doesn't have to wait
# on a 70-450 MB download.
#
# Two backbone families are supported, picked via YOLO_BUILD_FAMILY:
#
#   yolov8   — standard YOLOv8 with 80 COCO classes. ~8x faster on CPU
#              than YOLO-World M at similar mAP for the COCO subset we
#              care about (car / truck / bus / motorcycle / bicycle /
#              person). Recommended for production. DEFAULT.
#
#   yoloworld — YOLO-World v2 with 17 Indonesian-traffic classes baked
#              in via CLIP text embeddings (becak, bemo, bajaj, angkot,
#              ojek included). Zero-shot capability but 5-10x heavier on
#              CPU — only worthwhile on a GPU host.
#
# YOLO_BUILD_SIZE picks the variant size inside the family
# (n/s/m/l/x for yolov8 — yolo-world has no n).
# `--build-arg YOLO_BUILD_SIZE=skip` skips the bake entirely if you
# prefer to mount a custom model at runtime.
ARG YOLO_BUILD_FAMILY=yolov8
ARG YOLO_BUILD_SIZE=m
RUN if [ "$YOLO_BUILD_SIZE" != "skip" ]; then \
        case "$YOLO_BUILD_FAMILY" in \
            yolov8) \
                PYTHONPATH=. python -m scripts.setup_yolov8 "$YOLO_BUILD_SIZE" ;; \
            yoloworld) \
                # CLIP is only resolved when the build actually needs it,
                # keeping the default yolov8 image leaner by ~50 MB +
                # avoiding a CDN hop at build time on non-WorldYOLO paths.
                pip install --no-cache-dir "clip @ git+https://github.com/ultralytics/CLIP.git" \
                && PYTHONPATH=. python -m scripts.setup_yolo_world "$YOLO_BUILD_SIZE" ;; \
            *) echo "Unknown YOLO_BUILD_FAMILY=$YOLO_BUILD_FAMILY (expected yolov8|yoloworld)"; exit 1 ;; \
        esac; \
    fi

# Also export the ONNX variant during build so HF Spaces / CPU VPS
# deploys can switch backend instantly via MODEL_PATH without paying
# the ~20s runtime export cost (or carrying onnxscript memory pressure
# at first request). YOLO_EXPORT_ONNX=skip disables this step.
ARG YOLO_EXPORT_ONNX=auto
ARG YOLO_EXPORT_IMGSZ=640
RUN if [ "$YOLO_BUILD_SIZE" != "skip" ] && [ "$YOLO_EXPORT_ONNX" != "skip" ]; then \
        PYTHONPATH=. python -m scripts.export_onnx --imgsz "$YOLO_EXPORT_IMGSZ"; \
    fi

# HLS segments are written here at runtime; ephemeral disk is fine.
RUN mkdir -p /tmp/cakra-hls

# Alerts artifact directory. VPS deploy mounts /opt/cakra/yolo/alerts
# over this path so JPEG snapshots + MP4 clips survive container
# recreate; HF Spaces / local just use the in-container tmpfs.
RUN mkdir -p /opt/cakra/yolo/alerts

# --- runtime ---------------------------------------------------------
# Hugging Face Spaces exposes ${PORT} (default 7860). On a generic VPS
# you can override the same variable.
ENV PORT=7860 \
    HOST=0.0.0.0 \
    HLS_OUTPUT_DIR=/tmp/cakra-hls \
    HLS_ENCODER=libx264 \
    PYTHONUNBUFFERED=1

EXPOSE 7860

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD curl -fs http://localhost:${PORT}/health || exit 1

CMD ["./run.sh"]