Spaces:
Running on Zero
Running on Zero
File size: 16,275 Bytes
6e6f5db | 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | """Capture real Objectverse Diary UI assets for the HyperFrames demo video.
This script starts the local Gradio app with explicit mock runtimes, drives a
system Chrome instance through the Chrome DevTools Protocol, and saves
screenshots into ``video/objectverse-diary-demo/assets``.
"""
from __future__ import annotations
import argparse
import json
import os
import shutil
import signal
import socket
import subprocess
import textwrap
import time
import urllib.request
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_ASSET_DIR = PROJECT_ROOT / "video" / "objectverse-diary-demo" / "assets"
DEFAULT_TRACE_DIR = PROJECT_ROOT / ".tmp" / "demo-video-traces"
DEFAULT_CHROME_PATHS = [
Path("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"),
Path("/Applications/Chromium.app/Contents/MacOS/Chromium"),
]
NODE_CAPTURE_SCRIPT = r"""
const fs = require("fs");
const path = require("path");
const http = require("http");
const { spawn } = require("child_process");
const options = JSON.parse(process.argv[1] || process.argv[2]);
const chromeArgs = [
"--headless=new",
"--disable-gpu",
"--hide-scrollbars",
"--mute-audio",
"--no-first-run",
"--no-default-browser-check",
"--disable-background-networking",
"--disable-sync",
"--disable-extensions",
"--disable-features=Translate",
"--remote-debugging-port=0",
`--user-data-dir=${options.profileDir}`,
"about:blank",
];
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function requestJson(url) {
return new Promise((resolve, reject) => {
http.get(url, (res) => {
let body = "";
res.setEncoding("utf8");
res.on("data", (chunk) => (body += chunk));
res.on("end", () => {
try {
resolve(JSON.parse(body));
} catch (error) {
reject(error);
}
});
}).on("error", reject);
});
}
async function openWebSocket(url) {
if (typeof WebSocket === "undefined") {
throw new Error("Node WebSocket global is unavailable. Node 22+ is required.");
}
return new Promise((resolve, reject) => {
const socket = new WebSocket(url);
socket.addEventListener("open", () => resolve(socket), { once: true });
socket.addEventListener("error", (event) => reject(event.error || new Error("WebSocket error")), { once: true });
});
}
function makeClient(socket) {
let id = 0;
const pending = new Map();
socket.addEventListener("message", (event) => {
const message = JSON.parse(event.data);
if (message.id && pending.has(message.id)) {
const { resolve, reject } = pending.get(message.id);
pending.delete(message.id);
if (message.error) reject(new Error(`${message.error.message || "CDP error"} ${message.error.data || ""}`));
else resolve(message.result || {});
}
});
return {
send(method, params = {}) {
const commandId = ++id;
socket.send(JSON.stringify({ id: commandId, method, params }));
return new Promise((resolve, reject) => {
pending.set(commandId, { resolve, reject });
setTimeout(() => {
if (pending.has(commandId)) {
pending.delete(commandId);
reject(new Error(`Timed out waiting for ${method}`));
}
}, 15000);
});
},
close() {
socket.close();
},
};
}
async function waitForExpression(client, expression, timeoutMs = 20000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const result = await client.send("Runtime.evaluate", {
expression,
returnByValue: true,
awaitPromise: true,
});
if (result.result && result.result.value) return true;
await sleep(250);
}
let debugValue = {};
try {
const debug = await client.send("Runtime.evaluate", {
expression: "(() => ({ href: location.href, readyState: document.readyState, text: document.body ? document.body.innerText.slice(0, 500) : '', html: document.documentElement ? document.documentElement.outerHTML.slice(0, 500) : '' }))()",
returnByValue: true,
});
debugValue = debug.result ? debug.result.value : {};
} catch (error) {
debugValue = { debugError: String(error) };
}
throw new Error(`Timed out waiting for expression: ${expression}\nCurrent page: ${JSON.stringify(debugValue, null, 2)}`);
}
async function evaluate(client, expression) {
return client.send("Runtime.evaluate", {
expression,
returnByValue: true,
awaitPromise: true,
});
}
function withGradioRoot(source) {
return `
(() => {
const app = document.querySelector('gradio-app');
const root = app && app.shadowRoot ? app.shadowRoot : document;
const pageText = document.body ? document.body.innerText : '';
${source}
})()
`;
}
async function screenshot(client, name) {
await sleep(650);
const result = await client.send("Page.captureScreenshot", {
format: "png",
captureBeyondViewport: false,
fromSurface: true,
});
const output = path.join(options.assetDir, `${name}.png`);
fs.writeFileSync(output, Buffer.from(result.data, "base64"));
console.log(output);
}
async function main() {
fs.mkdirSync(options.assetDir, { recursive: true });
fs.mkdirSync(options.profileDir, { recursive: true });
const chrome = spawn(options.chromePath, chromeArgs, { stdio: ["ignore", "ignore", "pipe"] });
let stderr = "";
chrome.stderr.on("data", (chunk) => {
stderr += chunk.toString();
});
let debuggerUrl = "";
const start = Date.now();
while (Date.now() - start < 12000) {
const match = stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/);
if (match) {
debuggerUrl = match[1];
break;
}
await sleep(100);
}
if (!debuggerUrl) {
chrome.kill("SIGTERM");
throw new Error("Could not find Chrome DevTools WebSocket URL.");
}
const debugPort = new URL(debuggerUrl).port;
const browserSocket = await openWebSocket(debuggerUrl);
const browserClient = makeClient(browserSocket);
const createdTarget = await browserClient.send("Target.createTarget", {
url: options.appUrl,
});
browserClient.close();
await sleep(500);
const targets = await requestJson(`http://127.0.0.1:${debugPort}/json/list`);
const pageTarget = targets.find((target) => target.id === createdTarget.targetId) || targets.find(
(target) =>
target.type === "page" &&
target.webSocketDebuggerUrl &&
String(target.url || "").startsWith(options.appUrl)
);
if (!pageTarget) {
chrome.kill("SIGTERM");
throw new Error("Could not find a Chrome page target.");
}
const socket = await openWebSocket(pageTarget.webSocketDebuggerUrl);
const client = makeClient(socket);
try {
await client.send("Page.enable");
await client.send("Runtime.enable");
await client.send("Emulation.setDeviceMetricsOverride", {
width: 1920,
height: 1080,
deviceScaleFactor: 1,
mobile: false,
});
await client.send("Page.navigate", { url: options.appUrl });
await waitForExpression(client, withGradioRoot("return Boolean(root.querySelector('#objectverse-app') || pageText.includes('Objectverse Diary'));"), 60000);
await sleep(3000);
await screenshot(client, "01-hero");
await evaluate(client, withGradioRoot(`
const button = [...root.querySelectorAll('button')].find((el) => el.textContent.includes('OVD-001'));
if (!button) throw new Error('Coffee mug example button not found');
button.click();
return true;
`));
await waitForExpression(client, withGradioRoot("return pageText.includes('CoffeeMug worth is awake');"), 35000);
await sleep(1300);
await screenshot(client, "02-intake-awake");
const captureScroll = async (name, selector, offset = -120) => {
await evaluate(client, withGradioRoot(`
const el = document.querySelector(${JSON.stringify(selector)});
const shadowEl = root.querySelector(${JSON.stringify(selector)});
const target = shadowEl || el;
if (!target) throw new Error('Missing selector ${selector}');
target.scrollIntoView({ block: 'start', inline: 'nearest' });
window.scrollBy(0, ${offset});
return true;
`));
await screenshot(client, name);
};
await captureScroll("03-object-file", "#results", -80);
await captureScroll("04-diary", "#diary-output", -190);
await captureScroll("05-share-card", "#share-chat", -80);
await evaluate(client, withGradioRoot(`
const input = root.querySelector('#chat-panel textarea');
if (!input) throw new Error('Chat textarea not found');
input.value = "What have you been hiding on my desk?";
input.dispatchEvent(new Event('input', { bubbles: true }));
const button = [...root.querySelectorAll('#chat-panel button')].find((el) => el.textContent.trim() === 'Ask');
if (!button) throw new Error('Ask button not found');
button.click();
return true;
`));
await waitForExpression(client, withGradioRoot("return pageText.includes('unlimited office hours');"), 20000);
await evaluate(client, withGradioRoot(`
const input = root.querySelector('#chat-panel textarea');
if (input) {
input.value = "";
input.dispatchEvent(new Event('input', { bubbles: true }));
input.blur();
}
return true;
`));
await captureScroll("06-chat", "#chat-panel", -140);
await evaluate(client, withGradioRoot(`
const label = [...root.querySelectorAll('button, summary, [role="button"], .label-wrap, .accordion')].find((el) => el.textContent.includes('Developer details'));
const trigger = label ? (label.closest('button, summary, [role="button"]') || label) : null;
if (!trigger) throw new Error('Developer details trigger not found');
const expanded = trigger.getAttribute('aria-expanded');
if (expanded !== 'true') trigger.click();
window.scrollTo({ top: document.body.scrollHeight, behavior: 'instant' });
return true;
`));
await waitForExpression(client, withGradioRoot("return pageText.includes('Trace saved') || pageText.includes('sample-01');"), 10000);
await sleep(1200);
await screenshot(client, "07-trace");
} finally {
client.close();
chrome.kill("SIGTERM");
}
}
main().catch((error) => {
console.error(error.stack || String(error));
process.exit(1);
});
"""
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--asset-dir", type=Path, default=DEFAULT_ASSET_DIR)
parser.add_argument("--trace-dir", type=Path, default=DEFAULT_TRACE_DIR)
parser.add_argument("--port", type=int, default=7860)
parser.add_argument("--chrome-path", type=Path, default=None)
parser.add_argument("--python", type=Path, default=PROJECT_ROOT / ".venv" / "bin" / "python")
parser.add_argument(
"--use-uv",
action="store_true",
help="Run the app in a temporary uv environment instead of the project virtualenv.",
)
return parser.parse_args()
def find_free_port(preferred: int) -> int:
for port in range(preferred, preferred + 50):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe:
probe.settimeout(0.2)
if probe.connect_ex(("127.0.0.1", port)) != 0:
return port
raise RuntimeError("Could not find a free local port for Gradio.")
def wait_for_http(url: str, timeout: float = 45.0) -> None:
deadline = time.time() + timeout
last_error: Exception | None = None
while time.time() < deadline:
try:
with urllib.request.urlopen(url, timeout=3) as response:
if response.status < 500:
return
except Exception as exc: # noqa: BLE001 - retain last startup error for diagnostics.
last_error = exc
time.sleep(0.5)
raise RuntimeError(f"Timed out waiting for {url}: {last_error}")
def chrome_path(explicit_path: Path | None) -> Path:
candidates = [explicit_path] if explicit_path else []
candidates.extend(DEFAULT_CHROME_PATHS)
for candidate in candidates:
if candidate and candidate.exists():
return candidate
raise RuntimeError("Could not find Chrome. Pass --chrome-path with a Chrome or Chromium executable.")
def start_gradio(
python_path: Path,
port: int,
trace_dir: Path,
*,
use_uv: bool = False,
) -> subprocess.Popen[str]:
env = os.environ.copy()
env.update(
{
"OBJECTVERSE_VISION_BACKEND": "mock",
"OBJECTVERSE_TEXT_BACKEND": "mock",
"TRACE_OUTPUT_DIR": str(trace_dir),
"GRADIO_SERVER_NAME": "127.0.0.1",
"GRADIO_SERVER_PORT": str(port),
}
)
return subprocess.Popen(
_app_command(python_path, use_uv),
cwd=PROJECT_ROOT,
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
def _app_command(python_path: Path, use_uv: bool) -> list[str]:
if not use_uv:
return [str(python_path), "app.py"]
return [
"uv",
"run",
"--no-project",
"--with",
"gradio>=4.44,<6",
"--with",
"pydantic>=2.7,<3",
"--with",
"spaces>=0.30",
"--python",
str(python_path),
"python",
"app.py",
]
def run_node_capture(app_url: str, asset_dir: Path, chrome: Path) -> None:
profile_dir = PROJECT_ROOT / ".tmp" / "demo-video-chrome-profile"
if profile_dir.exists():
shutil.rmtree(profile_dir)
options = {
"appUrl": app_url,
"assetDir": str(asset_dir),
"profileDir": str(profile_dir),
"chromePath": str(chrome),
}
node = subprocess.run(
["node", "-e", NODE_CAPTURE_SCRIPT, json.dumps(options)],
cwd=PROJECT_ROOT,
text=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
print(node.stdout, end="")
if node.returncode != 0:
raise RuntimeError(f"Node UI capture failed with exit code {node.returncode}.")
def main() -> int:
args = parse_args()
asset_dir = args.asset_dir.resolve()
trace_dir = args.trace_dir.resolve()
python_path = args.python.resolve()
if not args.use_uv and not python_path.exists():
raise RuntimeError(f"Python interpreter not found: {python_path}")
if args.use_uv and shutil.which("uv") is None:
raise RuntimeError("uv is required when --use-uv is set.")
chrome = chrome_path(args.chrome_path)
port = find_free_port(args.port)
app_url = f"http://127.0.0.1:{port}"
asset_dir.mkdir(parents=True, exist_ok=True)
trace_dir.mkdir(parents=True, exist_ok=True)
print(
textwrap.dedent(
f"""
Capturing Objectverse Diary demo assets
app: {app_url}
assets: {asset_dir}
traces: {trace_dir}
chrome: {chrome}
"""
).strip()
)
server = start_gradio(python_path, port, trace_dir, use_uv=args.use_uv)
try:
wait_for_http(app_url)
run_node_capture(app_url, asset_dir, chrome)
finally:
if server.poll() is None:
server.send_signal(signal.SIGINT)
try:
server.wait(timeout=8)
except subprocess.TimeoutExpired:
server.terminate()
server.wait(timeout=8)
if server.stdout:
output = server.stdout.read()
if output:
print(output)
expected = [
"01-hero.png",
"02-intake-awake.png",
"03-object-file.png",
"04-diary.png",
"05-share-card.png",
"06-chat.png",
"07-trace.png",
]
missing = [name for name in expected if not (asset_dir / name).exists()]
if missing:
raise RuntimeError(f"Missing captured assets: {', '.join(missing)}")
print("Captured assets:")
for name in expected:
print(f"- {asset_dir / name}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|