File size: 8,677 Bytes
73828b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
{
 "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
}