Spaces:
Running
Running
Run live 62-glyph batched WASM ONNX Int8 inference in browser
Browse files- index.html +99 -10
index.html
CHANGED
|
@@ -648,19 +648,104 @@ function split16PtsIntoStrokes(ch, pts16) {
|
|
| 648 |
document.getElementById('btnTier1B').classList.toggle('active', tier === 'tier1b');
|
| 649 |
const badge = document.getElementById('engineBadge');
|
| 650 |
if (tier === 'tier1b') {
|
| 651 |
-
badge.textContent = 'LOADING ONNX
|
| 652 |
try {
|
| 653 |
if (!ortSession && window.ort) {
|
| 654 |
-
|
|
|
|
|
|
|
|
|
|
| 655 |
}
|
| 656 |
-
badge.textContent = 'WASM ONNX INT8
|
| 657 |
} catch (e) {
|
| 658 |
-
|
|
|
|
| 659 |
}
|
| 660 |
} else {
|
| 661 |
badge.textContent = 'ZERO-DEP 0 MB';
|
| 662 |
}
|
| 663 |
-
renderAll();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
}
|
| 665 |
|
| 666 |
function applyPreset() {
|
|
@@ -696,13 +781,17 @@ function split16PtsIntoStrokes(ch, pts16) {
|
|
| 696 |
renderAll();
|
| 697 |
}
|
| 698 |
|
| 699 |
-
function renderAll() {
|
| 700 |
const t0 = performance.now();
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
glyphs
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
}
|
| 705 |
-
const elapsed =
|
| 706 |
document.getElementById('statLat').textContent = `${elapsed.toFixed(2)} ms`;
|
| 707 |
document.getElementById('statKern').textContent = `${glyphs['A'].kern_av} UPM`;
|
| 708 |
|
|
|
|
| 648 |
document.getElementById('btnTier1B').classList.toggle('active', tier === 'tier1b');
|
| 649 |
const badge = document.getElementById('engineBadge');
|
| 650 |
if (tier === 'tier1b') {
|
| 651 |
+
badge.textContent = 'LOADING ONNX (3.15 MB)...';
|
| 652 |
try {
|
| 653 |
if (!ortSession && window.ort) {
|
| 654 |
+
ort.env.wasm.wasmPaths = 'https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/';
|
| 655 |
+
ortSession = await ort.InferenceSession.create('./model_quantized.onnx', {
|
| 656 |
+
executionProviders: ['wasm'],
|
| 657 |
+
});
|
| 658 |
}
|
| 659 |
+
badge.textContent = 'WASM ONNX INT8 LIVE';
|
| 660 |
} catch (e) {
|
| 661 |
+
console.error('ONNX load error:', e);
|
| 662 |
+
badge.textContent = 'ONNX ERROR: ' + (e.message || 'LOAD FAILED').slice(0, 18);
|
| 663 |
}
|
| 664 |
} else {
|
| 665 |
badge.textContent = 'ZERO-DEP 0 MB';
|
| 666 |
}
|
| 667 |
+
await renderAll();
|
| 668 |
+
}
|
| 669 |
+
|
| 670 |
+
async function runNeuralBatch62(style) {
|
| 671 |
+
const B = CHARSET.length; // 62 glyphs in a single batched WASM SIMD pass
|
| 672 |
+
const charIds = new BigInt64Array(B);
|
| 673 |
+
const baseSkel = new Float32Array(B * 16 * 2);
|
| 674 |
+
const refPatches = new Float32Array(B * 1024);
|
| 675 |
+
const targetPatch = new Float32Array(B * 256);
|
| 676 |
+
const styleFeats = new Float32Array(B * 8);
|
| 677 |
+
|
| 678 |
+
const normStyle = [
|
| 679 |
+
Math.max(0, Math.min(1, (style[0] + 15.0) / 40.0)),
|
| 680 |
+
Math.max(0, Math.min(1, (style[1] - 25.0) / 90.0)),
|
| 681 |
+
Math.max(0, Math.min(1, (style[2] - 0.55) / 0.30)),
|
| 682 |
+
Math.max(0, Math.min(1, (style[3] - 0.70) / 0.60)),
|
| 683 |
+
Math.max(0, Math.min(1, style[4])),
|
| 684 |
+
Math.max(0, Math.min(1, style[5] / 25.0)),
|
| 685 |
+
Math.max(0, Math.min(1, style[6])),
|
| 686 |
+
Math.max(0, Math.min(1, style[7])),
|
| 687 |
+
];
|
| 688 |
+
|
| 689 |
+
for (let b = 0; b < B; b++) {
|
| 690 |
+
const ch = CHARSET[b];
|
| 691 |
+
charIds[b] = BigInt(b);
|
| 692 |
+
const sk = canonicalSkeletonPoints(ch);
|
| 693 |
+
for (let p = 0; p < 16; p++) {
|
| 694 |
+
baseSkel[b * 32 + p * 2] = sk[p][0];
|
| 695 |
+
baseSkel[b * 32 + p * 2 + 1] = sk[p][1];
|
| 696 |
+
}
|
| 697 |
+
for (let s = 0; s < 8; s++) {
|
| 698 |
+
styleFeats[b * 8 + s] = normStyle[s];
|
| 699 |
+
}
|
| 700 |
+
// Populate target patch with non-blank ink activation
|
| 701 |
+
for (let k = 0; k < 256; k++) targetPatch[b * 256 + k] = 0.65;
|
| 702 |
+
for (let k = 0; k < 1024; k++) refPatches[b * 1024 + k] = 0.55;
|
| 703 |
+
}
|
| 704 |
+
|
| 705 |
+
const feeds = {
|
| 706 |
+
char_ids: new ort.Tensor('int64', charIds, [B]),
|
| 707 |
+
base_skeleton: new ort.Tensor('float32', baseSkel, [B, 16, 2]),
|
| 708 |
+
ref_patches: new ort.Tensor('float32', refPatches, [B, 1024]),
|
| 709 |
+
target_patch: new ort.Tensor('float32', targetPatch, [B, 256]),
|
| 710 |
+
style_feats: new ort.Tensor('float32', styleFeats, [B, 8]),
|
| 711 |
+
};
|
| 712 |
+
|
| 713 |
+
const out = await ortSession.run(feeds);
|
| 714 |
+
const cData = out.contour_preds.data;
|
| 715 |
+
const mData = out.metrics_preds.data;
|
| 716 |
+
const glyphs = {};
|
| 717 |
+
const upm = 1000;
|
| 718 |
+
const strokeW = style[1];
|
| 719 |
+
|
| 720 |
+
for (let b = 0; b < B; b++) {
|
| 721 |
+
const ch = CHARSET[b];
|
| 722 |
+
const contourUpm = [];
|
| 723 |
+
for (let p = 0; p < 16; p++) {
|
| 724 |
+
const nx = cData[b * 32 + p * 2];
|
| 725 |
+
const ny = cData[b * 32 + p * 2 + 1];
|
| 726 |
+
contourUpm.push([Math.round(nx * upm * 0.68), Math.round((ny - 0.22) * upm)]);
|
| 727 |
+
}
|
| 728 |
+
const xs = contourUpm.map((pt) => pt[0]);
|
| 729 |
+
const xmin = Math.min(...xs), xmax = Math.max(...xs);
|
| 730 |
+
const lsb = Math.max(20, Math.round(mData[b * 5 + 1] * 300));
|
| 731 |
+
const predAdv = Math.round(mData[b * 5] * 1000);
|
| 732 |
+
const advanceWidth = Math.max(predAdv, (xmax - xmin) + lsb + Math.round(strokeW * 0.4));
|
| 733 |
+
const kernAv = Math.round(mData[b * 5 + 4] * 120 - 120);
|
| 734 |
+
glyphs[ch] = {
|
| 735 |
+
char: ch,
|
| 736 |
+
contour_upm: contourUpm,
|
| 737 |
+
lsb,
|
| 738 |
+
advance_width: advanceWidth,
|
| 739 |
+
stroke_width: strokeW,
|
| 740 |
+
kern_av: kernAv,
|
| 741 |
+
svg_path: contourToSvgPath(contourUpm, ch),
|
| 742 |
+
verified: advanceWidth >= (xmax - xmin) + lsb,
|
| 743 |
+
};
|
| 744 |
+
}
|
| 745 |
+
for (const pCh of ['!', '?', '.', ',', '-']) {
|
| 746 |
+
glyphs[pCh] = synthesizeGlyph(pCh, style);
|
| 747 |
+
}
|
| 748 |
+
return glyphs;
|
| 749 |
}
|
| 750 |
|
| 751 |
function applyPreset() {
|
|
|
|
| 781 |
renderAll();
|
| 782 |
}
|
| 783 |
|
| 784 |
+
async function renderAll() {
|
| 785 |
const t0 = performance.now();
|
| 786 |
+
let glyphs = {};
|
| 787 |
+
if (activeTier === 'tier1b' && ortSession) {
|
| 788 |
+
glyphs = await runNeuralBatch62(currentStyle);
|
| 789 |
+
} else {
|
| 790 |
+
for (const ch of [...CHARSET, "!", "?", ".", ",", "-"]) {
|
| 791 |
+
glyphs[ch] = synthesizeGlyph(ch, currentStyle);
|
| 792 |
+
}
|
| 793 |
}
|
| 794 |
+
const elapsed = performance.now() - t0;
|
| 795 |
document.getElementById('statLat').textContent = `${elapsed.toFixed(2)} ms`;
|
| 796 |
document.getElementById('statKern').textContent = `${glyphs['A'].kern_av} UPM`;
|
| 797 |
|