millirec 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +71 -0
- package/README.md +229 -19
- package/dist/char-cnn/core.d.ts +18 -5
- package/dist/char-cnn/core.js +152 -25
- package/dist/char-cnn/index.d.ts +3 -2
- package/dist/char-cnn/index.js +152 -25
- package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -1
- package/dist/char-cnn/millirec-char-cnn.min.js +1 -1
- package/dist/char-cnn/options/accurate.js +4 -4
- package/dist/char-cnn/options/accurate.json +4 -4
- package/dist/char-cnn/options/digits/digits-base.head.bin +0 -0
- package/dist/char-cnn/options/digits/digits-base.q4.bin +0 -0
- package/dist/char-cnn/options/digits/digits-tiny.bin +0 -0
- package/dist/char-cnn/options/digits/digits-tiny.head.bin +0 -0
- package/dist/char-cnn/options/digits.js +17 -0
- package/dist/char-cnn/options/digits.json +15 -0
- package/dist/char-cnn/options/fast/tiny.bin +0 -0
- package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
- package/dist/char-cnn/options/fast.js +4 -4
- package/dist/char-cnn/options/fast.json +4 -4
- package/dist/char-cnn/options/lowercase/lowercase-base.head.bin +0 -0
- package/dist/char-cnn/options/lowercase/lowercase-base.q4.bin +0 -0
- package/dist/char-cnn/options/lowercase/lowercase-tiny.bin +0 -0
- package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
- package/dist/char-cnn/options/lowercase.js +17 -0
- package/dist/char-cnn/options/lowercase.json +15 -0
- package/dist/char-cnn/options/small/tiny.bin +0 -0
- package/dist/char-cnn/options/small/tiny.head.bin +0 -0
- package/dist/char-cnn/options/small.js +2 -2
- package/dist/char-cnn/options/small.json +2 -2
- package/dist/char-lite/index.d.ts +26 -4
- package/dist/char-lite/index.js +113 -11
- package/dist/char-lite/millirec-char-lite.min.js +1 -1
- package/dist/char-lite/millirec.wasm +0 -0
- package/dist/char-lite/sets.json +25 -29
- package/dist/overlap/index.d.ts +139 -0
- package/dist/overlap/millirec.wasm +0 -0
- package/package.json +45 -3
- /package/dist/char-cnn/{base.head.bin → options/accurate/base.head.bin} +0 -0
- /package/dist/char-cnn/{base.q4.bin → options/accurate/base.q4.bin} +0 -0
- /package/dist/char-cnn/{tiny.bin → options/accurate/tiny.bin} +0 -0
- /package/dist/char-cnn/{tiny.head.bin → options/accurate/tiny.head.bin} +0 -0
- /package/dist/char-cnn/{base-p25.bin → options/fast/base-p25.bin} +0 -0
- /package/dist/char-cnn/{base-p25.head.bin → options/fast/base-p25.head.bin} +0 -0
package/dist/char-cnn/core.js
CHANGED
|
@@ -299,6 +299,33 @@ function assetBaseOf(base) {
|
|
|
299
299
|
return typeof location !== "undefined" ? new URL(s, location.href).href : s;
|
|
300
300
|
}
|
|
301
301
|
var asStrokes = (s) => Array.isArray(s) && s.length && !Array.isArray(s[0]) ? [s] : s;
|
|
302
|
+
var notServed = (url2, what, base, recognizer) => new Error(`millirec: ${url2} is not ${what} \u2014 the package files are not served at ${base}; copy node_modules/millirec/dist/${recognizer}/ and pass assetBase (see README "Serving the files")`);
|
|
303
|
+
async function fetchAsset(url2, { as = "json", what, base, recognizer }) {
|
|
304
|
+
const fail = (detail) => {
|
|
305
|
+
const e = notServed(url2, what, base, recognizer);
|
|
306
|
+
if (detail) e.message += ` (${detail})`;
|
|
307
|
+
return e;
|
|
308
|
+
};
|
|
309
|
+
let r;
|
|
310
|
+
try {
|
|
311
|
+
r = await fetch(url2);
|
|
312
|
+
} catch (e) {
|
|
313
|
+
throw fail(e && e.message);
|
|
314
|
+
}
|
|
315
|
+
const type = r.headers && r.headers.get("content-type") || "";
|
|
316
|
+
if (!r.ok) throw fail(`HTTP ${r.status}`);
|
|
317
|
+
if (as === "json") {
|
|
318
|
+
try {
|
|
319
|
+
return await r.json();
|
|
320
|
+
} catch (e) {
|
|
321
|
+
throw fail(/html/i.test(type) ? "an HTML page" : e && e.message);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const buf = await r.arrayBuffer();
|
|
325
|
+
const head = String.fromCharCode(...new Uint8Array(buf, 0, Math.min(buf.byteLength, 16))).trimStart().toLowerCase();
|
|
326
|
+
if (head.startsWith("<!doctype") || head.startsWith("<html")) throw fail("an HTML page");
|
|
327
|
+
return buf;
|
|
328
|
+
}
|
|
302
329
|
|
|
303
330
|
// ../../library/src/char-cnn-core.js
|
|
304
331
|
function scriptBase() {
|
|
@@ -306,17 +333,29 @@ function scriptBase() {
|
|
|
306
333
|
return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
|
|
307
334
|
}
|
|
308
335
|
var ASSET_BASE = scriptBase();
|
|
309
|
-
var fetchBuf = (
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
336
|
+
var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
|
|
337
|
+
var DESTROYED = "millirec: recognizer destroyed";
|
|
338
|
+
var makeCanvas = (n) => typeof document !== "undefined" && document.createElement ? Object.assign(document.createElement("canvas"), { width: n, height: n }) : typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(n, n) : null;
|
|
339
|
+
var TEACH_SHARPNESS = 18;
|
|
340
|
+
var unitOf = (e) => {
|
|
341
|
+
let n = 0;
|
|
342
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
343
|
+
n = Math.sqrt(n) || 1;
|
|
344
|
+
const o = new Float32Array(e.length);
|
|
345
|
+
for (let i = 0; i < e.length; i++) o[i] = e[i] / n;
|
|
346
|
+
return o;
|
|
347
|
+
};
|
|
313
348
|
var MillirecCharCnnCore = class {
|
|
314
349
|
static async create(opts = {}) {
|
|
315
350
|
const g = typeof window !== "undefined" ? window : globalThis;
|
|
316
351
|
const config = { ...g.MILLIREC_CNN || {}, ...opts };
|
|
317
352
|
if (!config.stages || !config.stages.length) throw new Error("MillirecCharCnn: no stages configured");
|
|
353
|
+
if (typeof fetch === "undefined" || typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
|
|
354
|
+
throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
|
|
355
|
+
}
|
|
318
356
|
const inst = new this(config);
|
|
319
357
|
await inst.ready;
|
|
358
|
+
if (config.examples) await inst.teach(config.examples);
|
|
320
359
|
return inst;
|
|
321
360
|
}
|
|
322
361
|
constructor(config) {
|
|
@@ -324,26 +363,35 @@ var MillirecCharCnnCore = class {
|
|
|
324
363
|
this.base = assetBaseOf(config.assetBase || ASSET_BASE);
|
|
325
364
|
this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
|
|
326
365
|
this.stages = [];
|
|
327
|
-
this.off =
|
|
328
|
-
|
|
329
|
-
this.off.width = this.off.height = 140;
|
|
330
|
-
this.octx = this.off.getContext("2d", { willReadFrequently: true });
|
|
331
|
-
}
|
|
366
|
+
this.off = makeCanvas(140);
|
|
367
|
+
this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
|
|
332
368
|
this.ready = this.load();
|
|
333
369
|
}
|
|
334
370
|
async load() {
|
|
335
371
|
const { base, config } = this;
|
|
336
372
|
let factory = null;
|
|
337
|
-
if (config.kernel)
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
373
|
+
if (config.kernel) {
|
|
374
|
+
const url2 = base + config.kernel;
|
|
375
|
+
try {
|
|
376
|
+
factory = (await import(
|
|
377
|
+
/* @vite-ignore */
|
|
378
|
+
/* webpackIgnore: true */
|
|
379
|
+
url2
|
|
380
|
+
)).default;
|
|
381
|
+
} catch (e) {
|
|
382
|
+
const err = notServed(url2, "the char-cnn kernel", base, "char-cnn");
|
|
383
|
+
err.message += ` (${e && e.message})`;
|
|
384
|
+
throw err;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
341
387
|
for (const st of config.stages) {
|
|
342
|
-
const [modelBuf, headBuf] = await Promise.all([fetchBuf(base + st.model), fetchBuf(base + st.head)]);
|
|
388
|
+
const [modelBuf, headBuf] = await Promise.all([fetchBuf(base + st.model, base), fetchBuf(base + st.head, base)]);
|
|
389
|
+
if (this.destroyed) return;
|
|
343
390
|
const head = parseHead(headBuf);
|
|
344
391
|
const stage = { theta: st.theta == null ? null : st.theta, head, headNorm: st.headNorm !== false, size: 0 };
|
|
345
392
|
if (factory) {
|
|
346
393
|
const M = await factory();
|
|
394
|
+
if (this.destroyed) return;
|
|
347
395
|
const bytes = expandToInt8(new Uint8Array(modelBuf));
|
|
348
396
|
const ptr = M._malloc(bytes.byteLength);
|
|
349
397
|
M.HEAPU8.set(bytes, ptr);
|
|
@@ -376,6 +424,7 @@ var MillirecCharCnnCore = class {
|
|
|
376
424
|
* The canvas draw + readback is the dear part (~1 ms) and is shared by every
|
|
377
425
|
* stage of one recognize() call; only the resample differs per size. */
|
|
378
426
|
raster(strokes, size) {
|
|
427
|
+
if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
|
|
379
428
|
if (this._planeFor !== strokes) {
|
|
380
429
|
const W2 = drawStrokes(this.octx, strokes);
|
|
381
430
|
this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
|
|
@@ -394,9 +443,10 @@ var MillirecCharCnnCore = class {
|
|
|
394
443
|
strokes = asStrokes(strokes);
|
|
395
444
|
if (!strokes || !strokes.length) return null;
|
|
396
445
|
await this.ready;
|
|
397
|
-
if (!this.stages.length) return null;
|
|
446
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
398
447
|
this._planeFor = null;
|
|
399
448
|
const keep = only ? new Set(only) : null;
|
|
449
|
+
if (this.taught) return this.nearest(strokes, keep);
|
|
400
450
|
let z = null, used = 0;
|
|
401
451
|
for (let s = 0; s < this.stages.length; s++) {
|
|
402
452
|
const st = this.stages[s];
|
|
@@ -428,10 +478,67 @@ var MillirecCharCnnCore = class {
|
|
|
428
478
|
out.stage = used;
|
|
429
479
|
return out;
|
|
430
480
|
}
|
|
481
|
+
/** recognize() once teach() has run: the drawing against every taught example, in the last stage's feature space. */
|
|
482
|
+
nearest(strokes, keep) {
|
|
483
|
+
const last = this.stages.length - 1, st = this.stages[last];
|
|
484
|
+
const q = unitOf(st.embed(this.raster(strokes, st.size)));
|
|
485
|
+
const names = [...this.taught.keys()].filter((c) => !keep || keep.has(c));
|
|
486
|
+
const out = [];
|
|
487
|
+
if (names.length) {
|
|
488
|
+
const sim = names.map((c) => {
|
|
489
|
+
let b = -2;
|
|
490
|
+
for (const e of this.taught.get(c)) {
|
|
491
|
+
let d = 0;
|
|
492
|
+
for (let i = 0; i < q.length; i++) d += q[i] * e[i];
|
|
493
|
+
if (d > b) b = d;
|
|
494
|
+
}
|
|
495
|
+
return b;
|
|
496
|
+
});
|
|
497
|
+
const p = softmax(sim.map((v) => v * TEACH_SHARPNESS));
|
|
498
|
+
for (const i of [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3)) out.push({ char: names[i], prob: p[i] });
|
|
499
|
+
}
|
|
500
|
+
out.stage = last;
|
|
501
|
+
return out;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* TEACH: read symbols of your own from a few drawings of each, with no training. `examples` is
|
|
505
|
+
* `{ name: [drawing, …] }` — names are any strings (a letter, 'star', an emoji), a drawing is strokes as for
|
|
506
|
+
* recognize(). Each drawing is kept as what the last stage's network makes of it; from then on recognize() answers
|
|
507
|
+
* AMONG THE TAUGHT NAMES ONLY, by the nearest example. Three drawings of each is a good start, five is better;
|
|
508
|
+
* draw the variants people really use. `{ add: true }` keeps what was taught before (new drawings of a known name
|
|
509
|
+
* are added to it). Returns { classes, n } or null (nothing usable). forget() goes back to the shipped classes.
|
|
510
|
+
* Works best on an option whose last stage is the larger network (`accurate`, the default, or `fast`).
|
|
511
|
+
*/
|
|
512
|
+
async teach(examples, { add = false } = {}) {
|
|
513
|
+
await this.ready;
|
|
514
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
515
|
+
const taught = add && this.taught ? new Map([...this.taught].map(([c, list]) => [c, list.slice()])) : /* @__PURE__ */ new Map();
|
|
516
|
+
for (const [name, list] of Object.entries(examples || {})) {
|
|
517
|
+
for (const drawing of list || []) {
|
|
518
|
+
const strokes = asStrokes(drawing);
|
|
519
|
+
if (!strokes || !strokes.length) continue;
|
|
520
|
+
const e = unitOf(await this.embedding(strokes));
|
|
521
|
+
if (this.destroyed) return null;
|
|
522
|
+
if (!taught.has(name)) taught.set(name, []);
|
|
523
|
+
taught.get(name).push(e);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!taught.size) {
|
|
527
|
+
if (!add) this.taught = null;
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
this.taught = taught;
|
|
531
|
+
return { classes: [...taught.keys()], n: [...taught.values()].reduce((a, l) => a + l.length, 0) };
|
|
532
|
+
}
|
|
533
|
+
/** Drop everything teach() learned: recognize() reads the shipped classes again. */
|
|
534
|
+
forget() {
|
|
535
|
+
this.taught = null;
|
|
536
|
+
}
|
|
431
537
|
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
432
538
|
async embedding(strokes) {
|
|
433
539
|
strokes = asStrokes(strokes);
|
|
434
540
|
await this.ready;
|
|
541
|
+
if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
|
|
435
542
|
this._planeFor = null;
|
|
436
543
|
const st = this.stages[this.stages.length - 1];
|
|
437
544
|
return st.embed(this.raster(strokes, st.size));
|
|
@@ -439,13 +546,14 @@ var MillirecCharCnnCore = class {
|
|
|
439
546
|
/**
|
|
440
547
|
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
441
548
|
* (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
|
|
442
|
-
* a letter and many take it over
|
|
549
|
+
* a letter and many take it over. Plain
|
|
443
550
|
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
444
551
|
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
445
552
|
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
446
553
|
*/
|
|
447
554
|
async personalize(samples, { steps = 250, lr = 0.5, prior = 1 } = {}) {
|
|
448
555
|
await this.ready;
|
|
556
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
449
557
|
const st = this.stages[this.stages.length - 1];
|
|
450
558
|
const H0 = st.head0 || (st.head0 = st.head);
|
|
451
559
|
const X = [], Y = [];
|
|
@@ -459,6 +567,7 @@ var MillirecCharCnnCore = class {
|
|
|
459
567
|
Y.push(c);
|
|
460
568
|
}
|
|
461
569
|
}
|
|
570
|
+
if (this.destroyed) return null;
|
|
462
571
|
if (!X.length) {
|
|
463
572
|
st.head = H0;
|
|
464
573
|
this.personal = null;
|
|
@@ -490,32 +599,49 @@ var MillirecCharCnnCore = class {
|
|
|
490
599
|
this.personal = { n, classes: [...new Set(Y)].map((c) => this.classes[c]).sort() };
|
|
491
600
|
return this.personal;
|
|
492
601
|
}
|
|
493
|
-
/** Let go of the models and kernels; recognize()
|
|
602
|
+
/** Let go of the models and kernels; recognize() and personalize() return null afterwards, embedding() throws. */
|
|
494
603
|
destroy() {
|
|
604
|
+
this.destroyed = true;
|
|
495
605
|
this.stages = [];
|
|
496
606
|
this.personal = null;
|
|
607
|
+
this.taught = null;
|
|
497
608
|
this._plane = null;
|
|
498
609
|
this._planeFor = null;
|
|
499
610
|
}
|
|
500
611
|
depersonalize() {
|
|
501
612
|
const st = this.stages[this.stages.length - 1];
|
|
502
|
-
if (st.head0) st.head = st.head0;
|
|
613
|
+
if (st && st.head0) st.head = st.head0;
|
|
503
614
|
this.personal = null;
|
|
504
615
|
}
|
|
505
616
|
};
|
|
506
617
|
if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnnCore;
|
|
507
618
|
|
|
619
|
+
// src/here.js
|
|
620
|
+
var url = (import.meta.url || "").replace(/[?#].*$/, "");
|
|
621
|
+
var HERE = url.slice(0, url.lastIndexOf("/") + 1);
|
|
622
|
+
function nodeFileUrl(url2, spec) {
|
|
623
|
+
const g = globalThis;
|
|
624
|
+
const node = typeof g.process !== "undefined" && g.process.versions && g.process.versions.node && typeof g.Deno === "undefined" && typeof g.Bun === "undefined";
|
|
625
|
+
if (url2.startsWith("file:") && node && typeof document === "undefined" && typeof WorkerGlobalScope === "undefined") {
|
|
626
|
+
throw new Error(`millirec: cannot fetch ${url2} outside a browser \u2014 in Node, import the JSON instead: import x from '${spec}' with { type: 'json' }`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
508
630
|
// src/options.js
|
|
509
|
-
var
|
|
510
|
-
var OPTIONS = ["accurate", "fast", "small"];
|
|
631
|
+
var OPTIONS = ["accurate", "fast", "small", "digits", "lowercase"];
|
|
511
632
|
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
512
633
|
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
513
|
-
const
|
|
514
|
-
const
|
|
515
|
-
|
|
516
|
-
return
|
|
634
|
+
const base = assetBaseOf(assetBase);
|
|
635
|
+
const url2 = new URL(`options/${name}.json`, base).href;
|
|
636
|
+
nodeFileUrl(url2, `millirec/char-cnn/options/${name}.json`);
|
|
637
|
+
return fetchAsset(url2, { what: `the ${name} CNN option`, base, recognizer: "char-cnn" });
|
|
517
638
|
}
|
|
518
639
|
async function withOption(opts) {
|
|
640
|
+
if (typeof opts === "string") opts = { option: opts };
|
|
641
|
+
opts = opts || {};
|
|
642
|
+
if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
|
|
643
|
+
throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
|
|
644
|
+
}
|
|
519
645
|
const assetBase = assetBaseOf(opts.assetBase || HERE);
|
|
520
646
|
const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
|
|
521
647
|
return { ...preset, ...opts, assetBase };
|
|
@@ -524,7 +650,8 @@ async function withOption(opts) {
|
|
|
524
650
|
// src/char-cnn-core.js
|
|
525
651
|
var MillirecCharCnnCore2 = class extends MillirecCharCnnCore {
|
|
526
652
|
/** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
|
|
527
|
-
* { classes, stages, kernel }, plus assetBase (default: this module's folder).
|
|
653
|
+
* { classes, stages, kernel }, plus assetBase (default: this module's folder).
|
|
654
|
+
* A string is the option's name: create('fast') = create({ option: 'fast' }). */
|
|
528
655
|
static async create(opts = {}) {
|
|
529
656
|
return super.create(await withOption(opts));
|
|
530
657
|
}
|
package/dist/char-cnn/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { AttachOptions, Pad } from '../char-lite/index.js'
|
|
2
|
-
import { MillirecCharCnnCore, type CreateOptions } from './core.js'
|
|
2
|
+
import { MillirecCharCnnCore, type CreateOptions, type OptionName } from './core.js'
|
|
3
3
|
|
|
4
|
+
export type { ResultDetail, ClearDetail } from '../char-lite/index.js'
|
|
4
5
|
export { OPTIONS, loadOption, type OptionName, type CnnConfig, type Stage, type CreateOptions } from './core.js'
|
|
5
6
|
|
|
6
7
|
export declare class MillirecCharCnn extends MillirecCharCnnCore {
|
|
7
|
-
static create(opts?: CreateOptions): Promise<MillirecCharCnn>
|
|
8
|
+
static create(opts?: CreateOptions | OptionName): Promise<MillirecCharCnn>
|
|
8
9
|
attach(el: HTMLElement, opts?: AttachOptions): Pad
|
|
9
10
|
}
|
package/dist/char-cnn/index.js
CHANGED
|
@@ -299,6 +299,33 @@ function assetBaseOf(base) {
|
|
|
299
299
|
return typeof location !== "undefined" ? new URL(s, location.href).href : s;
|
|
300
300
|
}
|
|
301
301
|
var asStrokes = (s) => Array.isArray(s) && s.length && !Array.isArray(s[0]) ? [s] : s;
|
|
302
|
+
var notServed = (url2, what, base, recognizer) => new Error(`millirec: ${url2} is not ${what} \u2014 the package files are not served at ${base}; copy node_modules/millirec/dist/${recognizer}/ and pass assetBase (see README "Serving the files")`);
|
|
303
|
+
async function fetchAsset(url2, { as = "json", what, base, recognizer }) {
|
|
304
|
+
const fail = (detail) => {
|
|
305
|
+
const e = notServed(url2, what, base, recognizer);
|
|
306
|
+
if (detail) e.message += ` (${detail})`;
|
|
307
|
+
return e;
|
|
308
|
+
};
|
|
309
|
+
let r;
|
|
310
|
+
try {
|
|
311
|
+
r = await fetch(url2);
|
|
312
|
+
} catch (e) {
|
|
313
|
+
throw fail(e && e.message);
|
|
314
|
+
}
|
|
315
|
+
const type = r.headers && r.headers.get("content-type") || "";
|
|
316
|
+
if (!r.ok) throw fail(`HTTP ${r.status}`);
|
|
317
|
+
if (as === "json") {
|
|
318
|
+
try {
|
|
319
|
+
return await r.json();
|
|
320
|
+
} catch (e) {
|
|
321
|
+
throw fail(/html/i.test(type) ? "an HTML page" : e && e.message);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const buf = await r.arrayBuffer();
|
|
325
|
+
const head = String.fromCharCode(...new Uint8Array(buf, 0, Math.min(buf.byteLength, 16))).trimStart().toLowerCase();
|
|
326
|
+
if (head.startsWith("<!doctype") || head.startsWith("<html")) throw fail("an HTML page");
|
|
327
|
+
return buf;
|
|
328
|
+
}
|
|
302
329
|
|
|
303
330
|
// ../../library/src/char-cnn-core.js
|
|
304
331
|
function scriptBase() {
|
|
@@ -306,17 +333,29 @@ function scriptBase() {
|
|
|
306
333
|
return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
|
|
307
334
|
}
|
|
308
335
|
var ASSET_BASE = scriptBase();
|
|
309
|
-
var fetchBuf = (
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
336
|
+
var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
|
|
337
|
+
var DESTROYED = "millirec: recognizer destroyed";
|
|
338
|
+
var makeCanvas = (n) => typeof document !== "undefined" && document.createElement ? Object.assign(document.createElement("canvas"), { width: n, height: n }) : typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(n, n) : null;
|
|
339
|
+
var TEACH_SHARPNESS = 18;
|
|
340
|
+
var unitOf = (e) => {
|
|
341
|
+
let n = 0;
|
|
342
|
+
for (let i = 0; i < e.length; i++) n += e[i] * e[i];
|
|
343
|
+
n = Math.sqrt(n) || 1;
|
|
344
|
+
const o = new Float32Array(e.length);
|
|
345
|
+
for (let i = 0; i < e.length; i++) o[i] = e[i] / n;
|
|
346
|
+
return o;
|
|
347
|
+
};
|
|
313
348
|
var MillirecCharCnnCore = class {
|
|
314
349
|
static async create(opts = {}) {
|
|
315
350
|
const g = typeof window !== "undefined" ? window : globalThis;
|
|
316
351
|
const config = { ...g.MILLIREC_CNN || {}, ...opts };
|
|
317
352
|
if (!config.stages || !config.stages.length) throw new Error("MillirecCharCnn: no stages configured");
|
|
353
|
+
if (typeof fetch === "undefined" || typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
|
|
354
|
+
throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
|
|
355
|
+
}
|
|
318
356
|
const inst = new this(config);
|
|
319
357
|
await inst.ready;
|
|
358
|
+
if (config.examples) await inst.teach(config.examples);
|
|
320
359
|
return inst;
|
|
321
360
|
}
|
|
322
361
|
constructor(config) {
|
|
@@ -324,26 +363,35 @@ var MillirecCharCnnCore = class {
|
|
|
324
363
|
this.base = assetBaseOf(config.assetBase || ASSET_BASE);
|
|
325
364
|
this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
|
|
326
365
|
this.stages = [];
|
|
327
|
-
this.off =
|
|
328
|
-
|
|
329
|
-
this.off.width = this.off.height = 140;
|
|
330
|
-
this.octx = this.off.getContext("2d", { willReadFrequently: true });
|
|
331
|
-
}
|
|
366
|
+
this.off = makeCanvas(140);
|
|
367
|
+
this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
|
|
332
368
|
this.ready = this.load();
|
|
333
369
|
}
|
|
334
370
|
async load() {
|
|
335
371
|
const { base, config } = this;
|
|
336
372
|
let factory = null;
|
|
337
|
-
if (config.kernel)
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
373
|
+
if (config.kernel) {
|
|
374
|
+
const url2 = base + config.kernel;
|
|
375
|
+
try {
|
|
376
|
+
factory = (await import(
|
|
377
|
+
/* @vite-ignore */
|
|
378
|
+
/* webpackIgnore: true */
|
|
379
|
+
url2
|
|
380
|
+
)).default;
|
|
381
|
+
} catch (e) {
|
|
382
|
+
const err = notServed(url2, "the char-cnn kernel", base, "char-cnn");
|
|
383
|
+
err.message += ` (${e && e.message})`;
|
|
384
|
+
throw err;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
341
387
|
for (const st of config.stages) {
|
|
342
|
-
const [modelBuf, headBuf] = await Promise.all([fetchBuf(base + st.model), fetchBuf(base + st.head)]);
|
|
388
|
+
const [modelBuf, headBuf] = await Promise.all([fetchBuf(base + st.model, base), fetchBuf(base + st.head, base)]);
|
|
389
|
+
if (this.destroyed) return;
|
|
343
390
|
const head = parseHead(headBuf);
|
|
344
391
|
const stage = { theta: st.theta == null ? null : st.theta, head, headNorm: st.headNorm !== false, size: 0 };
|
|
345
392
|
if (factory) {
|
|
346
393
|
const M = await factory();
|
|
394
|
+
if (this.destroyed) return;
|
|
347
395
|
const bytes = expandToInt8(new Uint8Array(modelBuf));
|
|
348
396
|
const ptr = M._malloc(bytes.byteLength);
|
|
349
397
|
M.HEAPU8.set(bytes, ptr);
|
|
@@ -376,6 +424,7 @@ var MillirecCharCnnCore = class {
|
|
|
376
424
|
* The canvas draw + readback is the dear part (~1 ms) and is shared by every
|
|
377
425
|
* stage of one recognize() call; only the resample differs per size. */
|
|
378
426
|
raster(strokes, size) {
|
|
427
|
+
if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
|
|
379
428
|
if (this._planeFor !== strokes) {
|
|
380
429
|
const W2 = drawStrokes(this.octx, strokes);
|
|
381
430
|
this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
|
|
@@ -394,9 +443,10 @@ var MillirecCharCnnCore = class {
|
|
|
394
443
|
strokes = asStrokes(strokes);
|
|
395
444
|
if (!strokes || !strokes.length) return null;
|
|
396
445
|
await this.ready;
|
|
397
|
-
if (!this.stages.length) return null;
|
|
446
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
398
447
|
this._planeFor = null;
|
|
399
448
|
const keep = only ? new Set(only) : null;
|
|
449
|
+
if (this.taught) return this.nearest(strokes, keep);
|
|
400
450
|
let z = null, used = 0;
|
|
401
451
|
for (let s = 0; s < this.stages.length; s++) {
|
|
402
452
|
const st = this.stages[s];
|
|
@@ -428,10 +478,67 @@ var MillirecCharCnnCore = class {
|
|
|
428
478
|
out.stage = used;
|
|
429
479
|
return out;
|
|
430
480
|
}
|
|
481
|
+
/** recognize() once teach() has run: the drawing against every taught example, in the last stage's feature space. */
|
|
482
|
+
nearest(strokes, keep) {
|
|
483
|
+
const last = this.stages.length - 1, st = this.stages[last];
|
|
484
|
+
const q = unitOf(st.embed(this.raster(strokes, st.size)));
|
|
485
|
+
const names = [...this.taught.keys()].filter((c) => !keep || keep.has(c));
|
|
486
|
+
const out = [];
|
|
487
|
+
if (names.length) {
|
|
488
|
+
const sim = names.map((c) => {
|
|
489
|
+
let b = -2;
|
|
490
|
+
for (const e of this.taught.get(c)) {
|
|
491
|
+
let d = 0;
|
|
492
|
+
for (let i = 0; i < q.length; i++) d += q[i] * e[i];
|
|
493
|
+
if (d > b) b = d;
|
|
494
|
+
}
|
|
495
|
+
return b;
|
|
496
|
+
});
|
|
497
|
+
const p = softmax(sim.map((v) => v * TEACH_SHARPNESS));
|
|
498
|
+
for (const i of [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3)) out.push({ char: names[i], prob: p[i] });
|
|
499
|
+
}
|
|
500
|
+
out.stage = last;
|
|
501
|
+
return out;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* TEACH: read symbols of your own from a few drawings of each, with no training. `examples` is
|
|
505
|
+
* `{ name: [drawing, …] }` — names are any strings (a letter, 'star', an emoji), a drawing is strokes as for
|
|
506
|
+
* recognize(). Each drawing is kept as what the last stage's network makes of it; from then on recognize() answers
|
|
507
|
+
* AMONG THE TAUGHT NAMES ONLY, by the nearest example. Three drawings of each is a good start, five is better;
|
|
508
|
+
* draw the variants people really use. `{ add: true }` keeps what was taught before (new drawings of a known name
|
|
509
|
+
* are added to it). Returns { classes, n } or null (nothing usable). forget() goes back to the shipped classes.
|
|
510
|
+
* Works best on an option whose last stage is the larger network (`accurate`, the default, or `fast`).
|
|
511
|
+
*/
|
|
512
|
+
async teach(examples, { add = false } = {}) {
|
|
513
|
+
await this.ready;
|
|
514
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
515
|
+
const taught = add && this.taught ? new Map([...this.taught].map(([c, list]) => [c, list.slice()])) : /* @__PURE__ */ new Map();
|
|
516
|
+
for (const [name, list] of Object.entries(examples || {})) {
|
|
517
|
+
for (const drawing of list || []) {
|
|
518
|
+
const strokes = asStrokes(drawing);
|
|
519
|
+
if (!strokes || !strokes.length) continue;
|
|
520
|
+
const e = unitOf(await this.embedding(strokes));
|
|
521
|
+
if (this.destroyed) return null;
|
|
522
|
+
if (!taught.has(name)) taught.set(name, []);
|
|
523
|
+
taught.get(name).push(e);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!taught.size) {
|
|
527
|
+
if (!add) this.taught = null;
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
this.taught = taught;
|
|
531
|
+
return { classes: [...taught.keys()], n: [...taught.values()].reduce((a, l) => a + l.length, 0) };
|
|
532
|
+
}
|
|
533
|
+
/** Drop everything teach() learned: recognize() reads the shipped classes again. */
|
|
534
|
+
forget() {
|
|
535
|
+
this.taught = null;
|
|
536
|
+
}
|
|
431
537
|
/** The last stage's (unit) embedding of drawn strokes — what the head reads. */
|
|
432
538
|
async embedding(strokes) {
|
|
433
539
|
strokes = asStrokes(strokes);
|
|
434
540
|
await this.ready;
|
|
541
|
+
if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
|
|
435
542
|
this._planeFor = null;
|
|
436
543
|
const st = this.stages[this.stages.length - 1];
|
|
437
544
|
return st.embed(this.raster(strokes, st.size));
|
|
@@ -439,13 +546,14 @@ var MillirecCharCnnCore = class {
|
|
|
439
546
|
/**
|
|
440
547
|
* PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
|
|
441
548
|
* (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
|
|
442
|
-
* a letter and many take it over
|
|
549
|
+
* a letter and many take it over. Plain
|
|
443
550
|
* gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
|
|
444
551
|
* personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
|
|
445
552
|
* Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
|
|
446
553
|
*/
|
|
447
554
|
async personalize(samples, { steps = 250, lr = 0.5, prior = 1 } = {}) {
|
|
448
555
|
await this.ready;
|
|
556
|
+
if (this.destroyed || !this.stages.length) return null;
|
|
449
557
|
const st = this.stages[this.stages.length - 1];
|
|
450
558
|
const H0 = st.head0 || (st.head0 = st.head);
|
|
451
559
|
const X = [], Y = [];
|
|
@@ -459,6 +567,7 @@ var MillirecCharCnnCore = class {
|
|
|
459
567
|
Y.push(c);
|
|
460
568
|
}
|
|
461
569
|
}
|
|
570
|
+
if (this.destroyed) return null;
|
|
462
571
|
if (!X.length) {
|
|
463
572
|
st.head = H0;
|
|
464
573
|
this.personal = null;
|
|
@@ -490,16 +599,18 @@ var MillirecCharCnnCore = class {
|
|
|
490
599
|
this.personal = { n, classes: [...new Set(Y)].map((c) => this.classes[c]).sort() };
|
|
491
600
|
return this.personal;
|
|
492
601
|
}
|
|
493
|
-
/** Let go of the models and kernels; recognize()
|
|
602
|
+
/** Let go of the models and kernels; recognize() and personalize() return null afterwards, embedding() throws. */
|
|
494
603
|
destroy() {
|
|
604
|
+
this.destroyed = true;
|
|
495
605
|
this.stages = [];
|
|
496
606
|
this.personal = null;
|
|
607
|
+
this.taught = null;
|
|
497
608
|
this._plane = null;
|
|
498
609
|
this._planeFor = null;
|
|
499
610
|
}
|
|
500
611
|
depersonalize() {
|
|
501
612
|
const st = this.stages[this.stages.length - 1];
|
|
502
|
-
if (st.head0) st.head = st.head0;
|
|
613
|
+
if (st && st.head0) st.head = st.head0;
|
|
503
614
|
this.personal = null;
|
|
504
615
|
}
|
|
505
616
|
};
|
|
@@ -978,17 +1089,32 @@ var MillirecCharCnn = class extends MillirecCharCnnCore {
|
|
|
978
1089
|
};
|
|
979
1090
|
if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnn;
|
|
980
1091
|
|
|
1092
|
+
// src/here.js
|
|
1093
|
+
var url = (import.meta.url || "").replace(/[?#].*$/, "");
|
|
1094
|
+
var HERE = url.slice(0, url.lastIndexOf("/") + 1);
|
|
1095
|
+
function nodeFileUrl(url2, spec) {
|
|
1096
|
+
const g = globalThis;
|
|
1097
|
+
const node = typeof g.process !== "undefined" && g.process.versions && g.process.versions.node && typeof g.Deno === "undefined" && typeof g.Bun === "undefined";
|
|
1098
|
+
if (url2.startsWith("file:") && node && typeof document === "undefined" && typeof WorkerGlobalScope === "undefined") {
|
|
1099
|
+
throw new Error(`millirec: cannot fetch ${url2} outside a browser \u2014 in Node, import the JSON instead: import x from '${spec}' with { type: 'json' }`);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
981
1103
|
// src/options.js
|
|
982
|
-
var
|
|
983
|
-
var OPTIONS = ["accurate", "fast", "small"];
|
|
1104
|
+
var OPTIONS = ["accurate", "fast", "small", "digits", "lowercase"];
|
|
984
1105
|
async function loadOption(name, { assetBase = HERE } = {}) {
|
|
985
1106
|
if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
|
|
986
|
-
const
|
|
987
|
-
const
|
|
988
|
-
|
|
989
|
-
return
|
|
1107
|
+
const base = assetBaseOf(assetBase);
|
|
1108
|
+
const url2 = new URL(`options/${name}.json`, base).href;
|
|
1109
|
+
nodeFileUrl(url2, `millirec/char-cnn/options/${name}.json`);
|
|
1110
|
+
return fetchAsset(url2, { what: `the ${name} CNN option`, base, recognizer: "char-cnn" });
|
|
990
1111
|
}
|
|
991
1112
|
async function withOption(opts) {
|
|
1113
|
+
if (typeof opts === "string") opts = { option: opts };
|
|
1114
|
+
opts = opts || {};
|
|
1115
|
+
if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
|
|
1116
|
+
throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
|
|
1117
|
+
}
|
|
992
1118
|
const assetBase = assetBaseOf(opts.assetBase || HERE);
|
|
993
1119
|
const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
|
|
994
1120
|
return { ...preset, ...opts, assetBase };
|
|
@@ -997,7 +1123,8 @@ async function withOption(opts) {
|
|
|
997
1123
|
// src/char-cnn.js
|
|
998
1124
|
var MillirecCharCnn2 = class extends MillirecCharCnn {
|
|
999
1125
|
/** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
|
|
1000
|
-
* { classes, stages, kernel }, plus assetBase (default: this module's folder).
|
|
1126
|
+
* { classes, stages, kernel }, plus assetBase (default: this module's folder).
|
|
1127
|
+
* A string is the option's name: create('fast') = create({ option: 'fast' }). */
|
|
1001
1128
|
static async create(opts = {}) {
|
|
1002
1129
|
return super.create(await withOption(opts));
|
|
1003
1130
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";(()=>{var H={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function O(s){let n=new DataView(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),o=0;return{u32:()=>{let t=n.getUint32(o,!0);return o+=4,t},f32s:t=>{let r=new Float32Array(t);for(let f=0;f<t;f++)r[f]=n.getFloat32(o,!0),o+=4;return r},i8s:t=>{let r=new Int8Array(t);for(let f=0;f<t;f++)r[f]=l[o+f]<<24>>24;return o+=t,r},ibits:(t,r)=>{if(r===8){let i=new Int8Array(t);for(let e=0;e<t;e++)i[e]=l[o+e]<<24>>24;return o+=t,i}let f=new Int8Array(t),d=(1<<r)-1,u=1<<r-1;for(let i=0;i<t;i++){let e=i*r,c=o+(e>>3),a=e&7,w=(l[c]|l[c+1]<<8)>>a&d;f[i]=w&u?w-(1<<r):w}return o+=t*r+7>>3,f},str:t=>{let r=String.fromCharCode(...l.subarray(o,o+t));return o+=t,r},done:()=>o>=n.byteLength}}function T(s,n=8){let l=s.u32(),o=s.u32(),t=s.f32s(l),r=s.f32s(l),f=s.ibits(l*o,n),d=new Float32Array(l*o);for(let u=0;u<l;u++)for(let i=0;i<o;i++)d[u*o+i]=f[u*o+i]*t[u];return{out:l,in:o,w:d,bias:r}}function X(s){let n=O(s),l=n.str(6),o=H[l];if(!o)throw new Error("not an MRCNN1/3/4/5/6 model");let t=n.u32();if(t!==1)throw new Error("unknown model version "+t);let r=n.u32(),f=n.u32(),d=n.u32(),u=[];for(let e=0;e<d;e++){let c=n.u32(),a=n.u32(),w=n.u32(),x=n.u32(),y=n.f32s(c),m=n.f32s(c),A=n.ibits(c*a*w*w,o),I=new Float32Array(A.length),M=a*w*w;for(let p=0;p<c;p++)for(let b=0;b<M;b++)I[p*M+b]=A[p*M+b]*y[p];u.push({cout:c,cin:a,k:w,pool:x,w:I,bias:m})}let i=T(n,o);return{size:r,dim:f,convs:u,lin:i,bits:o}}function Y(s){let n=new Uint8Array(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=String.fromCharCode(...n.subarray(0,6)),o=H[l];if(!o)throw new Error("not an MRCNN model");if(o===8)return n;let t=O(s);t.str(6);let r=t.u32(),f=t.u32(),d=t.u32(),u=t.u32(),i=[],e=p=>{let b=new Uint8Array(4);return new DataView(b.buffer).setUint32(0,p,!0),b},c=p=>new Uint8Array(p.buffer,p.byteOffset,p.byteLength);i.push(new TextEncoder().encode("MRCNN1"),e(r),e(f),e(d),e(u));for(let p=0;p<u;p++){let b=t.u32(),h=t.u32(),g=t.u32(),_=t.u32(),k=t.f32s(b),N=t.f32s(b),F=t.ibits(b*h*g*g,o);i.push(e(b),e(h),e(g),e(_),c(k),c(N),new Uint8Array(F.buffer,F.byteOffset,F.length))}let a=t.u32(),w=t.u32(),x=t.f32s(a),y=t.f32s(a),m=t.ibits(a*w,o);i.push(e(a),e(w),c(x),c(y),new Uint8Array(m.buffer,m.byteOffset,m.length));let A=i.reduce((p,b)=>p+b.length,0),I=new Uint8Array(A),M=0;for(let p of i)I.set(p,M),M+=p.length;return I}function q(s){return T(O(s))}function nt(s,n,l,o){let{cout:t,cin:r,k:f,w:d,bias:u}=s,i=l*l,e=f-1>>1,c=r*f*f;for(let a=0;a<t;a++){let w=a*i,x=u[a],y=a*c;for(let m=0;m<l;m++)for(let A=0;A<l;A++){let I=x;for(let M=0;M<r;M++){let p=M*i,b=y+M*f*f;for(let h=0;h<f;h++){let g=m+h-e;if(g<0||g>=l)continue;let _=p+g*l,k=b+h*f;for(let N=0;N<f;N++){let F=A+N-e;F<0||F>=l||(I+=d[k+N]*n[_+F])}}}o[w+m*l+A]=I>0?I:0}}}function et(s,n,l,o){let t=n>>1;for(let r=0;r<l;r++){let f=r*n*n,d=r*t*t;for(let u=0;u<t;u++)for(let i=0;i<t;i++){let e=f+2*u*n+2*i,c=s[e],a=s[e+1],w=s[e+n],x=s[e+n+1];o[d+u*t+i]=Math.max(c,a,w,x)}}}function W(s,n){let l=s.size,o=Float32Array.from(n),t=l,r=1;for(let e of s.convs){let c=new Float32Array(e.cout*t*t);if(nt(e,o,t,c),o=c,r=e.cout,e.pool){let a=new Float32Array(r*(t>>1)*(t>>1));et(o,t,r,a),o=a,t>>=1}}let f=t*t,d=new Float32Array(r);for(let e=0;e<r;e++){let c=0;for(let a=0;a<f;a++)c+=o[e*f+a];d[e]=c/f}let{lin:u}=s,i=new Float32Array(u.out);for(let e=0;e<u.out;e++){let c=u.bias[e];for(let a=0;a<u.in;a++)c+=u.w[e*u.in+a]*d[a];i[e]=c}return i}function K(s){let n=0;for(let o=0;o<s.length;o++)n+=s[o]*s[o];n=1/(Math.sqrt(n)+1e-9);let l=new Float32Array(s.length);for(let o=0;o<s.length;o++)l[o]=s[o]*n;return l}function C(s,n){let l=new Float32Array(s.out);for(let o=0;o<s.out;o++){let t=s.bias[o];for(let r=0;r<s.in;r++){let f=n[r];f>0&&(t+=s.w[o*s.in+r]*f)}l[o]=t}return l}function B(s){let n=-1/0;for(let t of s)t>n&&(n=t);let l=new Float32Array(s.length),o=0;for(let t=0;t<s.length;t++)l[t]=Math.exp(s[t]-n),o+=l[t];for(let t=0;t<s.length;t++)l[t]/=o;return l}function Z(s,n,l=120,o=7,t=10){let r=1/0,f=1/0,d=-1/0,u=-1/0;for(let c of n)for(let a of c)a.x<r&&(r=a.x),a.x>d&&(d=a.x),a.y<f&&(f=a.y),a.y>u&&(u=a.y);let i=l/Math.max(d-r,u-f,1),e=l+2*t;s.fillStyle="#fff",s.fillRect(0,0,e,e),s.strokeStyle="#000",s.lineWidth=o,s.lineCap="round",s.lineJoin="round";for(let c of n)if(!(c.length<2)){s.beginPath(),s.moveTo((c[0].x-r)*i+t,(c[0].y-f)*i+t);for(let a=1;a<c.length;a++)s.lineTo((c[a].x-r)*i+t,(c[a].y-f)*i+t);s.stroke()}return e}function $(s,n,l){let o=new Float32Array(n*l);for(let t=0,r=0;t<o.length;t++,r+=4)o[t]=1-(s[r]*.299+s[r+1]*.587+s[r+2]*.114)/255;return o}function V(s,n,l,o=32,t=24){let r=1/0,f=-1,d=1/0,u=-1;for(let h=0;h<l;h++)for(let g=0;g<n;g++)s[h*n+g]>.15&&(g<r&&(r=g),g>f&&(f=g),h<d&&(d=h),h>u&&(u=h));let i=new Float32Array(o*o);if(f<0)return i;let e=f-r+1,c=u-d+1,a=new Float32Array(e*c);for(let h=0;h<c;h++)for(let g=0;g<e;g++)a[h*e+g]=s[(h+d)*n+(g+r)];let w=0;for(let h=0;h<a.length;h++)a[h]>w&&(w=a[h]);if(w>.05&&w<.999)for(let h=0;h<a.length;h++)a[h]=Math.min(1,a[h]/w);let x=t/Math.max(c,e),y=Math.max(1,Math.round(c*x)),m=Math.max(1,Math.round(e*x)),A=new Float32Array(m*y),I=e/m,M=c/y;for(let h=0;h<y;h++){let g=h*M,_=(h+1)*M,k=Math.floor(g),N=Math.min(c,Math.ceil(_));for(let F=0;F<m;F++){let z=F*I,j=(F+1)*I,Q=Math.floor(z),tt=Math.min(e,Math.ceil(j)),D=0,L=0;for(let S=k;S<N;S++){let R=Math.min(_,S+1)-Math.max(g,S);if(!(R<=0))for(let E=Q;E<tt;E++){let U=Math.min(j,E+1)-Math.max(z,E);U<=0||(D+=a[S*e+E]*U*R,L+=U*R)}}A[h*m+F]=L>0?D/L:0}}let p=o-y>>1,b=o-m>>1;for(let h=0;h<y;h++)for(let g=0;g<m;g++)i[(h+p)*o+g+b]=A[h*m+g];return i}function J(s){let n=s?String(s):"";return n?(n.endsWith("/")||(n+="/"),typeof location<"u"?new URL(n,location.href).href:n):typeof location<"u"?new URL("./",location.href).href:""}var v=s=>Array.isArray(s)&&s.length&&!Array.isArray(s[0])?[s]:s;function ot(){let s=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return s?s.slice(0,s.lastIndexOf("/")+1):""}var st=ot(),G=s=>fetch(s).then(n=>{if(!n.ok)throw new Error(`${s}: ${n.status}`);return n.arrayBuffer()}),P=class{static async create(n={}){let o={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...n};if(!o.stages||!o.stages.length)throw new Error("MillirecCharCnn: no stages configured");let t=new this(o);return await t.ready,t}constructor(n){this.config=n,this.base=J(n.assetBase||st),this.classes=typeof n.classes=="string"?n.classes.split(""):n.classes,this.stages=[],this.off=typeof document<"u"?document.createElement("canvas"):null,this.off&&(this.off.width=this.off.height=140,this.octx=this.off.getContext("2d",{willReadFrequently:!0})),this.ready=this.load()}async load(){let{base:n,config:l}=this,o=null;l.kernel&&(o=(await import(n+l.kernel)).default);for(let t of l.stages){let[r,f]=await Promise.all([G(n+t.model),G(n+t.head)]),d=q(f),u={theta:t.theta==null?null:t.theta,head:d,headNorm:t.headNorm!==!1,size:0};if(o){let i=await o(),e=Y(new Uint8Array(r)),c=i._malloc(e.byteLength);i.HEAPU8.set(e,c);let a=i._cnnLoad(c,e.byteLength);if(i._free(c),!a)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let w=i._cnnSize(),x=i._cnnDim();u.size=w,u.embed=y=>{let m=i._cnnInputPtr(1)>>2;i.HEAPF32.set(y,m),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+x)},u.logits=y=>C(u.head,u.embed(y))}else{let i=X(r);u.size=i.size,u.embed=e=>{let c=W(i,e);return u.headNorm?K(c):c},u.logits=e=>C(u.head,u.embed(e))}this.stages.push(u)}}raster(n,l){if(this._planeFor!==n){let r=Z(this.octx,n);this._plane={ink:$(this.octx.getImageData(0,0,r,r).data,r,r),W:r},this._planeFor=n}let{ink:o,W:t}=this._plane;return V(o,t,t,l,Math.round(l*.75))}async recognize(n,{only:l=null}={}){if(n=v(n),!n||!n.length||(await this.ready,!this.stages.length))return null;this._planeFor=null;let o=l?new Set(l):null,t=null,r=0;for(let e=0;e<this.stages.length;e++){let c=this.stages[e];if((this.personal||o)&&e<this.stages.length-1)continue;if(t=c.logits(this.raster(n,c.size)),r=e,c.theta==null||e===this.stages.length-1)break;let a=-1/0,w=-1/0;for(let x of t)x>a?(w=a,a=x):x>w&&(w=x);if(a-w>=c.theta)break}let f=[...t.keys()];if(o&&(f=f.filter(e=>o.has(this.classes[e])),!f.length)){let e=[];return e.stage=r,e}let d=B(f.map(e=>t[e])),i=[...d.keys()].sort((e,c)=>d[c]-d[e]).slice(0,3).map(e=>({char:this.classes[f[e]],prob:d[e]}));return i.stage=r,i}async embedding(n){n=v(n),await this.ready,this._planeFor=null;let l=this.stages[this.stages.length-1];return l.embed(this.raster(n,l.size))}async personalize(n,{steps:l=250,lr:o=.5,prior:t=1}={}){await this.ready;let r=this.stages[this.stages.length-1],f=r.head0||(r.head0=r.head),d=[],u=[];for(let[w,x]of Object.entries(n||{})){let y=this.classes.indexOf(w);if(!(y<0))for(let m of x||[]){let A=v(m);!A||!A.length||(d.push(await this.embedding(A)),u.push(y))}}if(!d.length)return r.head=f,this.personal=null,null;let i={in:f.in,out:f.out,w:Float32Array.from(f.w),bias:Float32Array.from(f.bias)},e=d.length,c=new Float32Array(i.w.length),a=new Float32Array(i.out);for(let w=0;w<l;w++){c.fill(0),a.fill(0);for(let y=0;y<e;y++){let m=B(C(i,d[y])),A=d[y];for(let I=0;I<i.out;I++){let M=(m[I]-(I===u[y]?1:0))/e;if(!M)continue;a[I]+=M;let p=I*i.in;for(let b=0;b<i.in;b++){let h=A[b];h>0&&(c[p+b]+=M*h)}}}let x=t/e;for(let y=0;y<i.w.length;y++)i.w[y]-=o*(c[y]+x*(i.w[y]-f.w[y]));for(let y=0;y<i.out;y++)i.bias[y]-=o*(a[y]+x*(i.bias[y]-f.bias[y]))}return r.head=i,this.personal={n:e,classes:[...new Set(u)].map(w=>this.classes[w]).sort()},this.personal}destroy(){this.stages=[],this.personal=null,this._plane=null,this._planeFor=null}depersonalize(){let n=this.stages[this.stages.length-1];n.head0&&(n.head=n.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=P);})();
|
|
1
|
+
"use strict";(()=>{var D={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function z(s){let n=new DataView(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),e=0;return{u32:()=>{let t=n.getUint32(e,!0);return e+=4,t},f32s:t=>{let r=new Float32Array(t);for(let a=0;a<t;a++)r[a]=n.getFloat32(e,!0),e+=4;return r},i8s:t=>{let r=new Int8Array(t);for(let a=0;a<t;a++)r[a]=l[e+a]<<24>>24;return e+=t,r},ibits:(t,r)=>{if(r===8){let i=new Int8Array(t);for(let o=0;o<t;o++)i[o]=l[e+o]<<24>>24;return e+=t,i}let a=new Int8Array(t),u=(1<<r)-1,h=1<<r-1;for(let i=0;i<t;i++){let o=i*r,c=e+(o>>3),f=o&7,d=(l[c]|l[c+1]<<8)>>f&u;a[i]=d&h?d-(1<<r):d}return e+=t*r+7>>3,a},str:t=>{let r=String.fromCharCode(...l.subarray(e,e+t));return e+=t,r},done:()=>e>=n.byteLength}}function q(s,n=8){let l=s.u32(),e=s.u32(),t=s.f32s(l),r=s.f32s(l),a=s.ibits(l*e,n),u=new Float32Array(l*e);for(let h=0;h<l;h++)for(let i=0;i<e;i++)u[h*e+i]=a[h*e+i]*t[h];return{out:l,in:e,w:u,bias:r}}function W(s){let n=z(s),l=n.str(6),e=D[l];if(!e)throw new Error("not an MRCNN1/3/4/5/6 model");let t=n.u32();if(t!==1)throw new Error("unknown model version "+t);let r=n.u32(),a=n.u32(),u=n.u32(),h=[];for(let o=0;o<u;o++){let c=n.u32(),f=n.u32(),d=n.u32(),m=n.u32(),g=n.f32s(c),b=n.f32s(c),A=n.ibits(c*f*d*d,e),M=new Float32Array(A.length),I=f*d*d;for(let p=0;p<c;p++)for(let x=0;x<I;x++)M[p*I+x]=A[p*I+x]*g[p];h.push({cout:c,cin:f,k:d,pool:m,w:M,bias:b})}let i=q(n,e);return{size:r,dim:a,convs:h,lin:i,bits:e}}function Y(s){let n=new Uint8Array(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=String.fromCharCode(...n.subarray(0,6)),e=D[l];if(!e)throw new Error("not an MRCNN model");if(e===8)return n;let t=z(s);t.str(6);let r=t.u32(),a=t.u32(),u=t.u32(),h=t.u32(),i=[],o=p=>{let x=new Uint8Array(4);return new DataView(x.buffer).setUint32(0,p,!0),x},c=p=>new Uint8Array(p.buffer,p.byteOffset,p.byteLength);i.push(new TextEncoder().encode("MRCNN1"),o(r),o(a),o(u),o(h));for(let p=0;p<h;p++){let x=t.u32(),y=t.u32(),w=t.u32(),k=t.u32(),N=t.f32s(x),S=t.f32s(x),F=t.ibits(x*y*w*w,e);i.push(o(x),o(y),o(w),o(k),c(N),c(S),new Uint8Array(F.buffer,F.byteOffset,F.length))}let f=t.u32(),d=t.u32(),m=t.f32s(f),g=t.f32s(f),b=t.ibits(f*d,e);i.push(o(f),o(d),c(m),c(g),new Uint8Array(b.buffer,b.byteOffset,b.length));let A=i.reduce((p,x)=>p+x.length,0),M=new Uint8Array(A),I=0;for(let p of i)M.set(p,I),I+=p.length;return M}function $(s){return q(z(s))}function st(s,n,l,e){let{cout:t,cin:r,k:a,w:u,bias:h}=s,i=l*l,o=a-1>>1,c=r*a*a;for(let f=0;f<t;f++){let d=f*i,m=h[f],g=f*c;for(let b=0;b<l;b++)for(let A=0;A<l;A++){let M=m;for(let I=0;I<r;I++){let p=I*i,x=g+I*a*a;for(let y=0;y<a;y++){let w=b+y-o;if(w<0||w>=l)continue;let k=p+w*l,N=x+y*a;for(let S=0;S<a;S++){let F=A+S-o;F<0||F>=l||(M+=u[N+S]*n[k+F])}}}e[d+b*l+A]=M>0?M:0}}}function rt(s,n,l,e){let t=n>>1;for(let r=0;r<l;r++){let a=r*n*n,u=r*t*t;for(let h=0;h<t;h++)for(let i=0;i<t;i++){let o=a+2*h*n+2*i,c=s[o],f=s[o+1],d=s[o+n],m=s[o+n+1];e[u+h*t+i]=Math.max(c,f,d,m)}}}function X(s,n){let l=s.size,e=Float32Array.from(n),t=l,r=1;for(let o of s.convs){let c=new Float32Array(o.cout*t*t);if(st(o,e,t,c),e=c,r=o.cout,o.pool){let f=new Float32Array(r*(t>>1)*(t>>1));rt(e,t,r,f),e=f,t>>=1}}let a=t*t,u=new Float32Array(r);for(let o=0;o<r;o++){let c=0;for(let f=0;f<a;f++)c+=e[o*a+f];u[o]=c/a}let{lin:h}=s,i=new Float32Array(h.out);for(let o=0;o<h.out;o++){let c=h.bias[o];for(let f=0;f<h.in;f++)c+=h.w[o*h.in+f]*u[f];i[o]=c}return i}function K(s){let n=0;for(let e=0;e<s.length;e++)n+=s[e]*s[e];n=1/(Math.sqrt(n)+1e-9);let l=new Float32Array(s.length);for(let e=0;e<s.length;e++)l[e]=s[e]*n;return l}function _(s,n){let l=new Float32Array(s.out);for(let e=0;e<s.out;e++){let t=s.bias[e];for(let r=0;r<s.in;r++){let a=n[r];a>0&&(t+=s.w[e*s.in+r]*a)}l[e]=t}return l}function L(s){let n=-1/0;for(let t of s)t>n&&(n=t);let l=new Float32Array(s.length),e=0;for(let t=0;t<s.length;t++)l[t]=Math.exp(s[t]-n),e+=l[t];for(let t=0;t<s.length;t++)l[t]/=e;return l}function Z(s,n,l=120,e=7,t=10){let r=1/0,a=1/0,u=-1/0,h=-1/0;for(let c of n)for(let f of c)f.x<r&&(r=f.x),f.x>u&&(u=f.x),f.y<a&&(a=f.y),f.y>h&&(h=f.y);let i=l/Math.max(u-r,h-a,1),o=l+2*t;s.fillStyle="#fff",s.fillRect(0,0,o,o),s.strokeStyle="#000",s.lineWidth=e,s.lineCap="round",s.lineJoin="round";for(let c of n)if(!(c.length<2)){s.beginPath(),s.moveTo((c[0].x-r)*i+t,(c[0].y-a)*i+t);for(let f=1;f<c.length;f++)s.lineTo((c[f].x-r)*i+t,(c[f].y-a)*i+t);s.stroke()}return o}function V(s,n,l){let e=new Float32Array(n*l);for(let t=0,r=0;t<e.length;t++,r+=4)e[t]=1-(s[r]*.299+s[r+1]*.587+s[r+2]*.114)/255;return e}function J(s,n,l,e=32,t=24){let r=1/0,a=-1,u=1/0,h=-1;for(let y=0;y<l;y++)for(let w=0;w<n;w++)s[y*n+w]>.15&&(w<r&&(r=w),w>a&&(a=w),y<u&&(u=y),y>h&&(h=y));let i=new Float32Array(e*e);if(a<0)return i;let o=a-r+1,c=h-u+1,f=new Float32Array(o*c);for(let y=0;y<c;y++)for(let w=0;w<o;w++)f[y*o+w]=s[(y+u)*n+(w+r)];let d=0;for(let y=0;y<f.length;y++)f[y]>d&&(d=f[y]);if(d>.05&&d<.999)for(let y=0;y<f.length;y++)f[y]=Math.min(1,f[y]/d);let m=t/Math.max(c,o),g=Math.max(1,Math.round(c*m)),b=Math.max(1,Math.round(o*m)),A=new Float32Array(b*g),M=o/b,I=c/g;for(let y=0;y<g;y++){let w=y*I,k=(y+1)*I,N=Math.floor(w),S=Math.min(c,Math.ceil(k));for(let F=0;F<b;F++){let B=F*M,H=(F+1)*M,nt=Math.floor(B),ot=Math.min(o,Math.ceil(H)),P=0,O=0;for(let v=N;v<S;v++){let R=Math.min(k,v+1)-Math.max(w,v);if(!(R<=0))for(let E=nt;E<ot;E++){let U=Math.min(H,E+1)-Math.max(B,E);U<=0||(P+=f[v*o+E]*U*R,O+=U*R)}}A[y*b+F]=O>0?P/O:0}}let p=e-g>>1,x=e-b>>1;for(let y=0;y<g;y++)for(let w=0;w<b;w++)i[(y+p)*e+w+x]=A[y*b+w];return i}function G(s){let n=s?String(s):"";return n?(n.endsWith("/")||(n+="/"),typeof location<"u"?new URL(n,location.href).href:n):typeof location<"u"?new URL("./",location.href).href:""}var C=s=>Array.isArray(s)&&s.length&&!Array.isArray(s[0])?[s]:s,T=(s,n,l,e)=>new Error(`millirec: ${s} is not ${n} \u2014 the package files are not served at ${l}; copy node_modules/millirec/dist/${e}/ and pass assetBase (see README "Serving the files")`);async function Q(s,{as:n="json",what:l,base:e,recognizer:t}){let r=o=>{let c=T(s,l,e,t);return o&&(c.message+=` (${o})`),c},a;try{a=await fetch(s)}catch(o){throw r(o&&o.message)}let u=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw r(`HTTP ${a.status}`);if(n==="json")try{return await a.json()}catch(o){throw r(/html/i.test(u)?"an HTML page":o&&o.message)}let h=await a.arrayBuffer(),i=String.fromCharCode(...new Uint8Array(h,0,Math.min(h.byteLength,16))).trimStart().toLowerCase();if(i.startsWith("<!doctype")||i.startsWith("<html"))throw r("an HTML page");return h}function it(){let s=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return s?s.slice(0,s.lastIndexOf("/")+1):""}var at=it(),tt=(s,n)=>Q(s,{as:"buf",what:"a char-cnn model file",base:n,recognizer:"char-cnn"}),lt="millirec: recognizer destroyed",ct=s=>typeof document<"u"&&document.createElement?Object.assign(document.createElement("canvas"),{width:s,height:s}):typeof OffscreenCanvas<"u"?new OffscreenCanvas(s,s):null,ft=18,et=s=>{let n=0;for(let e=0;e<s.length;e++)n+=s[e]*s[e];n=Math.sqrt(n)||1;let l=new Float32Array(s.length);for(let e=0;e<s.length;e++)l[e]=s[e]/n;return l},j=class{static async create(n={}){let e={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...n};if(!e.stages||!e.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");let t=new this(e);return await t.ready,e.examples&&await t.teach(e.examples),t}constructor(n){this.config=n,this.base=G(n.assetBase||at),this.classes=typeof n.classes=="string"?n.classes.split(""):n.classes,this.stages=[],this.off=ct(140),this.octx=this.off?this.off.getContext("2d",{willReadFrequently:!0}):null,this.ready=this.load()}async load(){let{base:n,config:l}=this,e=null;if(l.kernel){let t=n+l.kernel;try{e=(await import(t)).default}catch(r){let a=T(t,"the char-cnn kernel",n,"char-cnn");throw a.message+=` (${r&&r.message})`,a}}for(let t of l.stages){let[r,a]=await Promise.all([tt(n+t.model,n),tt(n+t.head,n)]);if(this.destroyed)return;let u=$(a),h={theta:t.theta==null?null:t.theta,head:u,headNorm:t.headNorm!==!1,size:0};if(e){let i=await e();if(this.destroyed)return;let o=Y(new Uint8Array(r)),c=i._malloc(o.byteLength);i.HEAPU8.set(o,c);let f=i._cnnLoad(c,o.byteLength);if(i._free(c),!f)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let d=i._cnnSize(),m=i._cnnDim();h.size=d,h.embed=g=>{let b=i._cnnInputPtr(1)>>2;i.HEAPF32.set(g,b),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+m)},h.logits=g=>_(h.head,h.embed(g))}else{let i=W(r);h.size=i.size,h.embed=o=>{let c=X(i,o);return h.headNorm?K(c):c},h.logits=o=>_(h.head,h.embed(o))}this.stages.push(h)}}raster(n,l){if(!this.octx)throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");if(this._planeFor!==n){let r=Z(this.octx,n);this._plane={ink:V(this.octx.getImageData(0,0,r,r).data,r,r),W:r},this._planeFor=n}let{ink:e,W:t}=this._plane;return J(e,t,t,l,Math.round(l*.75))}async recognize(n,{only:l=null}={}){if(n=C(n),!n||!n.length||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let e=l?new Set(l):null;if(this.taught)return this.nearest(n,e);let t=null,r=0;for(let o=0;o<this.stages.length;o++){let c=this.stages[o];if((this.personal||e)&&o<this.stages.length-1)continue;if(t=c.logits(this.raster(n,c.size)),r=o,c.theta==null||o===this.stages.length-1)break;let f=-1/0,d=-1/0;for(let m of t)m>f?(d=f,f=m):m>d&&(d=m);if(f-d>=c.theta)break}let a=[...t.keys()];if(e&&(a=a.filter(o=>e.has(this.classes[o])),!a.length)){let o=[];return o.stage=r,o}let u=L(a.map(o=>t[o])),i=[...u.keys()].sort((o,c)=>u[c]-u[o]).slice(0,3).map(o=>({char:this.classes[a[o]],prob:u[o]}));return i.stage=r,i}nearest(n,l){let e=this.stages.length-1,t=this.stages[e],r=et(t.embed(this.raster(n,t.size))),a=[...this.taught.keys()].filter(h=>!l||l.has(h)),u=[];if(a.length){let h=a.map(o=>{let c=-2;for(let f of this.taught.get(o)){let d=0;for(let m=0;m<r.length;m++)d+=r[m]*f[m];d>c&&(c=d)}return c}),i=L(h.map(o=>o*ft));for(let o of[...i.keys()].sort((c,f)=>i[f]-i[c]).slice(0,3))u.push({char:a[o],prob:i[o]})}return u.stage=e,u}async teach(n,{add:l=!1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let e=l&&this.taught?new Map([...this.taught].map(([t,r])=>[t,r.slice()])):new Map;for(let[t,r]of Object.entries(n||{}))for(let a of r||[]){let u=C(a);if(!u||!u.length)continue;let h=et(await this.embedding(u));if(this.destroyed)return null;e.has(t)||e.set(t,[]),e.get(t).push(h)}return e.size?(this.taught=e,{classes:[...e.keys()],n:[...e.values()].reduce((t,r)=>t+r.length,0)}):(l||(this.taught=null),null)}forget(){this.taught=null}async embedding(n){if(n=C(n),await this.ready,this.destroyed||!this.stages.length)throw new Error(lt);this._planeFor=null;let l=this.stages[this.stages.length-1];return l.embed(this.raster(n,l.size))}async personalize(n,{steps:l=250,lr:e=.5,prior:t=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let r=this.stages[this.stages.length-1],a=r.head0||(r.head0=r.head),u=[],h=[];for(let[d,m]of Object.entries(n||{})){let g=this.classes.indexOf(d);if(!(g<0))for(let b of m||[]){let A=C(b);!A||!A.length||(u.push(await this.embedding(A)),h.push(g))}}if(this.destroyed)return null;if(!u.length)return r.head=a,this.personal=null,null;let i={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},o=u.length,c=new Float32Array(i.w.length),f=new Float32Array(i.out);for(let d=0;d<l;d++){c.fill(0),f.fill(0);for(let g=0;g<o;g++){let b=L(_(i,u[g])),A=u[g];for(let M=0;M<i.out;M++){let I=(b[M]-(M===h[g]?1:0))/o;if(!I)continue;f[M]+=I;let p=M*i.in;for(let x=0;x<i.in;x++){let y=A[x];y>0&&(c[p+x]+=I*y)}}}let m=t/o;for(let g=0;g<i.w.length;g++)i.w[g]-=e*(c[g]+m*(i.w[g]-a.w[g]));for(let g=0;g<i.out;g++)i.bias[g]-=e*(f[g]+m*(i.bias[g]-a.bias[g]))}return r.head=i,this.personal={n:o,classes:[...new Set(h)].map(d=>this.classes[d]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this.taught=null,this._plane=null,this._planeFor=null}depersonalize(){let n=this.stages[this.stages.length-1];n&&n.head0&&(n.head=n.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=j);})();
|