Update index.html
Browse files2 unmodified lines345678915 unmodified lines252627282930316 unmodified lines383940414243444546474849505152535455565758596061626364656667686970717273745 unmodified lines8081828384858616 unmodified lines10310410510610710810916 unmodified lines1261271282 unmodified lines<head><meta charset="utf-8" /><meta name="viewport" content="width=device-width,initial-scale=1" /><title>WebGPU · Transformers.js · Better Image Captioning</title><style>body { font: 16px/1.45 system-ui, sans-serif; margin: 24px auto; max-width: 900px; padding: 0 16px; }.card { border:1px solid #4443; border-radius:12px; padding:16px; }15 unmodified lines<h4>Output</h4><div id="log" class="log">Loading model…</div><p class="muted">Model: <code>Xenova/blip-image-captioning-large</code><br />Backend: <span id="backend">…</span></p></div>6 unmodified linesconst imgEl = document.getElementById('preview');const backendEl = document.getElementById('backend');// Prefer WebGPU; fall back to WASMconst hasWebGPU = 'gpu' in navigator;let device = hasWebGPU ? 'webgpu' : 'wasm';backendEl.textContent = device.toUpperCase();envEl.textContent = hasWebGPU ? '✅ WebGPU detected (will fallback if slow)…' : '⚠️ Using WASM (CPU).';// --- SINGLE FIX: force ?download=1 on HF "resolve" URLs to avoid 401s ---const _fetch = window.fetch.bind(window);window.fetch = (url, opts) => {try {const u = new URL(url);if (u.hostname === 'huggingface.co' && u.pathname.includes('/resolve/')) {if (!u.searchParams.has('download')) u.searchParams.set('download', '1');url = u.toString();}} catch {}return _fetch(url, opts);};// -----------------------------------------------------------------------// Load Transformers.js v3const { pipeline } = await import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@3');// Watchdog: if WebGPU load takes too long, retry on WASMconst LOAD_TIMEOUT_MS = 30000;let captioner;async function buildPipeline(targetDevice) {logEl.textContent = `Loading model… device=${targetDevice}`;backendEl.textContent = targetDevice.toUpperCase();return await pipeline('image-to-text', 'Xenova/blip-image-captioning-large', { device: targetDevice });}try {5 unmodified linescaptioner = await buildPipeline('wasm');}} catch (e) {if (hasWebGPU && (e.message === 'webgpu-timeout' || String(e).includes('webgpu'))) {envEl.textContent = '⚠️ WebGPU load slow/failed → falling back to WASM.';device = 'wasm';captioner = await buildPipeline('wasm');16 unmodified linesimgEl.src = imgURL;});// Run captioning (beam search for better quality)runBtn.addEventListener('click', async () => {if (!captioner) return;if (!imgURL) { logEl.textContent = 'Pick an image first.'; return; }16 unmodified lines</html>2 unmodified lines345678915 unmodified lines252627282930316 unmodified lines38394041424344454647484950515253545556575859605 unmodified lines666768697071727316 unmodified lines9091929394959616 unmodified lines1131141151162 unmodified lines<head><meta charset="utf-8" /><meta name="viewport" content="width=device-width,initial-scale=1" /><title>WebGPU · Transformers.js · Image Captioning (Works)</title><style>body { font: 16px/1.45 system-ui, sans-serif; margin: 24px auto; max-width: 900px; padding: 0 16px; }.card { border:1px solid #4443; border-radius:12px; padding:16px; }15 unmodified lines<h4>Output</h4><div id="log" class="log">Loading model…</div><p class="muted">Model: <code>Xenova/vit-gpt2-image-captioning</code><br />Backend: <span id="backend">…</span></p></div>6 unmodified linesconst imgEl = document.getElementById('preview');const backendEl = document.getElementById('backend');// Prefer WebGPU; fall back to WASM if unavailable/slowconst hasWebGPU = 'gpu' in navigator;let device = hasWebGPU ? 'webgpu' : 'wasm';backendEl.textContent = device.toUpperCase();envEl.textContent = hasWebGPU ? '✅ WebGPU detected (will fallback if slow)…' : '⚠️ Using WASM (CPU).';// Load Transformers.js v3const { pipeline } = await import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@3');// Watchdog: if WebGPU load takes too long, retry on WASMconst LOAD_TIMEOUT_MS = 25000;let captioner;async function buildPipeline(targetDevice) {logEl.textContent = `Loading model… device=${targetDevice}`;backendEl.textContent = targetDevice.toUpperCase();return await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning', { device: targetDevice });}try {5 unmodified linescaptioner = await buildPipeline('wasm');}} catch (e) {// Fallback once to WASM if WebGPU fails or stallsif (hasWebGPU && (e.message === 'webgpu-timeout' || String(e).toLowerCase().includes('webgpu'))) {envEl.textContent = '⚠️ WebGPU load slow/failed → falling back to WASM.';device = 'wasm';captioner = await buildPipeline('wasm');16 unmodified linesimgEl.src = imgURL;});// Run captioning (beam search for better captions)runBtn.addEventListener('click', async () => {if (!captioner) return;if (!imgURL) { logEl.textContent = 'Pick an image first.'; return; }16 unmodified lines</html>
index.html+9-21