Fix support multi users
Browse files
app.py
CHANGED
|
@@ -26,7 +26,9 @@ import shutil
|
|
| 26 |
import socket
|
| 27 |
import sys
|
| 28 |
import tempfile
|
|
|
|
| 29 |
import time
|
|
|
|
| 30 |
from pathlib import Path
|
| 31 |
|
| 32 |
import numpy as np
|
|
@@ -115,9 +117,19 @@ PI3_MODEL_CPU = load_pi3_model(flavor="pi3x", device="cpu").eval()
|
|
| 115 |
print("[app] models ready (CPU)", flush=True)
|
| 116 |
|
| 117 |
# ---------------------------------------------------------------------------
|
| 118 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# ---------------------------------------------------------------------------
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
def _pick_free_port() -> int:
|
|
@@ -155,7 +167,7 @@ def reconstruct_gpu(img_dir: str, target_h: int, target_w: int, max_views: int)
|
|
| 155 |
ckpt_root=None, ckpt_name="", scene_data=scene_data, cmd_lst=[],
|
| 156 |
hf_variant=HF_VARIANT, model=DVSM_MODEL_CPU,
|
| 157 |
)
|
| 158 |
-
out_path = ARTIFACTS_DIR / f"{int(time.time() * 1000)}.recon.pt"
|
| 159 |
save_artifact(renderer, batch, ishape, c2w_r, c2w_t, intr, out_path,
|
| 160 |
hf_variant=HF_VARIANT)
|
| 161 |
return str(out_path)
|
|
@@ -172,14 +184,15 @@ class _SessionArgs:
|
|
| 172 |
|
| 173 |
|
| 174 |
@spaces.GPU(duration=SESSION_SECONDS)
|
| 175 |
-
def _live_session_worker(artifact_path: str
|
| 176 |
"""Generator running in the GPU worker. Builds the renderer, starts a viser
|
| 177 |
-
server on
|
|
|
|
| 178 |
|
| 179 |
Yields tagged tuples so the UI can show a prep progress bar then a live
|
| 180 |
countdown:
|
| 181 |
("prep", frac, desc) during setup
|
| 182 |
-
("ready", port, None) viser is up and serving
|
| 183 |
("alive", remaining, None) per-second heartbeat while holding the GPU
|
| 184 |
"""
|
| 185 |
import torch
|
|
@@ -209,7 +222,18 @@ def _live_session_worker(artifact_path: str, port: int):
|
|
| 209 |
)
|
| 210 |
|
| 211 |
yield ("prep", 0.6, "Loading scene…")
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
server.scene.set_up_direction("+y")
|
| 214 |
install_viser_handlers(server, renderer, scene_meta, pp_cpu, pp_scale,
|
| 215 |
model, _SessionArgs())
|
|
@@ -243,11 +267,13 @@ def _placeholder_html(msg: str = "Viser Viewer") -> str:
|
|
| 243 |
)
|
| 244 |
|
| 245 |
|
| 246 |
-
def _iframe_html(
|
|
|
|
|
|
|
| 247 |
# No own border — the gr.HTML(container=True) .block frames it.
|
| 248 |
return (
|
| 249 |
f"<div style='width:100%;aspect-ratio:16/9;border-radius:6px;overflow:hidden'>"
|
| 250 |
-
f"<iframe src='/viser-proxy/
|
| 251 |
f"allow='clipboard-write'></iframe></div>"
|
| 252 |
)
|
| 253 |
|
|
@@ -281,72 +307,92 @@ def _prep_overlay(frac: float, desc: str) -> str:
|
|
| 281 |
# ---------------------------------------------------------------------------
|
| 282 |
# Gradio callbacks (main process)
|
| 283 |
# ---------------------------------------------------------------------------
|
| 284 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
progress=gr.Progress(track_tqdm=False)):
|
| 286 |
"""CPU pre-processing (decode video / copy images / size) then one GPU
|
| 287 |
reconstruction call. Returns (artifact_path, viewer_html, start_btn_update).
|
| 288 |
The status overlays the viewer box (see _viewer_message)."""
|
|
|
|
|
|
|
| 289 |
if not input_files:
|
| 290 |
return "", _viewer_message("Upload a video or at least 3 images.", "#c66"), gr.update(interactive=False)
|
| 291 |
|
| 292 |
work = Path(tempfile.mkdtemp(prefix="dvsm_in_"))
|
| 293 |
img_dir = work / "images"
|
| 294 |
img_dir.mkdir()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
try:
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
except Exception as e:
|
| 306 |
-
return "", _viewer_message(f"
|
| 307 |
-
if n < 3:
|
| 308 |
-
return "", _viewer_message(f"Only {n} frames extracted; need ≥3.", "#c66"), gr.update(interactive=False)
|
| 309 |
-
n_inputs = n
|
| 310 |
-
elif len(images) >= 3:
|
| 311 |
-
if len(images) > n_frames:
|
| 312 |
-
idxs = np.linspace(0, len(images) - 1, n_frames).round().astype(int)
|
| 313 |
-
images = [images[i] for i in idxs]
|
| 314 |
-
progress(0.05, desc=f"Copying {len(images)} files...")
|
| 315 |
-
for src in images:
|
| 316 |
-
shutil.copy(src, img_dir / src.name)
|
| 317 |
-
n_inputs = len(images)
|
| 318 |
-
else:
|
| 319 |
-
return "", _viewer_message("Upload a video or at least 3 images.", "#c66"), gr.update(interactive=False)
|
| 320 |
-
|
| 321 |
-
# Snap target dims to the input aspect; slider is the long axis; multiples of 8.
|
| 322 |
-
try:
|
| 323 |
-
from PIL import Image as _PILImage
|
| 324 |
-
first_img = next(iter(sorted(img_dir.iterdir())))
|
| 325 |
-
with _PILImage.open(first_img) as im:
|
| 326 |
-
iw, ih = im.size
|
| 327 |
-
input_aspect = iw / ih
|
| 328 |
-
except Exception:
|
| 329 |
-
input_aspect = 16 / 9
|
| 330 |
-
long_axis = (int(target_w) // 8) * 8
|
| 331 |
-
if input_aspect >= 1.0:
|
| 332 |
-
target_w, target_h = long_axis, int(round(long_axis / input_aspect))
|
| 333 |
-
else:
|
| 334 |
-
target_h, target_w = long_axis, int(round(long_axis * input_aspect))
|
| 335 |
-
target_h = max(8, (target_h // 8) * 8)
|
| 336 |
-
target_w = max(8, (target_w // 8) * 8)
|
| 337 |
-
|
| 338 |
-
progress(0.2, desc=f"Running π³ + TTB-DVSM ({n_inputs} frames)...")
|
| 339 |
-
try:
|
| 340 |
-
artifact_path = reconstruct_gpu(str(img_dir), target_h, target_w, int(n_inputs))
|
| 341 |
-
except Exception as e:
|
| 342 |
-
return "", _viewer_message(f"Reconstruction failed: {e}", "#c66"), gr.update(interactive=False)
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
| 350 |
|
| 351 |
|
| 352 |
def _session_bar(remaining: int, total: int, expired: bool = False) -> str:
|
|
@@ -381,22 +427,23 @@ def live_session(artifact_path):
|
|
| 381 |
yield _viewer_message("Reconstruct a scene first.", "#c66"), gr.update()
|
| 382 |
return
|
| 383 |
|
| 384 |
-
|
| 385 |
-
_state["viser_port"] = port
|
| 386 |
-
_state["session_alive"] = True
|
| 387 |
try:
|
| 388 |
-
for tag, a, _b in _live_session_worker(artifact_path
|
| 389 |
if tag == "prep":
|
| 390 |
# a = fraction, _b = description -> prep bar overlaid in the viewer
|
| 391 |
yield (_prep_overlay(a, _b), gr.update())
|
| 392 |
elif tag == "ready":
|
| 393 |
-
# viser is up
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
| 395 |
else: # "alive": a = seconds remaining
|
| 396 |
yield (gr.update(), _session_bar(int(a), SESSION_SECONDS))
|
| 397 |
finally:
|
| 398 |
-
|
| 399 |
-
|
| 400 |
yield (gr.update(), _session_bar(0, SESSION_SECONDS, expired=True))
|
| 401 |
|
| 402 |
|
|
@@ -413,14 +460,10 @@ def _build_proxy_handlers():
|
|
| 413 |
|
| 414 |
http_client = httpx.AsyncClient(timeout=60.0)
|
| 415 |
|
| 416 |
-
def _port_or_none():
|
| 417 |
-
if not _state.get("session_alive"):
|
| 418 |
-
return None
|
| 419 |
-
return _state.get("viser_port")
|
| 420 |
-
|
| 421 |
async def proxy_http(request: Request) -> Response:
|
|
|
|
| 422 |
path = request.path_params.get("path", "")
|
| 423 |
-
port =
|
| 424 |
if port is None:
|
| 425 |
return Response(b"No active viser session.", status_code=503)
|
| 426 |
url = f"http://localhost:{port}/{path}"
|
|
@@ -440,8 +483,9 @@ def _build_proxy_handlers():
|
|
| 440 |
return Response(content=r.content, status_code=r.status_code, headers=resp_headers)
|
| 441 |
|
| 442 |
async def proxy_ws(websocket: WebSocket) -> None:
|
|
|
|
| 443 |
path = websocket.path_params.get("path", "")
|
| 444 |
-
port =
|
| 445 |
offered = list(websocket.scope.get("subprotocols") or [])
|
| 446 |
if port is None:
|
| 447 |
await websocket.close(code=1011)
|
|
@@ -544,8 +588,14 @@ input[type='range']::-moz-range-thumb { background: #76B900 !important; }
|
|
| 544 |
.col-right { flex: 3 1 0 !important; min-width: 360px; }
|
| 545 |
"""
|
| 546 |
|
| 547 |
-
|
| 548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 549 |
# Full-width description on top.
|
| 550 |
gr.HTML(
|
| 551 |
"<h1 style='margin-bottom:0.1em;color:#76B900'>TTB-DVSM</h1>"
|
|
@@ -624,7 +674,7 @@ with gr.Blocks(title="TTB-DVSM (ZeroGPU)") as demo:
|
|
| 624 |
|
| 625 |
# Recon status overlays the viewer box; the gradio progress bar (the "20%")
|
| 626 |
# therefore also shows over the viewer, aligned with the upload column.
|
| 627 |
-
recon_btn.click(reconstruct_cb, [inputs, n_frames, tw],
|
| 628 |
[artifact_state, viewer_html, session_btn])
|
| 629 |
# show_progress="hidden": the session generator streams for the whole GPU
|
| 630 |
# hold; without this Gradio overlays an auto progress bar on EACH output
|
|
@@ -664,10 +714,10 @@ def _attach_proxy_routes(app):
|
|
| 664 |
proxy_http, proxy_ws = _build_proxy_handlers()
|
| 665 |
methods = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"]
|
| 666 |
routes = [
|
| 667 |
-
Route("/viser-proxy", proxy_http, methods=methods),
|
| 668 |
-
Route("/viser-proxy/{path:path}", proxy_http, methods=methods),
|
| 669 |
-
WebSocketRoute("/viser-proxy", proxy_ws),
|
| 670 |
-
WebSocketRoute("/viser-proxy/{path:path}", proxy_ws),
|
| 671 |
]
|
| 672 |
app.router.routes[0:0] = routes # prepend → highest match priority
|
| 673 |
print("[app] viser-proxy routes prepended; ready", flush=True)
|
|
@@ -679,7 +729,11 @@ def launch_app():
|
|
| 679 |
`exec`s `python app.py` or `import`s the module and would otherwise call
|
| 680 |
demo.launch() itself. prevent_thread_lock=True lets us attach routes after
|
| 681 |
launch; we then block to keep the process alive."""
|
| 682 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
demo.launch(
|
| 684 |
server_name="0.0.0.0",
|
| 685 |
server_port=int(os.environ.get("PORT", "7860")),
|
|
@@ -695,7 +749,6 @@ def launch_app():
|
|
| 695 |
ssr_mode=False,
|
| 696 |
)
|
| 697 |
_attach_proxy_routes(demo.app)
|
| 698 |
-
import threading
|
| 699 |
threading.Event().wait() # keep the process alive
|
| 700 |
|
| 701 |
|
|
|
|
| 26 |
import socket
|
| 27 |
import sys
|
| 28 |
import tempfile
|
| 29 |
+
import threading
|
| 30 |
import time
|
| 31 |
+
import uuid
|
| 32 |
from pathlib import Path
|
| 33 |
|
| 34 |
import numpy as np
|
|
|
|
| 117 |
print("[app] models ready (CPU)", flush=True)
|
| 118 |
|
| 119 |
# ---------------------------------------------------------------------------
|
| 120 |
+
# Per-session viser registry (token -> worker viser port). The Gradio Space is
|
| 121 |
+
# a SINGLE shared process for ALL users — ZeroGPU only isolates GPU compute per
|
| 122 |
+
# @spaces.GPU call, NOT the app/server/state. So proxy routing MUST be keyed by
|
| 123 |
+
# a per-session token; a single global port would make every user's iframe
|
| 124 |
+
# point at whichever session set it last (they'd all collide on one viser).
|
| 125 |
# ---------------------------------------------------------------------------
|
| 126 |
+
_sessions: dict = {} # token -> viser port (present == alive)
|
| 127 |
+
_sessions_lock = threading.Lock()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def _session_port(token: str):
|
| 131 |
+
with _sessions_lock:
|
| 132 |
+
return _sessions.get(token)
|
| 133 |
|
| 134 |
|
| 135 |
def _pick_free_port() -> int:
|
|
|
|
| 167 |
ckpt_root=None, ckpt_name="", scene_data=scene_data, cmd_lst=[],
|
| 168 |
hf_variant=HF_VARIANT, model=DVSM_MODEL_CPU,
|
| 169 |
)
|
| 170 |
+
out_path = ARTIFACTS_DIR / f"{int(time.time() * 1000)}_{uuid.uuid4().hex[:8]}.recon.pt"
|
| 171 |
save_artifact(renderer, batch, ishape, c2w_r, c2w_t, intr, out_path,
|
| 172 |
hf_variant=HF_VARIANT)
|
| 173 |
return str(out_path)
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
@spaces.GPU(duration=SESSION_SECONDS)
|
| 187 |
+
def _live_session_worker(artifact_path: str):
|
| 188 |
"""Generator running in the GPU worker. Builds the renderer, starts a viser
|
| 189 |
+
server on a self-chosen free port, then holds the GPU until the session
|
| 190 |
+
budget expires.
|
| 191 |
|
| 192 |
Yields tagged tuples so the UI can show a prep progress bar then a live
|
| 193 |
countdown:
|
| 194 |
("prep", frac, desc) during setup
|
| 195 |
+
("ready", port, None) viser is up and serving on `port`
|
| 196 |
("alive", remaining, None) per-second heartbeat while holding the GPU
|
| 197 |
"""
|
| 198 |
import torch
|
|
|
|
| 222 |
)
|
| 223 |
|
| 224 |
yield ("prep", 0.6, "Loading scene…")
|
| 225 |
+
# Pick + bind the port HERE (inside the worker), with retry, so concurrent
|
| 226 |
+
# sessions can't race on the same port (no cross-process TOCTOU).
|
| 227 |
+
server = None
|
| 228 |
+
for _ in range(10):
|
| 229 |
+
port = _pick_free_port()
|
| 230 |
+
try:
|
| 231 |
+
server = viser.ViserServer(host="0.0.0.0", port=port)
|
| 232 |
+
break
|
| 233 |
+
except Exception:
|
| 234 |
+
server = None
|
| 235 |
+
if server is None:
|
| 236 |
+
raise RuntimeError("could not bind a free viser port")
|
| 237 |
server.scene.set_up_direction("+y")
|
| 238 |
install_viser_handlers(server, renderer, scene_meta, pp_cpu, pp_scale,
|
| 239 |
model, _SessionArgs())
|
|
|
|
| 267 |
)
|
| 268 |
|
| 269 |
|
| 270 |
+
def _iframe_html(token: str) -> str:
|
| 271 |
+
# Token in the PATH so every viser sub-request (assets + websocket) inherits
|
| 272 |
+
# it and the proxy can route this user's traffic to their own worker.
|
| 273 |
# No own border — the gr.HTML(container=True) .block frames it.
|
| 274 |
return (
|
| 275 |
f"<div style='width:100%;aspect-ratio:16/9;border-radius:6px;overflow:hidden'>"
|
| 276 |
+
f"<iframe src='/viser-proxy/{token}/' style='width:100%;height:100%;border:0' "
|
| 277 |
f"allow='clipboard-write'></iframe></div>"
|
| 278 |
)
|
| 279 |
|
|
|
|
| 307 |
# ---------------------------------------------------------------------------
|
| 308 |
# Gradio callbacks (main process)
|
| 309 |
# ---------------------------------------------------------------------------
|
| 310 |
+
def _delete_artifact(path):
|
| 311 |
+
"""Remove a user-generated recon artifact. Used both as the gr.State
|
| 312 |
+
delete_callback (fires when the session ends / TTL expires) and to drop the
|
| 313 |
+
previous artifact when a user reconstructs again. Guarded to ARTIFACTS_DIR so
|
| 314 |
+
it never touches the permanent examples/ files."""
|
| 315 |
+
try:
|
| 316 |
+
if path and isinstance(path, str):
|
| 317 |
+
p = Path(path)
|
| 318 |
+
if p.parent == ARTIFACTS_DIR and p.exists():
|
| 319 |
+
p.unlink()
|
| 320 |
+
except Exception:
|
| 321 |
+
pass
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
def reconstruct_cb(input_files, n_frames, target_w, prev_artifact,
|
| 325 |
progress=gr.Progress(track_tqdm=False)):
|
| 326 |
"""CPU pre-processing (decode video / copy images / size) then one GPU
|
| 327 |
reconstruction call. Returns (artifact_path, viewer_html, start_btn_update).
|
| 328 |
The status overlays the viewer box (see _viewer_message)."""
|
| 329 |
+
# Drop this session's previous artifact (if any) before making a new one.
|
| 330 |
+
_delete_artifact(prev_artifact)
|
| 331 |
if not input_files:
|
| 332 |
return "", _viewer_message("Upload a video or at least 3 images.", "#c66"), gr.update(interactive=False)
|
| 333 |
|
| 334 |
work = Path(tempfile.mkdtemp(prefix="dvsm_in_"))
|
| 335 |
img_dir = work / "images"
|
| 336 |
img_dir.mkdir()
|
| 337 |
+
# The uploaded/extracted input frames are only needed during reconstruction;
|
| 338 |
+
# delete the whole work dir on exit so /tmp doesn't accumulate per request.
|
| 339 |
+
try:
|
| 340 |
+
paths = [Path(f.name if hasattr(f, "name") else f) for f in input_files]
|
| 341 |
+
videos = [p for p in paths if p.suffix.lower() in VIDEO_EXTS]
|
| 342 |
+
images = [p for p in paths if p.suffix.lower() in IMAGE_EXTS]
|
| 343 |
+
n_frames = int(n_frames)
|
| 344 |
|
| 345 |
+
if videos:
|
| 346 |
+
progress(0.05, desc=f"Extracting {n_frames} frames from video...")
|
| 347 |
+
try:
|
| 348 |
+
n = extract_video_frames(videos[0], img_dir, n_frames)
|
| 349 |
+
except Exception as e:
|
| 350 |
+
return "", _viewer_message(f"Video decode failed: {e}", "#c66"), gr.update(interactive=False)
|
| 351 |
+
if n < 3:
|
| 352 |
+
return "", _viewer_message(f"Only {n} frames extracted; need ≥3.", "#c66"), gr.update(interactive=False)
|
| 353 |
+
n_inputs = n
|
| 354 |
+
elif len(images) >= 3:
|
| 355 |
+
if len(images) > n_frames:
|
| 356 |
+
idxs = np.linspace(0, len(images) - 1, n_frames).round().astype(int)
|
| 357 |
+
images = [images[i] for i in idxs]
|
| 358 |
+
progress(0.05, desc=f"Copying {len(images)} files...")
|
| 359 |
+
for src in images:
|
| 360 |
+
shutil.copy(src, img_dir / src.name)
|
| 361 |
+
n_inputs = len(images)
|
| 362 |
+
else:
|
| 363 |
+
return "", _viewer_message("Upload a video or at least 3 images.", "#c66"), gr.update(interactive=False)
|
| 364 |
+
|
| 365 |
+
# Snap target dims to the input aspect; slider is the long axis; multiples of 8.
|
| 366 |
try:
|
| 367 |
+
from PIL import Image as _PILImage
|
| 368 |
+
first_img = next(iter(sorted(img_dir.iterdir())))
|
| 369 |
+
with _PILImage.open(first_img) as im:
|
| 370 |
+
iw, ih = im.size
|
| 371 |
+
input_aspect = iw / ih
|
| 372 |
+
except Exception:
|
| 373 |
+
input_aspect = 16 / 9
|
| 374 |
+
long_axis = (int(target_w) // 8) * 8
|
| 375 |
+
if input_aspect >= 1.0:
|
| 376 |
+
target_w, target_h = long_axis, int(round(long_axis / input_aspect))
|
| 377 |
+
else:
|
| 378 |
+
target_h, target_w = long_axis, int(round(long_axis * input_aspect))
|
| 379 |
+
target_h = max(8, (target_h // 8) * 8)
|
| 380 |
+
target_w = max(8, (target_w // 8) * 8)
|
| 381 |
+
|
| 382 |
+
progress(0.2, desc=f"Running π³ + TTB-DVSM ({n_inputs} frames)...")
|
| 383 |
+
try:
|
| 384 |
+
artifact_path = reconstruct_gpu(str(img_dir), target_h, target_w, int(n_inputs))
|
| 385 |
except Exception as e:
|
| 386 |
+
return "", _viewer_message(f"Reconstruction failed: {e}", "#c66"), gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
+
return (
|
| 389 |
+
artifact_path,
|
| 390 |
+
_viewer_message(f"Reconstruction done ({n_inputs} views, {target_w}×{target_h}). "
|
| 391 |
+
f"Click “Start / Continue Live Session”."),
|
| 392 |
+
gr.update(interactive=True),
|
| 393 |
+
)
|
| 394 |
+
finally:
|
| 395 |
+
shutil.rmtree(work, ignore_errors=True)
|
| 396 |
|
| 397 |
|
| 398 |
def _session_bar(remaining: int, total: int, expired: bool = False) -> str:
|
|
|
|
| 427 |
yield _viewer_message("Reconstruct a scene first.", "#c66"), gr.update()
|
| 428 |
return
|
| 429 |
|
| 430 |
+
token = uuid.uuid4().hex
|
|
|
|
|
|
|
| 431 |
try:
|
| 432 |
+
for tag, a, _b in _live_session_worker(artifact_path):
|
| 433 |
if tag == "prep":
|
| 434 |
# a = fraction, _b = description -> prep bar overlaid in the viewer
|
| 435 |
yield (_prep_overlay(a, _b), gr.update())
|
| 436 |
elif tag == "ready":
|
| 437 |
+
# viser is up on port `a` -> register this token's route, then
|
| 438 |
+
# swap in the iframe (which carries the token).
|
| 439 |
+
with _sessions_lock:
|
| 440 |
+
_sessions[token] = int(a)
|
| 441 |
+
yield (_iframe_html(token), _session_bar(SESSION_SECONDS, SESSION_SECONDS))
|
| 442 |
else: # "alive": a = seconds remaining
|
| 443 |
yield (gr.update(), _session_bar(int(a), SESSION_SECONDS))
|
| 444 |
finally:
|
| 445 |
+
with _sessions_lock:
|
| 446 |
+
_sessions.pop(token, None)
|
| 447 |
yield (gr.update(), _session_bar(0, SESSION_SECONDS, expired=True))
|
| 448 |
|
| 449 |
|
|
|
|
| 460 |
|
| 461 |
http_client = httpx.AsyncClient(timeout=60.0)
|
| 462 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
async def proxy_http(request: Request) -> Response:
|
| 464 |
+
token = request.path_params.get("token", "")
|
| 465 |
path = request.path_params.get("path", "")
|
| 466 |
+
port = _session_port(token)
|
| 467 |
if port is None:
|
| 468 |
return Response(b"No active viser session.", status_code=503)
|
| 469 |
url = f"http://localhost:{port}/{path}"
|
|
|
|
| 483 |
return Response(content=r.content, status_code=r.status_code, headers=resp_headers)
|
| 484 |
|
| 485 |
async def proxy_ws(websocket: WebSocket) -> None:
|
| 486 |
+
token = websocket.path_params.get("token", "")
|
| 487 |
path = websocket.path_params.get("path", "")
|
| 488 |
+
port = _session_port(token)
|
| 489 |
offered = list(websocket.scope.get("subprotocols") or [])
|
| 490 |
if port is None:
|
| 491 |
await websocket.close(code=1011)
|
|
|
|
| 588 |
.col-right { flex: 3 1 0 !important; min-width: 360px; }
|
| 589 |
"""
|
| 590 |
|
| 591 |
+
# delete_cache=(freq, age): Gradio sweeps its OWN cache (uploaded files etc.)
|
| 592 |
+
# every hour, removing entries older than an hour — a safety net against disk
|
| 593 |
+
# exhaustion on HF Spaces.
|
| 594 |
+
with gr.Blocks(title="TTB-DVSM (ZeroGPU)", delete_cache=(3600, 3600)) as demo:
|
| 595 |
+
# delete_callback ties each user's recon artifact lifetime to their session:
|
| 596 |
+
# Gradio calls it when the State is cleaned up (tab closed +60min, or TTL),
|
| 597 |
+
# so /tmp artifacts don't accumulate. time_to_live also bounds idle sessions.
|
| 598 |
+
artifact_state = gr.State("", delete_callback=_delete_artifact, time_to_live=3600)
|
| 599 |
# Full-width description on top.
|
| 600 |
gr.HTML(
|
| 601 |
"<h1 style='margin-bottom:0.1em;color:#76B900'>TTB-DVSM</h1>"
|
|
|
|
| 674 |
|
| 675 |
# Recon status overlays the viewer box; the gradio progress bar (the "20%")
|
| 676 |
# therefore also shows over the viewer, aligned with the upload column.
|
| 677 |
+
recon_btn.click(reconstruct_cb, [inputs, n_frames, tw, artifact_state],
|
| 678 |
[artifact_state, viewer_html, session_btn])
|
| 679 |
# show_progress="hidden": the session generator streams for the whole GPU
|
| 680 |
# hold; without this Gradio overlays an auto progress bar on EACH output
|
|
|
|
| 714 |
proxy_http, proxy_ws = _build_proxy_handlers()
|
| 715 |
methods = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"]
|
| 716 |
routes = [
|
| 717 |
+
Route("/viser-proxy/{token}", proxy_http, methods=methods),
|
| 718 |
+
Route("/viser-proxy/{token}/{path:path}", proxy_http, methods=methods),
|
| 719 |
+
WebSocketRoute("/viser-proxy/{token}", proxy_ws),
|
| 720 |
+
WebSocketRoute("/viser-proxy/{token}/{path:path}", proxy_ws),
|
| 721 |
]
|
| 722 |
app.router.routes[0:0] = routes # prepend → highest match priority
|
| 723 |
print("[app] viser-proxy routes prepended; ready", flush=True)
|
|
|
|
| 729 |
`exec`s `python app.py` or `import`s the module and would otherwise call
|
| 730 |
demo.launch() itself. prevent_thread_lock=True lets us attach routes after
|
| 731 |
launch; we then block to keep the process alive."""
|
| 732 |
+
# Unlimited Gradio-side concurrency: live_session is a long generator that
|
| 733 |
+
# holds for the whole session, so Gradio's default limit of 1 would make a
|
| 734 |
+
# second window queue behind the first and never show. We let ZeroGPU's own
|
| 735 |
+
# scheduler handle GPU allocation/queueing across concurrent sessions.
|
| 736 |
+
demo.queue(default_concurrency_limit=None)
|
| 737 |
demo.launch(
|
| 738 |
server_name="0.0.0.0",
|
| 739 |
server_port=int(os.environ.get("PORT", "7860")),
|
|
|
|
| 749 |
ssr_mode=False,
|
| 750 |
)
|
| 751 |
_attach_proxy_routes(demo.app)
|
|
|
|
| 752 |
threading.Event().wait() # keep the process alive
|
| 753 |
|
| 754 |
|