text2scryfall / worker.js
vedalken's picture
Squash history: model v3 with term repair
04b8d13
Raw History Blame Contribute Delete
2.95 kB
// Runs the model off the main thread so typing stays responsive.
import {
env,
InterruptableStoppingCriteria,
pipeline,
TextStreamer,
} from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.6";
import { loadVocab, repair, translate } from "./text2scryfall.js";
env.localModelPath = new URL("./models/", import.meta.url).href;
env.allowLocalModels = true; // off by default in browsers
env.allowRemoteModels = false;
// transformers.js caches model files by URL and never revalidates them: bump this with every model upload.
const MODEL_VERSION = "v3";
try {
const marker = new URL(`./models/version-${MODEL_VERSION}`, import.meta.url).href;
if (!(await (await caches.open("transformers-cache")).match(marker))) {
await caches.delete("transformers-cache");
await (await caches.open("transformers-cache")).put(marker, new Response(""));
}
} catch (e) { console.warn("model cache check skipped:", e); }
let generator;
const vocab = loadVocab(await (await fetch(new URL("./vocab.json", import.meta.url))).json());
try {
const device = (await navigator.gpu?.requestAdapter()) ? "webgpu" : "wasm";
self.postMessage({ type: "stage", stage: "download", device });
generator = await pipeline("text-generation", "text2scryfall", {
dtype: "q4",
device,
progress_callback: (p) => self.postMessage({ type: "progress", ...p }),
});
self.postMessage({ type: "stage", stage: "warmup", device });
await translate(generator, "warm up"); // first call compiles the GPU kernels
self.postMessage({ type: "ready", device });
} catch (e) {
self.postMessage({ type: "error", message: String(e?.message ?? e) });
}
// Newer text stops the running generation, so only the latest request is ever finished.
const stop = new InterruptableStoppingCriteria();
let next = null, running = false;
self.onmessage = async ({ data }) => {
next = data;
stop.interrupt();
if (running) return;
running = true;
while (next) {
const { id, text } = next;
next = null;
stop.reset();
if (!text) continue;
const start = performance.now();
let partial = "";
const streamer = new TextStreamer(generator.tokenizer, {
skip_prompt: true,
skip_special_tokens: true,
callback_function: (piece) => self.postMessage({ type: "partial", id, text: (partial += piece) }),
});
try {
const query = repair(await translate(generator, text, { stopping_criteria: stop, streamer }), vocab);
if (!next) self.postMessage({ type: "query", id, query, ms: performance.now() - start });
} catch (e) {
console.error(e);
self.postMessage({ type: "query", id, query: `(error: ${e?.message ?? e})`, ms: performance.now() - start });
}
// On WebAssembly the run blocks this thread, so newer text queues up instead of interrupting;
// let it all arrive and keep only the newest.
await new Promise((resolve) => setTimeout(resolve));
}
running = false;
};