Spaces:
Runtime error
Runtime error
Commit ·
48df398
1
Parent(s): ed450db
fix(config): UI / shell env now beats profile overlay
Browse filesSnapshot os.environ before any .env file load and skip those keys in
both passes — so values set in Hugging Face Space "Variables & Secrets"
UI, in docker -e flags, or in the parent shell always win over the
profile file. Previously the profile overlay used a hard override and
clobbered the operator's UI overrides, which made MODEL_PATH and
TARGET_INFER_FPS untunable on a deployed Space.
Precedence is now (highest wins):
1. shell / docker -e / HF UI
2. .env.<profile> (profile overlay)
3. .env (base config + secrets)
4. hard-coded defaults in app/config.py
Also ship .env.vps as a reference profile for self-managed VPS deploys
(4-8 vCPU AMD/Intel without GPU): inference=768, fps_target=8,
max_streams=4, .onnx backend by default.
- .env.vps +62 -0
- app/config.py +30 -10
.env.vps
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ════════════════════════════════════════════════════════════════════════
|
| 2 |
+
# .env.vps — overlay profile for a self-managed VPS (no GPU)
|
| 3 |
+
# ════════════════════════════════════════════════════════════════════════
|
| 4 |
+
#
|
| 5 |
+
# Activation: set CAKRA_PROFILE=vps in the host .env (or shell), or pass
|
| 6 |
+
# docker run -e CAKRA_PROFILE=vps ...
|
| 7 |
+
#
|
| 8 |
+
# Sized for a typical CPU droplet (4-8 vCPU, 8-32 GB RAM, AVX2/AVX-512
|
| 9 |
+
# capable). Significantly more headroom than HF Spaces basic — uses the
|
| 10 |
+
# whole machine without melting it.
|
| 11 |
+
#
|
| 12 |
+
# Loading order (values here OVERRIDE matching keys in .env):
|
| 13 |
+
# 1. .env — secrets (DATABASE_URL) + base config.
|
| 14 |
+
# 2. .env.vps — this file, the resource tuning.
|
| 15 |
+
# 3. Shell env / docker -e — top priority for runtime overrides.
|
| 16 |
+
#
|
| 17 |
+
# Tune per machine:
|
| 18 |
+
# • 4 vCPU : keep TARGET_INFER_FPS=6, MAX_STREAMS=2
|
| 19 |
+
# • 8 vCPU : raise TARGET_INFER_FPS=10, MAX_STREAMS=4-5
|
| 20 |
+
# • 16 vCPU : MAX_STREAMS=8+ (memory is no longer the binding factor;
|
| 21 |
+
# watch CPU load average per stream)
|
| 22 |
+
# ════════════════════════════════════════════════════════════════════════
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ──── YOLO inference ────────────────────────────────────────────────────
|
| 26 |
+
# 768 is a comfortable middle ground for 8-core boxes: noticeably
|
| 27 |
+
# better small/far recall than 640, only ~30% slower. AMD EPYC + ONNX
|
| 28 |
+
# Runtime + AVX2 delivers ~15-18 fps raw inference at this size for
|
| 29 |
+
# YOLO-World M. Drop to 640 if you push MAX_STREAMS above 5.
|
| 30 |
+
INFERENCE_SIZE=768
|
| 31 |
+
|
| 32 |
+
# 8 fps with healthy headroom. Each stream worker pegs ~1.5 cores at
|
| 33 |
+
# this rate, leaving the remaining cores for ffmpeg encoders, the
|
| 34 |
+
# Socket.IO push loop, and the stream readers.
|
| 35 |
+
TARGET_INFER_FPS=8
|
| 36 |
+
|
| 37 |
+
# Process every captured frame.
|
| 38 |
+
FRAME_SKIP=1
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ──── Encoder ───────────────────────────────────────────────────────────
|
| 42 |
+
# 720p is the sweet spot on AMD EPYC: libx264 veryfast handles it at
|
| 43 |
+
# <15% CPU per stream. Drop to 854×480 only at MAX_STREAMS=8+.
|
| 44 |
+
OUTPUT_WIDTH=1280
|
| 45 |
+
OUTPUT_HEIGHT=720
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# ──── Pool ──────────────────────────────────────────────────────────────
|
| 49 |
+
# 4 concurrent streams is the comfort zone for an 8-core EPYC: each
|
| 50 |
+
# stream owns ~2 cores (1 inference + ~0.5 encoder + ~0.5 reader/WS),
|
| 51 |
+
# load average stays around 60-70%, fan profile and thermals quiet.
|
| 52 |
+
# Stretch to 5 if you trust the workload to be bursty (most streams
|
| 53 |
+
# idle most of the time); above 6, expect proc fps drop on every
|
| 54 |
+
# stream because cores are now time-sliced.
|
| 55 |
+
MAX_STREAMS=4
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# ──── Model backend ─────────────────────────────────────────────────────
|
| 59 |
+
# ONNX Runtime over the bundled .onnx — 3-5× faster than PyTorch on CPU.
|
| 60 |
+
# The Dockerfile bakes both .pt and .onnx at build time, so switching is
|
| 61 |
+
# zero-friction. Override to .pt for a PyTorch A/B benchmark.
|
| 62 |
+
MODEL_PATH=./models/yolo-world-id.onnx
|
app/config.py
CHANGED
|
@@ -8,24 +8,42 @@ from typing import Tuple
|
|
| 8 |
|
| 9 |
# Load .env + optional profile overlay (no python-dotenv hard dep).
|
| 10 |
#
|
| 11 |
-
#
|
| 12 |
-
# 1.
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
#
|
| 21 |
# Built-in profiles shipped in this repo:
|
| 22 |
# • .env.hfspace — Hugging Face Spaces free CPU tier
|
|
|
|
| 23 |
#
|
| 24 |
-
#
|
|
|
|
| 25 |
_REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def _load_env_file(path: Path, override: bool) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if not path.exists():
|
| 30 |
return
|
| 31 |
for line in path.read_text().splitlines():
|
|
@@ -35,6 +53,8 @@ def _load_env_file(path: Path, override: bool) -> None:
|
|
| 35 |
k, v = line.split("=", 1)
|
| 36 |
k = k.strip()
|
| 37 |
v = v.strip()
|
|
|
|
|
|
|
| 38 |
if override:
|
| 39 |
os.environ[k] = v
|
| 40 |
else:
|
|
|
|
| 8 |
|
| 9 |
# Load .env + optional profile overlay (no python-dotenv hard dep).
|
| 10 |
#
|
| 11 |
+
# Precedence (highest priority wins):
|
| 12 |
+
# 1. Shell / Docker -e / HF Space "Variables & Secrets" UI
|
| 13 |
+
# 2. Profile file (``.env.<profile>``)
|
| 14 |
+
# 3. ``.env`` base config
|
| 15 |
+
# 4. Hard-coded defaults in app/config.py
|
| 16 |
+
#
|
| 17 |
+
# To guarantee #1 beats everything, snapshot ``os.environ`` BEFORE any
|
| 18 |
+
# file load — keys already set there are then skipped by both passes.
|
| 19 |
+
#
|
| 20 |
+
# Profile activation:
|
| 21 |
+
# - Set ``CAKRA_PROFILE=<name>`` (in shell, .env, or platform UI) to
|
| 22 |
+
# load ``.env.<name>`` after .env.
|
| 23 |
+
# - HF Spaces deploys auto-apply ``hfspace`` via the SPACE_ID detection
|
| 24 |
+
# block below, so a Space needs zero extra config.
|
| 25 |
#
|
| 26 |
# Built-in profiles shipped in this repo:
|
| 27 |
# • .env.hfspace — Hugging Face Spaces free CPU tier
|
| 28 |
+
# • .env.vps — self-managed VPS (no GPU, 4-8 vCPU)
|
| 29 |
#
|
| 30 |
+
# Profile files only carry RESOURCE TUNING (inference size, fps target,
|
| 31 |
+
# pool size). Keep DATABASE_URL etc. in .env or platform secret UI.
|
| 32 |
_REPO_ROOT = Path(__file__).resolve().parents[1]
|
| 33 |
+
# Snapshot of env that was set externally (shell, docker -e, HF UI).
|
| 34 |
+
# Anything in this set is treated as load-bearing user intent — file
|
| 35 |
+
# overlays never overwrite it. NOTE: must be captured before any file
|
| 36 |
+
# load below.
|
| 37 |
+
_PRELOAD_ENV: set[str] = set(os.environ.keys())
|
| 38 |
|
| 39 |
|
| 40 |
def _load_env_file(path: Path, override: bool) -> None:
|
| 41 |
+
"""Load ``.env``-style key=value file.
|
| 42 |
+
|
| 43 |
+
``override=True`` lets the file overwrite values that came from
|
| 44 |
+
previously-loaded files, but NEVER overwrite values that were
|
| 45 |
+
present in the environment before any file was loaded (shell / UI
|
| 46 |
+
overrides always win)."""
|
| 47 |
if not path.exists():
|
| 48 |
return
|
| 49 |
for line in path.read_text().splitlines():
|
|
|
|
| 53 |
k, v = line.split("=", 1)
|
| 54 |
k = k.strip()
|
| 55 |
v = v.strip()
|
| 56 |
+
if k in _PRELOAD_ENV:
|
| 57 |
+
continue
|
| 58 |
if override:
|
| 59 |
os.environ[k] = v
|
| 60 |
else:
|