Download app.mjs from EasyOctopus/german-support-redactor: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/spaces/EasyOctopus/german-support-redactor/resolve/main/app.mjs
- Command line
-
hf download hf://spaces/EasyOctopus/german-support-redactor/app.mjs
-
curl -L -o app.mjs https://huggingface.co/spaces/EasyOctopus/german-support-redactor/resolve/main/app.mjs
5.62 kB
| import { filtered } from "./core.mjs"; | |
| const by = (id) => document.getElementById(id); | |
| const input = by("input"), | |
| slider = by("threshold"), | |
| status = by("status"); | |
| const previousQuestion = by("context"); | |
| const questions = {surname: "Wie lautet Ihr Nachname?", bookreply: "Wie lautet der Buchtitel?"}; | |
| const presets = { | |
| surname: "Zinnfalter", | |
| bookreply: "Zinnfalter", | |
| book: "Bitte den Buchtitel „Martha Atlas“ in der Leseliste korrigieren. Das zweite A ist groß.", | |
| mail: "Bitte schicken Sie die Bestätigung an lea.beispiel@example.org. Vielen Dank!", | |
| city: "Wir fahren morgen nach Essen. Ist das Museum am Montag geöffnet?", | |
| guest: "Im Einlassplan steht als Gast „Faust“. Stimmt der Eintrag?", | |
| play: "Im Spielplan steht als Stück „Faust“. Stimmt der Eintrag?", | |
| }; | |
| let worker, | |
| nextId = 0, | |
| pending = null, | |
| last = null, | |
| release, | |
| payload; | |
| function reset() { | |
| last = payload = null; | |
| by("copy").disabled = by("json").disabled = true; | |
| by("findings").hidden = true; | |
| by("out").textContent = "Das Ergebnis erscheint nach der Prüfung."; | |
| by("size").textContent = Array.from(input.value).length + " Zeichen"; | |
| by("run").disabled = !release || !input.value.trim() || pending !== null; | |
| } | |
| function render() { | |
| const threshold = Number(slider.value); | |
| by("value").textContent = threshold.toFixed(2).replace(".", ","); | |
| if (!last) return; | |
| payload = filtered(last.text, last.groups, threshold, last.model); | |
| const points = Array.from(last.text); | |
| let end = 0; | |
| const out = by("out"); | |
| out.replaceChildren(); | |
| for (const e of payload.entities) { | |
| out.append(document.createTextNode(points.slice(end, e.start).join(""))); | |
| const tag = document.createElement("mark"); | |
| tag.textContent = "[" + e.label + "]"; | |
| out.append(tag); | |
| end = e.end; | |
| } | |
| out.append(document.createTextNode(points.slice(end).join(""))); | |
| const tbody = document.querySelector("tbody"); | |
| tbody.replaceChildren(); | |
| const visible = last.groups.filter(g => g.total / g.count < threshold && | |
| !payload.entities.some(e => e.start < g.end && g.start < e.end)); | |
| const rows = [...payload.entities.map(g => ({...g, masked:true})), | |
| ...visible.map(g => ({...g, confidence:g.total/g.count, masked:false}))].sort((a,b)=>a.start-b.start); | |
| for (const g of rows) { | |
| const tr = tbody.insertRow(), confidence = g.confidence; | |
| for (const value of [ | |
| g.label, | |
| g.start + " bis " + g.end, | |
| confidence.toFixed(3).replace(".", ","), | |
| g.masked ? "geschwärzt" : "sichtbar", | |
| ]) | |
| tr.insertCell().textContent = value; | |
| } | |
| by("findings").hidden = last.groups.length === 0; | |
| by("copy").disabled = by("json").disabled = false; | |
| status.textContent = | |
| payload.entities.length + | |
| (payload.entities.length === 1 ? " geschwärzte Stelle · " : " geschwärzte Stellen · ") + | |
| last.ms + | |
| " ms auf diesem Gerät" + | |
| (last.first ? " (inklusive Modellstart)" : ""); | |
| } | |
| function fail(message) { | |
| pending = null; | |
| reset(); | |
| status.textContent = "Prüfung fehlgeschlagen. " + message; | |
| } | |
| by("run").onclick = () => { | |
| if (!release || pending !== null || !input.value.trim()) return; | |
| if (!worker) { | |
| worker = new Worker("./worker.mjs", { type: "module" }); | |
| worker.onerror = () => { | |
| worker.terminate(); | |
| worker = null; | |
| fail("Bitte lade die Seite neu oder versuche einen aktuellen Browser."); | |
| }; | |
| } | |
| const first = !worker.used; | |
| worker.used = true; | |
| const request = { | |
| id: ++nextId, | |
| text: input.value, | |
| context: previousQuestion.value, | |
| start: performance.now(), | |
| first, | |
| }; | |
| pending = request; | |
| reset(); | |
| status.textContent = first | |
| ? "Modell wird geladen. Die erste Prüfung kann etwas dauern." | |
| : "Text wird auf diesem Gerät geprüft …"; | |
| worker.onmessage = ({ data }) => { | |
| if (!pending || data.id !== pending.id) return; | |
| pending = null; | |
| by("run").disabled = !input.value.trim(); | |
| if (data.error) { | |
| fail(data.error); | |
| return; | |
| } | |
| if (request.text !== input.value || request.context !== previousQuestion.value) { | |
| reset(); | |
| status.textContent = "Der Text wurde geändert. Bitte erneut prüfen."; | |
| return; | |
| } | |
| last = { | |
| ...request, | |
| ...data, | |
| ms: Math.round(performance.now() - request.start), | |
| }; | |
| render(); | |
| }; | |
| worker.postMessage({ id: request.id, text: request.text, context: request.context }); | |
| }; | |
| previousQuestion.oninput = input.oninput = () => { | |
| reset(); | |
| if (pending === null) status.textContent = "Bereit zur Prüfung."; | |
| }; | |
| slider.oninput = render; | |
| document.querySelectorAll("[data-example]").forEach( | |
| (button) => | |
| (button.onclick = () => { | |
| previousQuestion.value = questions[button.dataset.example] ?? ""; | |
| input.value = presets[button.dataset.example]; | |
| input.oninput(); | |
| }), | |
| ); | |
| async function copy(asJson) { | |
| if (!payload) return; | |
| try { | |
| await navigator.clipboard.writeText( | |
| asJson ? JSON.stringify(payload, null, 2) : payload.redacted_text, | |
| ); | |
| status.textContent = asJson ? "JSON kopiert." : "Text kopiert."; | |
| } catch { | |
| status.textContent = | |
| "Die Zwischenablage ist gesperrt. Du kannst das Ergebnis markieren und kopieren."; | |
| } | |
| } | |
| by("copy").onclick = () => copy(false); | |
| by("json").onclick = () => copy(true); | |
| reset(); | |
| try { | |
| const res = await fetch("./release.json"); | |
| if (!res.ok) throw Error(); | |
| release = await res.json(); | |
| slider.value = release.threshold; | |
| slider.disabled = false; | |
| by("version").textContent = release.name + " · " + release.size; | |
| render(); | |
| reset(); | |
| } catch { | |
| fail("Die Modelldaten konnten nicht geladen werden."); | |
| } | |