Spaces:
Running
Running
File size: 14,551 Bytes
8e950ce 004e4d7 8e950ce 004e4d7 8e950ce 455d643 004e4d7 8e950ce 004e4d7 8e950ce 004e4d7 a757215 004e4d7 8e950ce 004e4d7 8e950ce 004e4d7 8e950ce 004e4d7 8e950ce 004e4d7 8e950ce 004e4d7 8e950ce 004e4d7 292ea01 a757215 292ea01 004e4d7 8e950ce 004e4d7 292ea01 004e4d7 a757215 004e4d7 b21ad16 004e4d7 61840c3 004e4d7 8e950ce 004e4d7 61840c3 004e4d7 61840c3 004e4d7 61840c3 004e4d7 a757215 004e4d7 | 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | // AI summaries per article section, running fully on-device via WebGPU.
// Model runtime vendored from huggingface.co/spaces/webml-community/lfm2-webgpu-kernels
// (Lfm2Mobile: GGUF loader + custom WGSL kernels + generation loop, all in lfm2_5.js).
import { Lfm2Mobile, DEFAULT_MODEL_ID } from "./lfm2_5.js";
// A "section" is an H1 or H2 plus every direct-child element after it up to the next
// H1/H2 (mirrors the boundaries the .article-sidebar TOC already uses — H3s are
// sub-headings within their parent section, not their own summarizable unit).
const HEADING_SELECTOR = ":scope > h1, :scope > h2";
const MIN_CHARS = 60; // skip empty/near-empty sections (e.g. a heading right before the next one)
const MAX_NEW_TOKENS = 512;
const CIRC = 2 * Math.PI * 13; // matches the r=13 <circle> in renderRing()
// Model + its download/warmup are shared across every section on the page: only the
// first click anywhere pays the load cost. `progressListeners` fans load progress out to
// every ring currently waiting on it (a section clicked mid-download gets live updates,
// not just the one that triggered the load). `genTail` serializes generate() calls, since
// the model can only run one generation at a time.
let model = null;
let loadPromise = null;
const progressListeners = new Set();
let genTail = Promise.resolve();
// Confirm WebGPU is actually usable BEFORE we ever touch the model. `navigator.gpu`
// merely existing isn't enough — a browser can expose the API yet hand back no
// adapter (blocklisted GPU, headless, disabled flag), so we request an adapter and
// only resolve once one comes back. The result is cached so repeated summary clicks
// don't re-probe; a failure clears the cache so a later attempt can retry.
let webgpuReady = null;
function ensureWebGPU() {
if (!navigator.gpu) {
return Promise.reject(new Error("Summaries need a WebGPU browser (recent Chrome or Edge)."));
}
if (!webgpuReady) {
webgpuReady = (async () => {
let adapter = null;
try {
adapter = await navigator.gpu.requestAdapter();
} catch {
adapter = null;
}
if (!adapter) {
throw new Error("WebGPU is present but no compatible GPU adapter is available on this device.");
}
return adapter;
})().catch((err) => {
webgpuReady = null; // let a future click re-probe
throw err;
});
}
return webgpuReady;
}
document.addEventListener("DOMContentLoaded", init);
function init() {
const article = document.querySelector(".article");
if (!article) return;
// Read the section boundaries from the ORIGINAL tree before any wrapping happens below
// (wrapping a heading only reparents the heading itself, not its following siblings, but
// computing every section's content list up front keeps this a clean read-then-write pass).
// Also: must run before script.js wraps <pre> blocks into .code-collapsible — relies on
// this file's <script type="module"> tag appearing before ../script.js in each post's
// HTML, which makes it execute first (defer/module scripts run in document order).
const headings = Array.from(article.querySelectorAll(HEADING_SELECTOR));
headings
.map((heading, i) => ({ heading, contentEls: collectSection(heading, headings[i + 1] || null) }))
.filter(({ contentEls }) => sectionText(contentEls).length >= MIN_CHARS)
.forEach(({ heading, contentEls }) => setupSection(heading, contentEls));
}
function collectSection(heading, until) {
const els = [];
let node = heading.nextElementSibling;
while (node && node !== until) {
els.push(node);
node = node.nextElementSibling;
}
return els;
}
function sectionText(contentEls) {
return contentEls.map(extractElementText).filter(Boolean).join("\n\n");
}
// textContent of a stripped clone so we never touch the live DOM; drops KaTeX's hidden
// MathML annotation clone (raw LaTeX source) so math-heavy sections don't feed the model
// duplicated/garbled text. Tables and lists get a lightweight text layout so cells/items
// don't run together; code blocks keep their original whitespace.
function extractElementText(el) {
const clone = el.cloneNode(true);
clone.querySelectorAll(".katex-mathml").forEach((n) => n.remove());
if (clone.tagName === "TABLE") return extractTableText(clone);
if (clone.tagName === "UL" || clone.tagName === "OL") return extractListText(clone);
if (clone.tagName === "PRE") return clone.textContent.trim();
return clone.textContent.replace(/\s+/g, " ").trim();
}
function extractTableText(table) {
return Array.from(table.querySelectorAll("tr"))
.map((tr) =>
Array.from(tr.querySelectorAll("th,td"))
.map((cell) => cell.textContent.replace(/\s+/g, " ").trim())
.join(" | ")
)
.join("\n");
}
function extractListText(list) {
return Array.from(list.querySelectorAll(":scope > li"))
.map((li) => "- " + li.textContent.replace(/\s+/g, " ").trim())
.join("\n");
}
function setupSection(heading, contentEls) {
const wrap = document.createElement("div");
wrap.className = "ai-sum-wrap";
heading.parentNode.insertBefore(wrap, heading);
wrap.appendChild(heading);
const btn = document.createElement("button");
btn.type = "button";
btn.className = "ai-sum-btn tooltip";
btn.setAttribute("aria-label", "Summarize this section");
btn.dataset.tooltip = "Summarize this section";
btn.innerHTML = SUMMARIZE_ICON;
heading.appendChild(btn);
let note = null;
let state = "idle"; // idle | busy | done | error
btn.addEventListener("click", () => {
if (state === "busy") return;
if (state === "done") {
toggleNote();
return;
}
startSummary(); // idle or error -> (re)start
});
function ensureNote() {
if (note) return note;
note = document.createElement("div");
note.className = "ai-sum-note";
const toggle = document.createElement("button");
toggle.type = "button";
toggle.className = "ai-sum-note-toggle";
toggle.setAttribute("aria-expanded", "true");
toggle.innerHTML = '<span class="ai-sum-arrow">▼</span><span>AI summary</span>';
toggle.addEventListener("click", toggleNote);
const body = document.createElement("div");
body.className = "ai-sum-note-body";
body.setAttribute("aria-live", "polite");
note.appendChild(toggle);
note.appendChild(body);
wrap.appendChild(note); // right after the heading, before the section's own content
return note;
}
function toggleNote() {
if (!note) return;
const collapsed = note.classList.toggle("collapsed");
note.querySelector(".ai-sum-note-toggle")
.setAttribute("aria-expanded", String(!collapsed));
btn.dataset.tooltip = collapsed
? "Show summary"
: "Hide summary";
}
async function startSummary() {
state = "busy";
btn.classList.add("is-busy");
btn.disabled = true;
ensureNote();
note.classList.remove("collapsed");
note.querySelector(".ai-sum-note-toggle").setAttribute("aria-expanded", "true");
const body = note.querySelector(".ai-sum-note-body");
const ring = renderRing(body, "Requesting GPU device…");
const onProgress = (event) => applyLoadProgress(ring, event);
try {
// Confirm a real GPU adapter exists first; only then do we start pulling
// the (large) model down. Never the other way around.
await ensureWebGPU();
if (!model) {
progressListeners.add(onProgress);
try {
model = await getModel();
} finally {
progressListeners.delete(onProgress);
}
}
ring.setLabel("Generating summary…");
ring.setDetail("");
ring.setIndeterminate();
function limitWords(text, maxWords) {
return text.split(/\s+/).slice(0, maxWords).join(" ");
}
const input = limitWords(sectionText(contentEls), 130);
const prompt = "Summarize this section directly:\n\n" + input;
let textEl = null; // created on the first streamed chunk, once we drop the ring
const summary = await enqueueGenerate(() =>
runGenerate(prompt, (partial) => {
if (!textEl) {
ring.destroy();
textEl = renderStreamText(body);
}
textEl.textContent = partial;
})
);
const finalEl = textEl || renderStreamText(body); // stream yielded nothing before completing
finalEl.textContent = summary || "(empty response)";
finalEl.classList.remove("ai-sum-streaming");
state = "done";
} catch (err) {
console.error(err);
ring.destroy();
body.innerHTML = "";
const errEl = document.createElement("p");
errEl.className = "ai-sum-error";
errEl.textContent = "Couldn't generate a summary — " + (err?.message ?? String(err));
body.appendChild(errEl);
state = "error";
} finally {
btn.classList.remove("is-busy");
btn.disabled = false;
}
}
}
function getModel() {
if (!loadPromise) {
loadPromise = (async () => {
const m = await Lfm2Mobile.load(DEFAULT_MODEL_ID, {
onProgress: (event) => {
for (const fn of progressListeners) fn(event);
},
});
for (const fn of progressListeners) fn({ status: "warmup" });
await m.warmup();
return m;
})().catch((err) => {
loadPromise = null; // let the next click retry the load
throw err;
});
}
return loadPromise;
}
// Runs one generation exclusively — chained onto the shared tail so two sections clicked
// close together never call generate() on the model concurrently.
function enqueueGenerate(fn) {
const run = genTail.then(fn, fn);
genTail = run.then(
() => {},
() => {}
);
return run;
}
// Streams the reply, but caps how often onChunk actually touches the DOM — small models
// can emit far faster than the screen (or a screen reader on the note's aria-live region)
// can usefully keep up with.
const STREAM_RENDER_MS = 90;
async function runGenerate(prompt, onChunk) {
const stream = model.generate([{ role: "user", content: prompt }], {
maxNewTokens: MAX_NEW_TOKENS,
});
let full = "";
let lastRenderAt = 0;
for await (const chunk of stream) {
full = chunk.text;
const now = performance.now();
if (now - lastRenderAt >= STREAM_RENDER_MS) {
lastRenderAt = now;
onChunk?.(full);
}
}
return full.trim();
}
function renderStreamText(container) {
container.innerHTML = "";
const el = document.createElement("span");
el.className = "ai-sum-streaming";
container.appendChild(el);
return el;
}
// Maps Lfm2Mobile.load()'s onProgress events onto a 0..0.9 ring (0.9..1 is warmup,
// driven separately below), mirroring the cost-weighted mapping the kernels demo uses:
// the GGUF download dwarfs everything else, so it owns most of the bar.
function applyLoadProgress(ring, event) {
if (event.status === "warmup") {
ring.setLabel("Warming up kernels…");
ring.setDetail("");
ring.setIndeterminate();
return;
}
const frac = Number.isFinite(event.fraction) ? Math.max(0, Math.min(1, event.fraction)) : null;
const labels = {
init: "Requesting GPU device…",
tokenizer: "Loading tokenizer…",
weights: event.fromCache ? "Loading cached model…" : "Downloading model…",
ready: "Preparing GPU weights…",
};
ring.setLabel(labels[event.status] ?? String(event.status));
if (event.status === "weights") {
if (frac !== null) ring.setProgress(0.08 + frac * 0.82);
if (Number.isFinite(event.loaded) && Number.isFinite(event.total)) {
ring.setDetail(formatBytes(event.loaded) + " / " + formatBytes(event.total));
} else {
ring.setDetail("");
}
} else if (event.status === "tokenizer") {
ring.setProgress(0.06);
ring.setDetail("");
} else if (event.status === "ready") {
ring.setProgress(0.9);
ring.setDetail("");
} else {
ring.setProgress(0.02);
ring.setDetail("");
}
}
function formatBytes(n) {
if (n < 1024) return n + " B";
const units = ["KB", "MB", "GB"];
let u = -1;
do {
n /= 1024;
u++;
} while (n >= 1024 && u < units.length - 1);
return n.toFixed(1) + " " + units[u];
}
// A small circular (never spinning) progress indicator: a determinate ring driven by
// setProgress() while we know a real fraction (model download), and an indeterminate
// "breathing" ring — stroke-dashoffset oscillating via CSS keyframes — once we don't
// (warmup, token generation).
function renderRing(container, initialLabel) {
container.innerHTML = "";
const wrapEl = document.createElement("div");
wrapEl.className = "ai-sum-progress";
wrapEl.innerHTML =
'<span class="ai-sum-ring">' +
'<svg viewBox="0 0 32 32">' +
'<circle class="ai-sum-ring-track" cx="16" cy="16" r="13"/>' +
'<circle class="ai-sum-ring-fill" cx="16" cy="16" r="13" ' +
'stroke-dasharray="' +
CIRC.toFixed(2) +
'" stroke-dashoffset="' +
CIRC.toFixed(2) +
'"/>' +
"</svg>" +
"</span>" +
'<span class="ai-sum-progress-text">' +
'<span class="ai-sum-progress-label"></span>' +
'<span class="ai-sum-progress-detail"></span>' +
"</span>";
container.appendChild(wrapEl);
const fill = wrapEl.querySelector(".ai-sum-ring-fill");
const ringEl = wrapEl.querySelector(".ai-sum-ring");
const labelEl = wrapEl.querySelector(".ai-sum-progress-label");
const detailEl = wrapEl.querySelector(".ai-sum-progress-detail");
const api = {
setLabel(text) {
labelEl.textContent = text;
},
setDetail(text) {
detailEl.textContent = text || "";
},
setProgress(frac) {
ringEl.classList.remove("is-indeterminate");
fill.style.strokeDashoffset = (CIRC * (1 - Math.max(0, Math.min(1, frac)))).toFixed(2);
},
setIndeterminate() {
ringEl.classList.add("is-indeterminate");
},
destroy() {
wrapEl.remove();
},
};
api.setLabel(initialLabel);
api.setProgress(0.02);
return api;
}
// A plain "condensed text" glyph — three lines collapsing into a short one —
// that reads as "summarize" without borrowing the four-point spark silhouette
// people now associate with Gemini.
const SUMMARIZE_ICON =
'<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" ' +
'stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
'<line x1="4" y1="6" x2="20" y2="6"/>' +
'<line x1="4" y1="11" x2="20" y2="11"/>' +
'<line x1="4" y1="16" x2="14" y2="16"/>' +
"</svg>";
|