Upload example_web.ts with huggingface_hub
Browse files38 unmodified lines39404142434454 unmodified lines991001011021031041057 unmodified lines11311411511611711839 unmodified lines15815916016116216316438 unmodified linesvae: ort.InferenceSession;tokenize: (text: string) => Promise<{ ctx: Float32Array; mask: Float32Array }>;backend: string;}const fileUrl = (repo: string, path: string) => `https://huggingface.co/${repo}/resolve/main/${path}`;54 unmodified linesasync function download(url: string, file: string, onProgress?: (p: DownloadProgress) => void): Promise<ArrayBuffer> {const cache = await caches.open('supra2-img-v1');const hit = await cache.match(url);if (hit) return hit.arrayBuffer();const res = await fetch(url);if (!res.ok) throw new Error(`download failed (${res.status}): ${file}`);const total = Number(res.headers.get('content-length')) || null;7 unmodified linesloaded += value.byteLength;onProgress?.({ file, loaded, total });}const buf = new Uint8Array(loaded);let off = 0;for (const c of chunks) { buf.set(c, off); off += c.byteLength; }39 unmodified linesfor (let i = 0; i < cfg.ctx_len; i++) mask[i] = am[i] === 0n ? 0 : 1;return { ctx: data, mask };};return { cfg, dit, enc, vae, tokenize, backend };} catch (err) {errors.push(`${backend}: ${(err as Error).message}`);}38 unmodified lines3940414243444554 unmodified lines1001011021031041051061071081091101111121131141151161171181191207 unmodified lines12812913013113213313413513639 unmodified lines17617717817918018118238 unmodified linesvae: ort.InferenceSession;tokenize: (text: string) => Promise<{ ctx: Float32Array; mask: Float32Array }>;backend: string;encoderPath: string;}const fileUrl = (repo: string, path: string) => `https://huggingface.co/${repo}/resolve/main/${path}`;54 unmodified linesasync function download(url: string, file: string, onProgress?: (p: DownloadProgress) => void): Promise<ArrayBuffer> {const cache = await caches.open('supra2-img-v1');const hit = await cache.match(url);if (hit) {const buf = await hit.arrayBuffer();try {const head = await fetch(url, { method: 'HEAD' });const total = Number(head.headers.get('content-length')) || null;if (total == null || buf.byteLength === total) {onProgress?.({ file, loaded: buf.byteLength, total: total ?? buf.byteLength });return buf;}await cache.delete(url);} catch {onProgress?.({ file, loaded: buf.byteLength, total: buf.byteLength });return buf;}}const res = await fetch(url);if (!res.ok) throw new Error(`download failed (${res.status}): ${file}`);const total = Number(res.headers.get('content-length')) || null;7 unmodified linesloaded += value.byteLength;onProgress?.({ file, loaded, total });}if (total != null && loaded !== total) {throw new Error(`truncated download: ${file} got ${loaded}/${total} bytes — retry`);}const buf = new Uint8Array(loaded);let off = 0;for (const c of chunks) { buf.set(c, off); off += c.byteLength; }39 unmodified linesfor (let i = 0; i < cfg.ctx_len; i++) mask[i] = am[i] === 0n ? 0 : 1;return { ctx: data, mask };};return { cfg, dit, enc, vae, tokenize, backend, encoderPath: encPath };} catch (err) {errors.push(`${backend}: ${(err as Error).message}`);}
example_web.ts+20-2