{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Baetes visualizer - Colab GPU backend\n", "\n", "Runs the FastAPI backend on Colab's GPU and exposes it on a **fixed** URL,\n", "so the frontend `.env` never needs editing between sessions.\n", "\n", "**Run the cells top to bottom.** After a session is reclaimed, run them all\n", "again; model weights are cached on Drive so only the first run downloads them.\n", "\n", "### One-time setup\n", "\n", "1. Runtime -> Change runtime type -> **T4 GPU** (or better).\n", "2. Add a Colab Secret named `GH_TOKEN` (key icon in the left sidebar) holding a\n", " GitHub personal access token with `repo` scope. Never paste a token into a\n", " cell: anything typed into a notebook is saved inside it and travels with\n", " every copy, share and screenshot of that file.\n", "3. That is all — the repo and branch are already set in cell 2.\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# 1. Drive and model caches\n", "#\n", "# HF_HOME lives on Drive because SDXL and its depth ControlNet are ~7 GB.\n", "# Colab's local cache is wiped when a session is reclaimed, so without this\n", "# every reconnect re-downloads all of it before the first refinement can run.\n", "from google.colab import drive\n", "drive.mount('/content/drive')\n", "\n", "import os, pathlib\n", "CACHE = pathlib.Path('/content/drive/MyDrive/baetes-cache')\n", "(CACHE / 'hf').mkdir(parents=True, exist_ok=True)\n", "os.environ['HF_HOME'] = str(CACHE / 'hf')\n", "os.environ['HF_HUB_ENABLE_HF_TRANSFER'] = '1'\n", "print('HF_HOME =', os.environ['HF_HOME'])\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# 2. Clone or update the repo\n", "#\n", "# The token is read from Colab Secrets and used only to build the remote URL.\n", "# It is never written to disk, and output is scrubbed in case git echoes the\n", "# remote back inside an error message.\n", "import subprocess, pathlib\n", "from google.colab import userdata\n", "\n", "REPO_SLUG = 'MOD-Ventures-Org/room-editor'\n", "BRANCH = 'feat/diffusion-refinement'\n", "REPO_DIR = pathlib.Path('/content/room-editor')\n", "\n", "token = userdata.get('GH_TOKEN')\n", "assert token, 'Add a Colab Secret named GH_TOKEN first (key icon, left sidebar).'\n", "remote = 'https://' + token + '@github.com/' + REPO_SLUG + '.git'\n", "\n", "def run(*args, cwd=REPO_DIR):\n", " result = subprocess.run(args, cwd=cwd, capture_output=True, text=True)\n", " print((result.stdout + result.stderr).replace(token, '***'))\n", "\n", "if not REPO_DIR.exists():\n", " run('git', 'clone', '--branch', BRANCH, remote, str(REPO_DIR), cwd='/content')\n", "else:\n", " run('git', 'remote', 'set-url', 'origin', remote)\n", " run('git', 'fetch', 'origin', BRANCH)\n", " run('git', 'checkout', BRANCH)\n", " run('git', 'reset', '--hard', 'origin/' + BRANCH)\n", "\n", "run('git', 'log', '--oneline', '-3')\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# 3. Dependencies\n", "#\n", "# Several minutes on a cold session. requirements-gpu-cu126.txt pulls in\n", "# diffusers and accelerate, which the refinement stage needs.\n", "!pip install -q -r /content/room-editor/backend/floor-visualizer/requirements-gpu-cu126.txt\n", "!pip install -q hf_transfer\n", "print('dependencies installed')\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# 4. Sanity check: GPU, imports, and the refinement modules\n", "import subprocess, sys\n", "BACKEND = '/content/room-editor/backend/floor-visualizer'\n", "\n", "import torch\n", "print('CUDA :', torch.cuda.is_available())\n", "if torch.cuda.is_available():\n", " print('GPU :', torch.cuda.get_device_name(0))\n", " print('VRAM : %.1f GB' % (torch.cuda.get_device_properties(0).total_memory / 1024**3))\n", "\n", "import diffusers\n", "print('diffusers :', diffusers.__version__)\n", "\n", "# The CPU half of the refinement stage, verified without touching the GPU.\n", "result = subprocess.run([sys.executable, 'scripts/test_refine.py'],\n", " cwd=BACKEND, capture_output=True, text=True)\n", "print(result.stdout[-600:])\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# 5. Start the backend\n", "#\n", "# As a subprocess, not in-kernel: an in-kernel server keeps stale modules\n", "# loaded after a git pull, which silently serves yesterday's code.\n", "import os, subprocess, time, pathlib\n", "\n", "BACKEND = '/content/room-editor/backend/floor-visualizer'\n", "LOG = pathlib.Path('/content/backend.log')\n", "\n", "subprocess.run(['pkill', '-f', 'uvicorn app:app'], capture_output=True)\n", "time.sleep(2)\n", "\n", "env = dict(os.environ)\n", "env['VISUALIZER_CONFIG'] = 'visualizer.gpu.toml'\n", "env['PYTHONUNBUFFERED'] = '1'\n", "\n", "with LOG.open('wb') as log:\n", " backend = subprocess.Popen(\n", " ['uvicorn', 'app:app', '--host', '0.0.0.0', '--port', '8002'],\n", " cwd=BACKEND, env=env, stdout=log, stderr=subprocess.STDOUT)\n", "\n", "print('backend pid', backend.pid, '- warm start takes about 60s')\n", "for _ in range(90):\n", " time.sleep(2)\n", " if 'Application startup complete' in LOG.read_text(errors='ignore'):\n", " print('backend ready')\n", " break\n", "else:\n", " print('did not report ready within 3 minutes')\n", "print(LOG.read_text(errors='ignore')[-1500:])\n" ] }, { "cell_type": "code", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "# 6. Expose it on a fixed URL\n", "#\n", "# --subdomain keeps the URL stable across sessions, so the frontend .env stays\n", "# at VITE_API_URL=https://baetes-wall-viz.loca.lt and never needs editing.\n", "# First visit in a browser asks for a password: it is your public IP, printed\n", "# at the bottom of this cell.\n", "import subprocess, time, pathlib\n", "\n", "SUBDOMAIN = 'baetes-wall-viz'\n", "TUNNEL_LOG = pathlib.Path('/content/tunnel.log')\n", "\n", "subprocess.run(['pkill', '-f', 'localtunnel'], capture_output=True)\n", "!npm install -q -g localtunnel\n", "\n", "with TUNNEL_LOG.open('wb') as log:\n", " subprocess.Popen(['lt', '--port', '8002', '--subdomain', SUBDOMAIN],\n", " stdout=log, stderr=subprocess.STDOUT)\n", "time.sleep(8)\n", "print(TUNNEL_LOG.read_text(errors='ignore'))\n", "\n", "# Confirms the tunnel reaches the backend and that refinement is available.\n", "!curl -s -H 'bypass-tunnel-reminder: 1' https://$SUBDOMAIN.loca.lt/viz2d/refine/materials\n", "print()\n", "print('Tunnel password (your public IP):')\n", "!curl -s https://loca.lt/mytunnelpassword\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Watching the logs\n", "\n", "```python\n", "!tail -100 /content/backend.log\n", "```\n", "\n", "Lines worth looking for: `[REFINE]` for each pass and its timing, `[VRAM ...]`\n", "for memory, `[WALL_COMPONENTS]` for segmentation.\n", "\n", "### If a refinement pass hits CUDA out of memory\n", "\n", "Edit `visualizer.gpu.toml` under `[refine]`, then re-run cell 5:\n", "\n", "- `cpu_offload = true` - roughly halves peak VRAM and roughly doubles pass\n", " time. Try this first: it costs time, not quality.\n", "- `max_dimension = 768` - costs quality, so prefer offload.\n", "\n", "### End-to-end check without a browser\n", "\n", "```python\n", "!cd /content/room-editor/backend/floor-visualizer && python scripts/refine_smoke_test.py\n", "```\n" ] } ], "metadata": { "accelerator": "GPU", "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }