Spaces:
Running
Running
File size: 15,162 Bytes
99c7c9f | 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 424 425 426 427 428 429 430 431 | /**
* Pulpie WebGPU - Main Application Controller
*
* Orchestrates WebGPU hardware detection, model caching, EuroBERT tokenization,
* block segmentation, and rendering for sizzlebop/pulpie-orange-small-onnx.
*/
import { checkWebGpuSupport, createSession, runWebGpuInference } from './lib/webgpu-engine.js';
import { loadModelBuffer } from './lib/model-cache.js';
import { getTokenizer, tokenizeBlocks } from './lib/tokenizer.js';
import { simplifyAndExtractBlocks } from './lib/html-simplifier.js';
import { packChunks } from './lib/chunker.js';
import { reconstructMainContent } from './lib/reconstructor.js';
import { fetchWebpage } from './lib/url-fetcher.js';
import { SAMPLE_HTMLS } from './lib/samples.js';
import { displayResults } from './lib/ui-renderer.js';
// Application State
const state = {
gpuSupported: false,
gpuInfo: null,
isModelReady: false,
isLoadingModel: false,
session: null,
provider: 'webgpu', // 'webgpu' | 'wasm'
tokenizer: null,
currentMode: 'url', // 'url' | 'html'
activeOutputTab: 'preview', // 'preview' | 'markdown' | 'html' | 'blocks'
lastResult: null,
};
// DOM Elements
const elements = {
webgpuBadge: document.getElementById('webgpuBadge'),
modelBanner: document.getElementById('modelBanner'),
progressBarContainer: document.getElementById('progressBarContainer'),
progressBarFill: document.getElementById('progressBarFill'),
progressStatusText: document.getElementById('progressStatusText'),
progressSpeedText: document.getElementById('progressSpeedText'),
modeUrlTab: document.getElementById('modeUrlTab'),
modeHtmlTab: document.getElementById('modeHtmlTab'),
urlView: document.getElementById('urlView'),
htmlView: document.getElementById('htmlView'),
urlInput: document.getElementById('urlInput'),
clearUrlBtn: document.getElementById('clearUrlBtn'),
pasteUrlBtn: document.getElementById('pasteUrlBtn'),
extractBtn: document.getElementById('extractBtn'),
htmlInput: document.getElementById('htmlInput'),
htmlDropZone: document.getElementById('htmlDropZone'),
dropOverlay: document.getElementById('dropOverlay'),
baseUrlInput: document.getElementById('baseUrlInput'),
clearHtmlBtn: document.getElementById('clearHtmlBtn'),
extractHtmlBtn: document.getElementById('extractHtmlBtn'),
statusAlert: document.getElementById('statusAlert'),
statusAlertIcon: document.getElementById('statusAlertIcon'),
statusAlertMsg: document.getElementById('statusAlertMsg'),
resultsCard: document.getElementById('resultsCard'),
pageTitleDisplay: document.getElementById('pageTitleDisplay'),
sourceUrlDisplay: document.getElementById('sourceUrlDisplay'),
statKeptBlocks: document.getElementById('statKeptBlocks'),
statRetention: document.getElementById('statRetention'),
statTokens: document.getElementById('statTokens'),
statTimeLabel: document.getElementById('statTimeLabel'),
statGpuTime: document.getElementById('statGpuTime'),
copyBtn: document.getElementById('copyBtn'),
copyBtnText: document.getElementById('copyBtnText'),
downloadMdBtn: document.getElementById('downloadMdBtn'),
tabViewPreview: document.getElementById('tabViewPreview'),
tabViewMarkdown: document.getElementById('tabViewMarkdown'),
tabViewHtml: document.getElementById('tabViewHtml'),
tabViewBlocks: document.getElementById('tabViewBlocks'),
readerContent: document.getElementById('readerContent'),
markdownContent: document.getElementById('markdownContent'),
htmlContent: document.getElementById('htmlContent'),
blockInspectorList: document.getElementById('blockInspectorList'),
};
function showStatus(msg, type = 'info') {
if (!elements.statusAlert) return;
elements.statusAlert.className = `status-alert ${type}`;
elements.statusAlertMsg.textContent = msg;
elements.statusAlert.style.display = 'flex';
}
function hideStatus() {
if (elements.statusAlert) elements.statusAlert.style.display = 'none';
}
async function initializeSystem() {
const gpuCheck = await checkWebGpuSupport();
state.gpuSupported = gpuCheck.supported;
state.gpuInfo = gpuCheck;
}
async function loadAndPrepareModel(providedBuffer = null) {
if (state.isLoadingModel) return;
state.isLoadingModel = true;
hideStatus();
if (elements.progressBarContainer) {
elements.progressBarContainer.style.display = 'flex';
}
if (elements.progressStatusText) {
elements.progressStatusText.textContent = 'Preparing model...';
}
try {
let modelBuffer = providedBuffer;
if (!modelBuffer) {
modelBuffer = await loadModelBuffer(null, ({ loaded, total, percent, speedMbps, fromCache }) => {
if (elements.progressBarFill) {
elements.progressBarFill.style.width = `${percent}%`;
}
if (fromCache) {
if (elements.progressStatusText) {
elements.progressStatusText.textContent = 'Loading model from browser cache...';
}
if (elements.progressSpeedText) {
elements.progressSpeedText.textContent = 'Cached';
}
} else {
const loadedMb = (loaded / (1024 * 1024)).toFixed(1);
const totalMb = (total / (1024 * 1024)).toFixed(1);
if (elements.progressStatusText) {
elements.progressStatusText.textContent = `Downloading model: ${loadedMb} / ${totalMb} MB (${speedMbps} Mbps)`;
}
if (elements.progressSpeedText) {
elements.progressSpeedText.textContent = `${percent}%`;
}
}
});
}
if (elements.progressStatusText) {
elements.progressStatusText.textContent = 'Compiling model session...';
}
if (elements.progressBarFill) {
elements.progressBarFill.style.width = '100%';
}
const [{ session, provider }, tokenizer] = await Promise.all([
createSession(modelBuffer, (msg) => {
if (elements.progressStatusText) {
elements.progressStatusText.textContent = msg;
}
}),
getTokenizer((status) => {
if (elements.progressStatusText) {
elements.progressStatusText.textContent = status.status;
}
}),
]);
state.session = session;
state.provider = provider;
state.tokenizer = tokenizer;
state.isModelReady = true;
if (elements.webgpuBadge) {
if (provider === 'webgpu') {
elements.webgpuBadge.textContent = 'WebGPU';
elements.webgpuBadge.className = 'badge badge-gpu';
} else {
elements.webgpuBadge.textContent = 'WASM';
elements.webgpuBadge.className = 'badge badge-wasm';
}
}
if (elements.modelBanner) {
elements.modelBanner.style.display = 'none';
}
} catch (err) {
console.error('[App] Model setup failed:', err);
showStatus(`Failed to initialize model: ${err.message}`, 'error');
if (elements.progressStatusText) {
elements.progressStatusText.textContent = 'Model loading failed.';
}
} finally {
state.isLoadingModel = false;
}
}
async function handleExtract() {
hideStatus();
let rawHtml = '';
let sourceUrl = '';
let baseUrl = '';
if (state.currentMode === 'url') {
sourceUrl = elements.urlInput.value.trim();
if (!sourceUrl) {
showStatus('Please enter a valid webpage URL to extract.', 'error');
elements.urlInput.focus();
return;
}
baseUrl = sourceUrl;
} else {
rawHtml = elements.htmlInput.value.trim();
baseUrl = elements.baseUrlInput.value.trim();
if (!rawHtml) {
showStatus('Please paste HTML content or load a sample preset.', 'error');
elements.htmlInput.focus();
return;
}
}
if (!state.isModelReady) {
showStatus('Loading model into WebGPU before running extraction...', 'info');
await loadAndPrepareModel();
if (!state.isModelReady) return;
}
const submitBtns = [elements.extractBtn, elements.extractHtmlBtn];
submitBtns.forEach((b) => {
b.disabled = true;
b.querySelector('.btn-text').textContent = 'Extracting...';
});
const overallStartTime = performance.now();
try {
if (state.currentMode === 'url') {
showStatus('Fetching webpage content...', 'info');
const fetchResult = await fetchWebpage(sourceUrl, (msg) => showStatus(msg, 'info'));
rawHtml = fetchResult.html;
sourceUrl = fetchResult.finalUrl;
}
showStatus('Segmenting HTML into semantic content blocks...', 'info');
const { doc, blocks, itemIds, title } = simplifyAndExtractBlocks(rawHtml, baseUrl);
if (blocks.length === 0) {
throw new Error('No content blocks identified in HTML.');
}
showStatus(`Tokenizing ${blocks.length} blocks with EuroBERT tokenizer...`, 'info');
const blockTokens = tokenizeBlocks(blocks, state.tokenizer);
const maxTokens = state.provider === 'webgpu' ? 2048 : 1024;
const chunks = packChunks(blockTokens, { maxTokens });
const engineName = state.provider === 'webgpu' ? 'WebGPU' : 'WASM';
showStatus(`Running ${engineName} inference across ${chunks.length} sequence chunk(s)...`, 'info');
const inferenceResult = await runWebGpuInference(state.session, chunks, itemIds, {
onProgress: ({ currentChunk, totalChunks }) => {
showStatus(`${engineName} forward pass: chunk ${currentChunk}/${totalChunks}...`, 'info');
},
});
showStatus('Reconstructing main content and generating Markdown...', 'info');
const { html: cleanHtml, markdown } = reconstructMainContent(doc, inferenceResult.labels);
const totalTimeMs = Number((performance.now() - overallStartTime).toFixed(1));
state.lastResult = {
title: title || 'Extracted Document',
sourceUrl,
cleanHtml,
markdown,
blocks,
itemIds,
labels: inferenceResult.labels,
rawPredictions: inferenceResult.rawPredictions,
inferenceTimeMs: inferenceResult.inferenceTimeMs,
totalTokens: inferenceResult.totalTokens,
totalTimeMs,
provider: state.provider,
};
displayResults(state.lastResult, elements);
hideStatus();
} catch (err) {
console.error('[App] Extraction failed:', err);
showStatus(`Extraction failed: ${err.message}`, 'error');
} finally {
submitBtns.forEach((b) => {
b.disabled = false;
b.querySelector('.btn-text').textContent = 'Extract';
});
}
}
function setupEventListeners() {
elements.modeUrlTab.addEventListener('click', () => {
state.currentMode = 'url';
elements.modeUrlTab.classList.add('active');
elements.modeHtmlTab.classList.remove('active');
elements.urlView.style.display = 'block';
elements.htmlView.style.display = 'none';
});
elements.modeHtmlTab.addEventListener('click', () => {
state.currentMode = 'html';
elements.modeHtmlTab.classList.add('active');
elements.modeUrlTab.classList.remove('active');
elements.htmlView.style.display = 'block';
elements.urlView.style.display = 'none';
});
elements.urlInput.addEventListener('input', () => {
elements.clearUrlBtn.style.display = elements.urlInput.value ? 'flex' : 'none';
});
elements.clearUrlBtn.addEventListener('click', () => {
elements.urlInput.value = '';
elements.clearUrlBtn.style.display = 'none';
elements.urlInput.focus();
});
elements.pasteUrlBtn.addEventListener('click', async () => {
try {
const text = await navigator.clipboard.readText();
if (text) {
elements.urlInput.value = text;
elements.clearUrlBtn.style.display = 'flex';
}
} catch {
showStatus('Unable to access clipboard. Please paste manually.', 'info');
}
});
elements.clearHtmlBtn.addEventListener('click', () => {
elements.htmlInput.value = '';
elements.htmlInput.focus();
});
document.querySelectorAll('.preset-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const presetKey = btn.dataset.preset;
const sample = SAMPLE_HTMLS[presetKey];
if (sample) {
elements.modeHtmlTab.click();
elements.htmlInput.value = sample;
elements.baseUrlInput.value = 'https://usefeyn.com';
showStatus(`Loaded '${presetKey}' sample HTML document.`, 'info');
}
});
});
elements.htmlDropZone.addEventListener('dragover', (e) => {
e.preventDefault();
elements.dropOverlay.style.display = 'flex';
});
elements.htmlDropZone.addEventListener('dragleave', (e) => {
if (!elements.htmlDropZone.contains(e.relatedTarget)) {
elements.dropOverlay.style.display = 'none';
}
});
elements.htmlDropZone.addEventListener('drop', (e) => {
e.preventDefault();
elements.dropOverlay.style.display = 'none';
const files = e.dataTransfer?.files;
if (files && files[0]) {
const reader = new FileReader();
reader.onload = () => {
elements.htmlInput.value = reader.result;
elements.modeHtmlTab.click();
showStatus(`Loaded file: ${files[0].name}`, 'info');
};
reader.readAsText(files[0]);
}
});
document.querySelectorAll('.output-tab').forEach((tab) => {
tab.addEventListener('click', () => {
document.querySelectorAll('.output-tab').forEach((t) => t.classList.remove('active'));
tab.classList.add('active');
const tabName = tab.dataset.tab;
state.activeOutputTab = tabName;
elements.tabViewPreview.style.display = tabName === 'preview' ? 'block' : 'none';
elements.tabViewMarkdown.style.display = tabName === 'markdown' ? 'block' : 'none';
elements.tabViewHtml.style.display = tabName === 'html' ? 'block' : 'none';
elements.tabViewBlocks.style.display = tabName === 'blocks' ? 'block' : 'none';
});
});
elements.copyBtn.addEventListener('click', async () => {
if (!state.lastResult) return;
const content =
state.activeOutputTab === 'html' ? state.lastResult.cleanHtml : state.lastResult.markdown;
try {
await navigator.clipboard.writeText(content);
elements.copyBtnText.textContent = 'Copied!';
setTimeout(() => (elements.copyBtnText.textContent = 'Copy'), 2000);
} catch {
showStatus('Clipboard copy failed. Please copy manually.', 'info');
}
});
elements.downloadMdBtn.addEventListener('click', () => {
if (!state.lastResult?.markdown) return;
const blob = new Blob([state.lastResult.markdown], { type: 'text/markdown;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const safeTitle = (state.lastResult.title || 'extracted')
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
a.href = url;
a.download = `${safeTitle || 'pulpie-content'}.md`;
a.click();
URL.revokeObjectURL(url);
});
elements.extractBtn.addEventListener('click', handleExtract);
elements.extractHtmlBtn.addEventListener('click', handleExtract);
window.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault();
handleExtract();
}
});
}
document.addEventListener('DOMContentLoaded', async () => {
setupEventListeners();
await initializeSystem();
loadAndPrepareModel();
});
|