Spaces:
Running
Running
File size: 5,117 Bytes
87b5db3 | 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 | // Headless Chrome smoke: REAL model download + REAL inference in the browser (WASM/CPU; no GPU here).
// URL defaults to local `vite preview`; set URL=https://<space>.static.hf.space/ to test the published Space.
// OUT=dir for screenshots/JSON. VIDEO_FPS (default 2) to keep the CPU run short.
import puppeteer from 'puppeteer-core';
import { mkdirSync, writeFileSync } from 'node:fs';
const url = process.env.URL || 'http://127.0.0.1:43135/';
const out = process.env.OUT || '/workspace/evidence/rotoscopia-lab/browser-local';
const variant = process.env.VARIANT || 'q4f16';
mkdirSync(out, { recursive: true });
const browser = await puppeteer.launch({ executablePath: process.env.CHROME || '/usr/bin/google-chrome', headless: true, protocolTimeout: 3600000, args: ['--no-sandbox'] });
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 1600 });
const hosts = {}, nonGet = [], errors = [];
page.on('request', (r) => { const u = new URL(r.url()); hosts[u.host] = (hosts[u.host] || 0) + 1; if (!['GET', 'HEAD', 'OPTIONS'].includes(r.method()) && !u.protocol.startsWith('blob') && !u.protocol.startsWith('data')) nonGet.push(`${r.method()} ${r.url().slice(0, 120)}`); });
page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text().slice(0, 300)); });
page.on('pageerror', (e) => errors.push('pageerror ' + String(e).slice(0, 300)));
await page.goto(url, { waitUntil: 'networkidle0' });
const env = await page.evaluate(() => ({ crossOriginIsolated: self.crossOriginIsolated, ua: navigator.userAgent, cores: navigator.hardwareConcurrency, webgpu: !!navigator.gpu }));
console.log('env', JSON.stringify(env));
await page.evaluate((v) => { document.getElementById('chk-webgpu').checked = false; const s = document.getElementById('variant'); s.value = v; }, variant);
const t0 = Date.now();
await page.click('#btn-load');
await page.waitForFunction(() => /Modelo listo|No se pudo/.test(document.getElementById('model-status').textContent), { timeout: 1800000, polling: 2000 });
const modelStatus = await page.$eval('#model-status', (e) => e.textContent);
const loadMs = Date.now() - t0;
console.log('model:', modelStatus, `(${loadMs} ms incl. download)`);
const report = { url, createdAt: new Date().toISOString(), env, variant, modelStatus, loadMsInclDownload: loadMs, examples: [] };
if (!/Modelo listo/.test(modelStatus)) { writeFileSync(out + '/report.json', JSON.stringify({ ...report, errors }, null, 2)); await browser.close(); process.exit(2); }
const shot = async (name) => page.screenshot({ path: `${out}/${name}.png`, clip: await page.$eval('#stage', (c) => { const b = c.getBoundingClientRect(); return { x: b.x, y: b.y + window.scrollY, width: b.width, height: b.height }; }) });
for (const id of ['fruit-bowl', 'desk-laptop', 'bicycle-street']) {
const t = Date.now();
await page.click(`.example[data-id="${id}"]`);
await page.waitForFunction((id) => { const r = document.getElementById('results'); return r.dataset.exampleId === id && (r.dataset.status === 'ok' || r.dataset.status === 'error'); }, { timeout: 1800000, polling: 1000 }, id);
const info = await page.evaluate(() => { const r = document.getElementById('results'); return { status: r.dataset.status, area: +r.dataset.area, areaPct: +r.dataset.areaPct, bbox: JSON.parse(r.dataset.bbox || 'null'), encodeMs: +r.dataset.encodeMs, decodeMs: +r.dataset.decodeMs, device: r.dataset.device, text: r.textContent.slice(0, 400) }; });
info.wallMs = Date.now() - t;
console.log(id, JSON.stringify(info));
report.examples.push({ id, ...info });
await shot(`ui-${id}`);
}
// Interactive re-click on the last image: add a click and measure decode-only latency
// Video example
const fps = process.env.VIDEO_FPS || '2';
const tv = Date.now();
await page.click('.example[data-id="desk-pan"]');
await page.waitForFunction(() => { const r = document.getElementById('results'); return r.dataset.exampleId === 'desk-pan' && (r.dataset.status === 'ok' || r.dataset.status === 'error'); }, { timeout: 1800000, polling: 1000 });
await shot('ui-desk-pan-frame0');
await page.select('#fps', fps);
await page.click('#btn-track');
await page.waitForFunction(() => ['track-ok', 'error'].includes(document.getElementById('results').dataset.status), { timeout: 3600000, polling: 2000 });
const vinfo = await page.evaluate(() => { const r = document.getElementById('results'); return { status: r.dataset.status, frames: +r.dataset.frames, lost: +r.dataset.lost, meanFrameMs: +r.dataset.meanFrameMs, areas: JSON.parse(r.dataset.areas || '[]'), text: document.getElementById('track-text').textContent }; });
vinfo.wallMs = Date.now() - tv; vinfo.fps = +fps;
console.log('desk-pan', JSON.stringify(vinfo));
report.video = vinfo;
await shot('ui-desk-pan-last');
await page.screenshot({ path: `${out}/ui-full.png`, fullPage: true });
report.hostsContacted = hosts; report.nonGetRequests = nonGet; report.consoleErrors = errors;
writeFileSync(out + '/report.json', JSON.stringify(report, null, 2));
console.log('hosts', JSON.stringify(hosts), 'nonGet', nonGet.length, 'errors', errors.length);
await browser.close();
|