Spaces:
Runtime error
Runtime error
File size: 1,377 Bytes
148af80 c9f9bbf 148af80 | 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 | // Only model assets leave/enter the network. User inputs go to a local Worker.
let worker;
let serial = 0;
const pending = new Map();
function client() {
if (!worker) {
worker = new Worker(new URL('./inference-worker.js?v=20260921-load-progress-v1', import.meta.url), {type: 'module'});
worker.onmessage = ({data}) => {
const call = pending.get(data.id);
if (!call) return;
if (data.progress) { call.onProgress?.(data.progress); return; }
pending.delete(data.id);
data.error ? call.reject(new Error(data.error)) : call.resolve(data.result);
};
worker.onerror = event => {
for (const call of pending.values()) call.reject(new Error(event.message || 'ブラウザ内の推論処理に失敗しました。'));
pending.clear(); worker.terminate(); worker = null;
};
}
return worker;
}
function request(type, payload, onProgress) {
const id = ++serial;
return new Promise((resolve, reject) => {
pending.set(id, {resolve, reject, onProgress});
client().postMessage({id, type, ...payload});
});
}
export function loadModel({onProgress, backend = 'auto'} = {}) {
return request('load', {backend}, onProgress);
}
export function analyze(samples16k, samples48k, context, questions, {onProgress} = {}) {
return request('analyze', {samples: samples16k, samples48k, context, questions}, onProgress);
}
|