Download worker.mjs from EasyOctopus/german-support-redactor: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/spaces/EasyOctopus/german-support-redactor/resolve/main/worker.mjs
- Command line
-
hf download hf://spaces/EasyOctopus/german-support-redactor/worker.mjs
-
curl -L -o worker.mjs https://huggingface.co/spaces/EasyOctopus/german-support-redactor/resolve/main/worker.mjs
3.27 kB
| import {applyPolicy as filterExplicitContext} from "./explicit-context.mjs"; | |
| import * as ort from "./vendor/ort.wasm.min.mjs"; | |
| import { tokenize, extendGroups } from "./core.mjs"; | |
| ort.env.wasm.numThreads = 1; | |
| ort.env.wasm.proxy = false; | |
| ort.env.wasm.wasmPaths = new URL("./vendor/", import.meta.url).href; | |
| let ready; | |
| let roleModel; | |
| import {contextualGroups} from "./previous-question.mjs"; | |
| async function asset(path, kind) { | |
| const res = await fetch(new URL(path, import.meta.url)); | |
| if (!res.ok) throw new Error("Download failed (" + res.status + ")"); | |
| return kind === "json" ? res.json() : res.arrayBuffer(); | |
| } | |
| async function load() { | |
| const [vocab, meta, bytes] = await Promise.all([ | |
| asset("vocab.json", "json"), | |
| asset("model.json", "json"), | |
| asset("redactor.onnx", "buffer"), | |
| ]); | |
| const digest = await crypto.subtle.digest("SHA-256", bytes); | |
| const fingerprint = Array.from(new Uint8Array(digest), (x) => | |
| x.toString(16).padStart(2, "0"), | |
| ).join(""); | |
| if (fingerprint !== meta.onnx_sha256) | |
| throw new Error( | |
| "Die Modelldatei ist unvollständig. Bitte lade die Seite neu.", | |
| ); | |
| const session = await ort.InferenceSession.create(bytes, { | |
| executionProviders: ["wasm"], | |
| graphOptimizationLevel: "all", | |
| }); | |
| return { vocab, meta, session }; | |
| } | |
| self.onmessage = async ({ data }) => { | |
| try { | |
| ready ??= load().catch((error) => { | |
| ready = null; | |
| throw error; | |
| }); | |
| const { vocab, meta, session } = await ready; | |
| const tokens = tokenize(data.text, vocab); | |
| const state = { groups: [], last: null }; | |
| for (let i = 0; i < tokens.length; i += 254) { | |
| const chunk = tokens.slice(i, i + 254); | |
| const ids = BigInt64Array.from( | |
| [vocab["[CLS]"], ...chunk.map((t) => t.id), vocab["[SEP]"]], | |
| BigInt, | |
| ); | |
| const dims = [1, ids.length]; | |
| const mask = new BigInt64Array(ids.length).fill(1n); | |
| const a = new ort.Tensor("int64", ids, dims), | |
| b = new ort.Tensor("int64", mask, dims); | |
| const out = await session.run({ input_ids: a, attention_mask: b }); | |
| extendGroups(chunk, out.logits.data, meta.labels, state); | |
| a.dispose(); | |
| b.dispose(); | |
| out.logits.dispose(); | |
| } | |
| let groups = filterExplicitContext(data.text, state.groups, null); | |
| let model = meta.onnx_sha256 + "/boundaries-5b77093267e8/guard-641065f26fb7"; | |
| if (data.context?.trim()) { | |
| roleModel ??= asset("role-model.json", "buffer").then(async bytes => { | |
| const hash = await crypto.subtle.digest("SHA-256", bytes); | |
| const signature = Array.from(new Uint8Array(hash), byte => byte.toString(16).padStart(2, "0")).join(""); | |
| if (signature !== "5b9daa09e7a2758ba900aa4b8e8423dee95b1d4809e800172529a6cc9c25f70e") throw new Error("Die Datei für die Supportfrage ist unvollständig."); | |
| return JSON.parse(new TextDecoder().decode(bytes)); | |
| }).catch(error => { | |
| roleModel = null; | |
| throw error; | |
| }); | |
| groups = contextualGroups(data.context, data.text, groups, await roleModel, true); | |
| model += "/question-5b9daa09e7a2-60bdc2d65eeb-public"; | |
| } | |
| self.postMessage({id:data.id,groups,model}); | |
| } catch (error) { | |
| self.postMessage({ id: data.id, error: String(error.message ?? error) }); | |
| } | |
| }; | |