From bbfcdd5480db248c9e0176e3a4aa4f690a207425 Mon Sep 17 00:00:00 2001 From: Pierre De Lancre Date: Fri, 30 Oct 2026 15:33:42 +0300 Subject: [PATCH] =?UTF-8?q?ops:=20portable=20launcher=20=E2=80=94=20clone?= =?UTF-8?q?=20anywhere,=20./chamber=20up=20(cuda=20autodetect,=20cpu=20fal?= =?UTF-8?q?lback,=20site+relay=20one=20process)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chamber | 122 +++++++++++++++++++++++++++++++++++++++++++++++ chamber_local.py | 31 ++++++++++++ 2 files changed, 153 insertions(+) create mode 100755 chamber create mode 100644 chamber_local.py diff --git a/chamber b/chamber new file mode 100755 index 0000000..a317643 --- /dev/null +++ b/chamber @@ -0,0 +1,122 @@ +#!/usr/bin/env bash +# Portable ai-torture-chamber launcher: clone repo anywhere, `./chamber up`. +# Auto-detects CUDA (installs cu121 torch) vs CPU (cpu torch), creates .venv, +# starts chamber_local.py (relay at /chamber, static site at /), polls health, +# and falls back to CPU if the CUDA model load OOMs. +# +# Usage: +# ./chamber up [--cpu] [--port N] [--host H] +# ./chamber down +# ./chamber status +# ./chamber health +set -euo pipefail +cd "$(dirname "$0")" + +PORT="${CHAMBER_PORT:-8000}" +HOST="${CHAMBER_HOST:-127.0.0.1}" +DEVICE="auto" + +while [[ $# -gt 0 ]]; do + case "$1" in + --port) PORT="$2"; shift 2;; + --host) HOST="$2"; shift 2;; + --cpu) DEVICE="cpu"; shift;; + up|down|status|health) CMD="$1"; shift;; + *) echo "unknown arg: $1"; exit 1;; + esac +done +CMD="${CMD:-up}" + +PIDFILE=".chamber.pid" + +have_nvidia() { command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi -L >/dev/null 2>&1; } + +install_venv() { + if [[ ! -x .venv/bin/python ]]; then + python3 -m venv .venv + echo "[chamber] venv created" + fi + if have_nvidia && [[ "$DEVICE" != "cpu" ]]; then + echo "[chamber] NVIDIA detected -> installing CUDA torch (big download)" + .venv/bin/pip install --quiet "torch" --index-url https://download.pytorch.org/whl/cu121 + else + echo "[chamber] CPU mode -> installing CPU torch" + .venv/bin/pip install --quiet "torch" --index-url https://download.pytorch.org/whl/cpu + fi + # requirements.txt pins cpu-index torch on its own line; install the rest + grep -v '^torch' live/requirements.txt | .venv/bin/pip install --quiet -r /dev/stdin + echo "[chamber] deps installed" +} + +start_server() { + local device="$1" + : > .chamber.log + if [[ "$device" == "cuda" ]]; then + CHAMBER_DEVICE=cuda CHAMBER_DTYPE=float16 \ + CHAMBER_PORT="$PORT" CHAMBER_HOST="$HOST" \ + nohup .venv/bin/python chamber_local.py >> .chamber.log 2>&1 & + else + CHAMBER_DEVICE=cpu CHAMBER_DTYPE=float32 \ + CHAMBER_PORT="$PORT" CHAMBER_HOST="$HOST" \ + nohup .venv/bin/python chamber_local.py >> .chamber.log 2>&1 & + fi + echo $! > "$PIDFILE" +} + +wait_healthy() { + local tries="${1:-90}" + for _ in $(seq 1 "$tries"); do + if curl -sf --max-time 3 "http://$HOST:$PORT/chamber/health" >/dev/null 2>&1; then + return 0 + fi + sleep 2 + done + return 1 +} + +case "$CMD" in + up) + install_venv + if [[ "$DEVICE" != "cpu" ]] && have_nvidia; then + echo "[chamber] starting with CUDA..." + start_server cuda + if ! wait_healthy 60; then + echo "[chamber] CUDA start failed (OOM or driver) -> falling back to CPU" + kill "$(cat "$PIDFILE")" 2>/dev/null || true + sleep 2 + DEVICE="cpu" + start_server cpu + fi + else + echo "[chamber] starting CPU mode..." + start_server cpu + fi + if wait_healthy 150; then + echo "[chamber] UP -> http://$HOST:$PORT/ (relay at /chamber/)" + else + echo "[chamber] FAILED to become healthy. Last log lines:" + tail -20 .chamber.log + exit 1 + fi + ;; + down) + if [[ -f "$PIDFILE" ]]; then + kill "$(cat "$PIDFILE")" 2>/dev/null || true + rm -f "$PIDFILE" + echo "[chamber] stopped" + else + echo "[chamber] not running (no pidfile)" + fi + ;; + status) + if [[ -f "$PIDFILE" ]] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then + echo "running (pid $(cat "$PIDFILE"), port $PORT)" + else + echo "not running" + fi + ;; + health) + curl -s --max-time 5 "http://$HOST:$PORT/chamber/health" && echo + ;; + *) echo "usage: ./chamber {up|down|status|health} [--cpu] [--port N] [--host H]"; exit 1;; +esac diff --git a/chamber_local.py b/chamber_local.py new file mode 100644 index 0000000..5984448 --- /dev/null +++ b/chamber_local.py @@ -0,0 +1,31 @@ +"""Portable chamber launcher: original relay app at /chamber, static site at /. + +One process, one port — clone anywhere, `./chamber up`, get the full lab. +The upstream live/server.py stays unmodified; this only mounts it. +""" +import os +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent +sys.path.insert(0, str(ROOT / "live")) + +import server # noqa: E402 (loads the model at import — env vars go before this) +from fastapi import FastAPI # noqa: E402 +from fastapi.staticfiles import StaticFiles # noqa: E402 + +SITE = ROOT / "site" + +app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) +app.mount("/chamber", server.app) +if SITE.exists(): + app.mount("/", StaticFiles(directory=str(SITE), html=True), name="site") +else: + app.mount("/", server.app) # no site dir: relay everywhere + + +if __name__ == "__main__": + import uvicorn + + port = int(os.environ.get("CHAMBER_PORT", "8000")) + uvicorn.run(app, host=os.environ.get("CHAMBER_HOST", "127.0.0.1"), port=port)