@hackbuild/ai-views 0.1.2 → 0.1.5
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/README.md +3 -3
- package/dist/chat.d.ts +35 -2
- package/dist/chat.js +47 -3
- package/dist/elements.d.ts +3 -1
- package/dist/elements.js +2 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -1
- package/dist/loaders.js +40 -7
- package/dist/mount-mask.d.ts +56 -6
- package/dist/mount-mask.js +334 -36
- package/dist/mount-talk.d.ts +92 -0
- package/dist/mount-talk.js +502 -0
- package/dist/mount-transcribe.d.ts +22 -0
- package/dist/mount-transcribe.js +90 -4
- package/dist/pick.d.ts +23 -0
- package/dist/pick.js +51 -0
- package/dist/single/elements.iife.js +1784 -627
- package/dist/single/elements.js +1774 -628
- package/dist/single/index.iife.js +1478 -56
- package/dist/single/index.js +1473 -57
- package/dist/single/vue.iife.js +1720 -622
- package/dist/single/vue.js +1714 -623
- package/dist/speaker.d.ts +55 -0
- package/dist/speaker.js +109 -0
- package/dist/talk.d.ts +89 -0
- package/dist/talk.js +121 -0
- package/dist/transcribe.d.ts +36 -1
- package/dist/transcribe.js +19 -3
- package/dist/vue.d.ts +137 -5
- package/dist/vue.js +80 -9
- package/llms.md +376 -14
- package/package.json +5 -5
package/dist/single/index.js
CHANGED
|
@@ -2984,6 +2984,36 @@ var init_pins = __esmMin((() => {
|
|
|
2984
2984
|
527503840,
|
|
2985
2985
|
"vendor"
|
|
2986
2986
|
] }
|
|
2987
|
+
},
|
|
2988
|
+
"Felladrin/gguf-sharded-UD-Q4_K_XL-Phi-4-mini-reasoning": {
|
|
2989
|
+
"revision": "bc6bac3a27893ab006486992e46a7662ef4790a9",
|
|
2990
|
+
"files": {
|
|
2991
|
+
"model.shard-00001-of-00005.gguf": [
|
|
2992
|
+
"e730254967e049ab3bd5e2311cedd200155b9d0835fb05a9218de82b324cac3f",
|
|
2993
|
+
512426912,
|
|
2994
|
+
"vendor"
|
|
2995
|
+
],
|
|
2996
|
+
"model.shard-00002-of-00005.gguf": [
|
|
2997
|
+
"812f190b06a786ec7ed22378bb5a136476f5c040a434140916cceb5563ab7f4d",
|
|
2998
|
+
481483520,
|
|
2999
|
+
"vendor"
|
|
3000
|
+
],
|
|
3001
|
+
"model.shard-00003-of-00005.gguf": [
|
|
3002
|
+
"f5b575dc8a4fd7d75dc4616261a050a224a2e81783e5616e74d2cd022b89d658",
|
|
3003
|
+
499301472,
|
|
3004
|
+
"vendor"
|
|
3005
|
+
],
|
|
3006
|
+
"model.shard-00004-of-00005.gguf": [
|
|
3007
|
+
"edba060f8bf871dc4953ff633a0b1e0de7d312cd05ef3a5a449b69c9d2e53765",
|
|
3008
|
+
495664224,
|
|
3009
|
+
"vendor"
|
|
3010
|
+
],
|
|
3011
|
+
"model.shard-00005-of-00005.gguf": [
|
|
3012
|
+
"da2110debf2334e102413017e3ad9285f6a5475aeb5a2d3b85b03e5865df5cab",
|
|
3013
|
+
473803392,
|
|
3014
|
+
"vendor"
|
|
3015
|
+
]
|
|
3016
|
+
}
|
|
2987
3017
|
}
|
|
2988
3018
|
};
|
|
2989
3019
|
}));
|
|
@@ -4059,6 +4089,42 @@ var init_catalog = __esmMin((() => {
|
|
|
4059
4089
|
commercial: true,
|
|
4060
4090
|
runtime: "@mlc-ai/web-llm@0.2.84"
|
|
4061
4091
|
},
|
|
4092
|
+
{
|
|
4093
|
+
id: "phi-4-mini-reasoning-gguf",
|
|
4094
|
+
label: "Phi-4 mini reasoning",
|
|
4095
|
+
params: "3.8B",
|
|
4096
|
+
quant: "Q4_K_XL",
|
|
4097
|
+
censorship: "stock",
|
|
4098
|
+
tier: "medium",
|
|
4099
|
+
family: "phi",
|
|
4100
|
+
tags: ["think", "reasoning"],
|
|
4101
|
+
note: "phi-4 mini tuned for maths with a think trace, unsloth dynamic q4. five 500 MB shards, so it loads past the 2 GB file cap.",
|
|
4102
|
+
group: "reasoning",
|
|
4103
|
+
badge: "maths, thinks first",
|
|
4104
|
+
speed: "slow",
|
|
4105
|
+
verdict: "maths, thinks first",
|
|
4106
|
+
provides: ["chat"],
|
|
4107
|
+
provider: "wllama",
|
|
4108
|
+
source: {
|
|
4109
|
+
repo: "Felladrin/gguf-sharded-UD-Q4_K_XL-Phi-4-mini-reasoning",
|
|
4110
|
+
file: "model.shard-00001-of-00005.gguf"
|
|
4111
|
+
},
|
|
4112
|
+
sizeMB: 2349,
|
|
4113
|
+
sizeSource: "vendor",
|
|
4114
|
+
ctx: 4096,
|
|
4115
|
+
ladder: [{
|
|
4116
|
+
device: "webgpu",
|
|
4117
|
+
dtype: "q4_k_m"
|
|
4118
|
+
}, {
|
|
4119
|
+
device: "wasm",
|
|
4120
|
+
dtype: "q4_k_m"
|
|
4121
|
+
}],
|
|
4122
|
+
quirks: { think: "tags" },
|
|
4123
|
+
licence: "MIT (the base model card microsoft/Phi-4-mini-reasoning; the shard repo has no licence field)",
|
|
4124
|
+
commercial: true,
|
|
4125
|
+
runtime: "@wllama/wllama@3.8.1",
|
|
4126
|
+
notes: ["no webllm or transformers.js build on the hub as of 2026-11-25: the microsoft onnx export is an onnxruntime-genai layout, so the gguf shards are the browser path"]
|
|
4127
|
+
},
|
|
4062
4128
|
{
|
|
4063
4129
|
id: "smollm2-1.7b-instruct-mlc",
|
|
4064
4130
|
label: "SmolLM2 1.7B",
|
|
@@ -7450,9 +7516,47 @@ var words;
|
|
|
7450
7516
|
var init_embed = __esmMin((() => {
|
|
7451
7517
|
words = (t) => t.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [];
|
|
7452
7518
|
}));
|
|
7519
|
+
// packages/memory/src/assemble.ts
|
|
7520
|
+
/**
|
|
7521
|
+
* Fixed lines always go in; ranked sections join highest priority first
|
|
7522
|
+
* while they fit the budget (equal priorities keep their given order). A
|
|
7523
|
+
* section that does not fit is skipped and smaller ones below it may still
|
|
7524
|
+
* join.
|
|
7525
|
+
*/
|
|
7526
|
+
function budget(opts) {
|
|
7527
|
+
const est = opts.estimate ?? estimateTokens;
|
|
7528
|
+
const fixed = (opts.fixed ?? []).filter((s) => s.length > 0);
|
|
7529
|
+
let used = fixed.reduce((n, s) => n + est(s), 0);
|
|
7530
|
+
const kept = [];
|
|
7531
|
+
const dropped = [];
|
|
7532
|
+
const ranked = opts.sections.map((s, i) => ({
|
|
7533
|
+
s,
|
|
7534
|
+
i
|
|
7535
|
+
})).filter((x) => x.s.text.length > 0).sort((a, b) => b.s.priority - a.s.priority || a.i - b.i);
|
|
7536
|
+
for (const { s } of ranked) {
|
|
7537
|
+
const t = est(s.text);
|
|
7538
|
+
if (used + t <= opts.budgetTokens) {
|
|
7539
|
+
used += t;
|
|
7540
|
+
kept.push(s);
|
|
7541
|
+
} else dropped.push(s.name);
|
|
7542
|
+
}
|
|
7543
|
+
return {
|
|
7544
|
+
text: [...fixed, ...kept.map((s) => s.text)].join(opts.joiner ?? "\n\n"),
|
|
7545
|
+
kept: kept.map((s) => s.name),
|
|
7546
|
+
dropped,
|
|
7547
|
+
usedTokens: used,
|
|
7548
|
+
budgetTokens: opts.budgetTokens
|
|
7549
|
+
};
|
|
7550
|
+
}
|
|
7551
|
+
var CHARS_PER_TOKEN, estimateTokens;
|
|
7552
|
+
var init_assemble = __esmMin((() => {
|
|
7553
|
+
CHARS_PER_TOKEN = 3.6;
|
|
7554
|
+
estimateTokens = (s) => Math.ceil(s.length / CHARS_PER_TOKEN);
|
|
7555
|
+
}));
|
|
7453
7556
|
// packages/memory/src/index.ts
|
|
7454
7557
|
var init_src$8 = __esmMin((() => {
|
|
7455
7558
|
init_embed();
|
|
7559
|
+
init_assemble();
|
|
7456
7560
|
}));
|
|
7457
7561
|
// packages/embed/src/index.ts
|
|
7458
7562
|
var src_exports$6 = /* @__PURE__ */ __exportAll({
|
|
@@ -7818,17 +7922,17 @@ var init_records$3 = __esmMin((() => {
|
|
|
7818
7922
|
label: "sdxl-turbo 512",
|
|
7819
7923
|
speed: "steady",
|
|
7820
7924
|
verdict: "not run yet",
|
|
7821
|
-
unrunnable: "not run yet. sdxl
|
|
7925
|
+
unrunnable: "not run yet. the sdxl pipeline is written; the export's graph names and fp16 io are inferred and unchecked.",
|
|
7822
7926
|
group: "good",
|
|
7823
7927
|
provides: ["imagegen"],
|
|
7824
7928
|
provider: "ort",
|
|
7825
7929
|
source: {
|
|
7826
7930
|
repo: "webnn/sdxl-turbo",
|
|
7827
7931
|
files: [
|
|
7828
|
-
{ path: "
|
|
7829
|
-
{ path: "
|
|
7830
|
-
{ path: "
|
|
7831
|
-
{ path: "
|
|
7932
|
+
{ path: "onnx/text_encoder_model_q4f16.onnx" },
|
|
7933
|
+
{ path: "onnx/text_encoder_2_model_q4f16.onnx" },
|
|
7934
|
+
{ path: "onnx/unet_model_q4f16.onnx" },
|
|
7935
|
+
{ path: "onnx/vae_decoder_model_q4f16.onnx" }
|
|
7832
7936
|
]
|
|
7833
7937
|
},
|
|
7834
7938
|
sizeMB: 2548,
|
|
@@ -7842,7 +7946,11 @@ var init_records$3 = __esmMin((() => {
|
|
|
7842
7946
|
commercial: "unknown",
|
|
7843
7947
|
place: "gpu",
|
|
7844
7948
|
runtime: "onnxruntime-web@1.27.0",
|
|
7845
|
-
notes: [
|
|
7949
|
+
notes: [
|
|
7950
|
+
"two text encoders, pooled embeddings and time ids, on generateXl",
|
|
7951
|
+
"fp16 inputs and outputs per the export's README (--force_fp16_inputs); timestep dtype and hidden state names inferred",
|
|
7952
|
+
"the highest quality of the three image models"
|
|
7953
|
+
],
|
|
7846
7954
|
form: {
|
|
7847
7955
|
steps: {
|
|
7848
7956
|
min: 1,
|
|
@@ -7855,27 +7963,256 @@ var init_records$3 = __esmMin((() => {
|
|
|
7855
7963
|
spacing: "trailing",
|
|
7856
7964
|
note: "distilled from sdxl. 1 to 4 steps, no negative prompt. the best looking of the three.",
|
|
7857
7965
|
tokenizer: "Xenova/clip-vit-base-patch16",
|
|
7858
|
-
graphs
|
|
7966
|
+
graphs: {
|
|
7967
|
+
textEncoder: "text_encoder_model_q4f16",
|
|
7968
|
+
textEncoder2: "text_encoder_2_model_q4f16",
|
|
7969
|
+
unet: "unet_model_q4f16",
|
|
7970
|
+
vaeDecoder: "vae_decoder_model_q4f16"
|
|
7971
|
+
},
|
|
7972
|
+
pipeline: "sdxl",
|
|
7973
|
+
xl: {
|
|
7974
|
+
io: "float16",
|
|
7975
|
+
timestep: "float16"
|
|
7976
|
+
}
|
|
7859
7977
|
}
|
|
7860
7978
|
}];
|
|
7861
7979
|
}));
|
|
7980
|
+
// packages/imagegen/src/xl.ts
|
|
7981
|
+
/** Float32 to IEEE 754 half bits, round to nearest even. */
|
|
7982
|
+
function toHalf(f) {
|
|
7983
|
+
const out = new Uint16Array(f.length);
|
|
7984
|
+
const b = /* @__PURE__ */ new Float32Array(1);
|
|
7985
|
+
const u = new Uint32Array(b.buffer);
|
|
7986
|
+
for (let i = 0; i < f.length; i++) {
|
|
7987
|
+
b[0] = f[i];
|
|
7988
|
+
const x = u[0];
|
|
7989
|
+
const sign = x >>> 16 & 32768;
|
|
7990
|
+
const exp = x >>> 23 & 255;
|
|
7991
|
+
let mant = x & 8388607;
|
|
7992
|
+
if (exp === 255) {
|
|
7993
|
+
out[i] = sign | 31744 | (mant ? 512 : 0);
|
|
7994
|
+
continue;
|
|
7995
|
+
}
|
|
7996
|
+
const e = exp - 127 + 15;
|
|
7997
|
+
if (e >= 31) {
|
|
7998
|
+
out[i] = sign | 31744;
|
|
7999
|
+
continue;
|
|
8000
|
+
}
|
|
8001
|
+
if (e <= 0) {
|
|
8002
|
+
if (e < -10) {
|
|
8003
|
+
out[i] = sign;
|
|
8004
|
+
continue;
|
|
8005
|
+
}
|
|
8006
|
+
mant |= 8388608;
|
|
8007
|
+
const shift = 14 - e;
|
|
8008
|
+
let h = mant >>> shift;
|
|
8009
|
+
const rem = mant & (1 << shift) - 1;
|
|
8010
|
+
const half = 1 << shift - 1;
|
|
8011
|
+
if (rem > half || rem === half && h & 1) h++;
|
|
8012
|
+
out[i] = sign | h;
|
|
8013
|
+
continue;
|
|
8014
|
+
}
|
|
8015
|
+
let h = e << 10 | mant >>> 13;
|
|
8016
|
+
const rem = mant & 8191;
|
|
8017
|
+
if (rem > 4096 || rem === 4096 && h & 1) h++;
|
|
8018
|
+
out[i] = sign | h;
|
|
8019
|
+
}
|
|
8020
|
+
return out;
|
|
8021
|
+
}
|
|
8022
|
+
/** IEEE 754 half bits to float32. */
|
|
8023
|
+
function fromHalf(h) {
|
|
8024
|
+
const out = new Float32Array(h.length);
|
|
8025
|
+
for (let i = 0; i < h.length; i++) {
|
|
8026
|
+
const v = h[i];
|
|
8027
|
+
const s = v & 32768 ? -1 : 1;
|
|
8028
|
+
const e = v >>> 10 & 31;
|
|
8029
|
+
const m = v & 1023;
|
|
8030
|
+
out[i] = e === 0 ? s * m * 2 ** -24 : e === 31 ? m ? NaN : s * Infinity : s * (1 + m / 1024) * 2 ** (e - 15);
|
|
8031
|
+
}
|
|
8032
|
+
return out;
|
|
8033
|
+
}
|
|
8034
|
+
/** An output's floats; fp16 outputs (type "float16" or Uint16Array data) are decoded. */
|
|
8035
|
+
function floatsOf(t) {
|
|
8036
|
+
if (t.type === "float16" || t.data instanceof Uint16Array) return fromHalf(t.data);
|
|
8037
|
+
return Float32Array.from(t.data);
|
|
8038
|
+
}
|
|
8039
|
+
/** CLIP ids for the second encoder: up to the first end token, then `pad` to 77. */
|
|
8040
|
+
function padIds2(ids, pad = 0) {
|
|
8041
|
+
const list = Array.from(ids);
|
|
8042
|
+
const end = list.indexOf(EOT);
|
|
8043
|
+
return padIds(end >= 0 ? list.slice(0, end + 1) : list, 77, pad);
|
|
8044
|
+
}
|
|
8045
|
+
function pick(outs, name, fallback, what) {
|
|
8046
|
+
for (const n of [name, ...fallback]) if (outs[n]) return outs[n];
|
|
8047
|
+
throw new Error(`${what}: no ${name} output; the graph gives ${Object.keys(outs).join(", ") || "none"}`);
|
|
8048
|
+
}
|
|
8049
|
+
/**
|
|
8050
|
+
* SDXL-Turbo text to image: both encoders, the joined hidden states and
|
|
8051
|
+
* pooled embedding, the seeded Euler loop with `text_embeds` and
|
|
8052
|
+
* `time_ids` on every UNet call, then the VAE. Guidance above 1 runs the
|
|
8053
|
+
* UNet on the negative too. Throws when `size` is not a multiple of 8.
|
|
8054
|
+
*/
|
|
8055
|
+
async function generateXl(ort, s, tokenize, o) {
|
|
8056
|
+
const io = {
|
|
8057
|
+
...XL_IO,
|
|
8058
|
+
...o.xl ?? {}
|
|
8059
|
+
};
|
|
8060
|
+
const now = o.now ?? (() => globalThis.performance ? performance.now() : Date.now());
|
|
8061
|
+
const run = o.run ?? ((fn) => fn());
|
|
8062
|
+
const steps = Math.max(1, Math.min(50, o.steps ?? 1));
|
|
8063
|
+
const guidance = o.guidance ?? 1;
|
|
8064
|
+
const seed = (o.seed ?? 0) >>> 0;
|
|
8065
|
+
const size = o.size ?? 512;
|
|
8066
|
+
if (size % 8) throw new Error("size must be a multiple of 8");
|
|
8067
|
+
const lw = size / 8;
|
|
8068
|
+
const n = 4 * lw * lw;
|
|
8069
|
+
const ms = {
|
|
8070
|
+
text: 0,
|
|
8071
|
+
unet: 0,
|
|
8072
|
+
vae: 0
|
|
8073
|
+
};
|
|
8074
|
+
const T = ort.Tensor;
|
|
8075
|
+
const float = (data, dims) => io.io === "float16" ? new T("float16", toHalf(data), dims) : new T("float32", data, dims);
|
|
8076
|
+
let t = now();
|
|
8077
|
+
const encode = async (text) => {
|
|
8078
|
+
const ids = await tokenize(text.length ? text : " ");
|
|
8079
|
+
const a = await run(() => s.textEncoder.run({ input_ids: new ort.Tensor("int32", padIds(ids), [1, 77]) }));
|
|
8080
|
+
const b = await run(() => s.textEncoder2.run({ input_ids: new ort.Tensor("int32", padIds2(ids, io.pad2), [1, 77]) }));
|
|
8081
|
+
const h1 = floatsOf(pick(a, io.hidden1, ["last_hidden_state"], "text encoder"));
|
|
8082
|
+
const h2 = floatsOf(pick(b, io.hidden2, ["last_hidden_state"], "text encoder 2"));
|
|
8083
|
+
const pooled = floatsOf(pick(b, io.pooled, ["pooler_output"], "text encoder 2"));
|
|
8084
|
+
const w1 = h1.length / 77;
|
|
8085
|
+
const w2 = h2.length / 77;
|
|
8086
|
+
const joined = new Float32Array(77 * (w1 + w2));
|
|
8087
|
+
for (let i = 0; i < 77; i++) {
|
|
8088
|
+
joined.set(h1.subarray(i * w1, (i + 1) * w1), i * (w1 + w2));
|
|
8089
|
+
joined.set(h2.subarray(i * w2, (i + 1) * w2), i * (w1 + w2) + w1);
|
|
8090
|
+
}
|
|
8091
|
+
return {
|
|
8092
|
+
hidden: float(joined, [
|
|
8093
|
+
1,
|
|
8094
|
+
77,
|
|
8095
|
+
w1 + w2
|
|
8096
|
+
]),
|
|
8097
|
+
pooled: float(pooled, [1, pooled.length])
|
|
8098
|
+
};
|
|
8099
|
+
};
|
|
8100
|
+
const cond = await encode(o.prompt);
|
|
8101
|
+
const uncond = guidance > 1 ? await encode(o.negative ?? "") : null;
|
|
8102
|
+
ms.text = now() - t;
|
|
8103
|
+
o.onStep?.({
|
|
8104
|
+
stage: "text",
|
|
8105
|
+
step: 0,
|
|
8106
|
+
steps,
|
|
8107
|
+
ms: ms.text
|
|
8108
|
+
});
|
|
8109
|
+
const timeIds = float(Float32Array.from([
|
|
8110
|
+
size,
|
|
8111
|
+
size,
|
|
8112
|
+
0,
|
|
8113
|
+
0,
|
|
8114
|
+
size,
|
|
8115
|
+
size
|
|
8116
|
+
]), [1, 6]);
|
|
8117
|
+
const stepTensor = (ts) => io.timestep === "int64" ? new ort.Tensor("int64", BigInt64Array.from([BigInt(ts)]), [1]) : float(Float32Array.from([ts]), [1]);
|
|
8118
|
+
const plan = eulerPlan(steps, o.spacing ?? "trailing");
|
|
8119
|
+
const randn = gaussian(o.rng ?? mulberry32(seed));
|
|
8120
|
+
const latent = new Float32Array(n);
|
|
8121
|
+
const s0 = plan.sigmas[0];
|
|
8122
|
+
const init = o.spacing === "leading" ? Math.sqrt(s0 * s0 + 1) : s0;
|
|
8123
|
+
for (let i = 0; i < n; i++) latent[i] = randn() * init;
|
|
8124
|
+
const final = await euler(latent, plan, async (scaled, timestep, step) => {
|
|
8125
|
+
const t0 = now();
|
|
8126
|
+
const unet = async (c) => {
|
|
8127
|
+
const u = await run(() => s.unet.run({
|
|
8128
|
+
sample: float(scaled, [
|
|
8129
|
+
1,
|
|
8130
|
+
4,
|
|
8131
|
+
lw,
|
|
8132
|
+
lw
|
|
8133
|
+
]),
|
|
8134
|
+
timestep: stepTensor(timestep),
|
|
8135
|
+
encoder_hidden_states: c.hidden,
|
|
8136
|
+
text_embeds: c.pooled,
|
|
8137
|
+
time_ids: timeIds
|
|
8138
|
+
}));
|
|
8139
|
+
return floatsOf(u.out_sample ?? Object.values(u)[0]);
|
|
8140
|
+
};
|
|
8141
|
+
const c = await unet(cond);
|
|
8142
|
+
let e = c;
|
|
8143
|
+
if (uncond) {
|
|
8144
|
+
const u = await unet(uncond);
|
|
8145
|
+
e = new Float32Array(c.length);
|
|
8146
|
+
for (let i = 0; i < e.length; i++) e[i] = u[i] + guidance * (c[i] - u[i]);
|
|
8147
|
+
}
|
|
8148
|
+
const d = now() - t0;
|
|
8149
|
+
ms.unet += d;
|
|
8150
|
+
o.onStep?.({
|
|
8151
|
+
stage: "denoise",
|
|
8152
|
+
step: step + 1,
|
|
8153
|
+
steps,
|
|
8154
|
+
ms: d
|
|
8155
|
+
});
|
|
8156
|
+
return e;
|
|
8157
|
+
}, o.signal);
|
|
8158
|
+
t = now();
|
|
8159
|
+
const vin = new Float32Array(n);
|
|
8160
|
+
for (let i = 0; i < n; i++) vin[i] = final[i] / io.vaeScale;
|
|
8161
|
+
const v = await run(() => s.vaeDecoder.run({ latent_sample: float(vin, [
|
|
8162
|
+
1,
|
|
8163
|
+
4,
|
|
8164
|
+
lw,
|
|
8165
|
+
lw
|
|
8166
|
+
]) }));
|
|
8167
|
+
ms.vae = now() - t;
|
|
8168
|
+
o.onStep?.({
|
|
8169
|
+
stage: "decode",
|
|
8170
|
+
step: steps,
|
|
8171
|
+
steps,
|
|
8172
|
+
ms: ms.vae
|
|
8173
|
+
});
|
|
8174
|
+
return {
|
|
8175
|
+
width: size,
|
|
8176
|
+
height: size,
|
|
8177
|
+
rgba: toRgba(floatsOf(v.sample ?? Object.values(v)[0]), size, size),
|
|
8178
|
+
seed,
|
|
8179
|
+
ms
|
|
8180
|
+
};
|
|
8181
|
+
}
|
|
8182
|
+
var XL_IO, EOT;
|
|
8183
|
+
var init_xl = __esmMin((() => {
|
|
8184
|
+
init_pipeline();
|
|
8185
|
+
init_schedule();
|
|
8186
|
+
XL_IO = {
|
|
8187
|
+
io: "float32",
|
|
8188
|
+
timestep: "int64",
|
|
8189
|
+
hidden1: "hidden_states.11",
|
|
8190
|
+
hidden2: "hidden_states.31",
|
|
8191
|
+
pooled: "text_embeds",
|
|
8192
|
+
pad2: 0,
|
|
8193
|
+
vaeScale: .13025
|
|
8194
|
+
};
|
|
8195
|
+
EOT = 49407;
|
|
8196
|
+
}));
|
|
7862
8197
|
// packages/imagegen/src/hub.ts
|
|
7863
8198
|
/** Add the image records to the hub's registry. */
|
|
7864
8199
|
function attachImagegen(hub) {
|
|
7865
8200
|
hub.registry.add(...IMAGEGEN_RECORDS);
|
|
7866
8201
|
}
|
|
7867
|
-
/** The
|
|
8202
|
+
/** The sessions of a loaded model, by the form's graph names: three, or four with SDXL's second text encoder. */
|
|
7868
8203
|
function sessionsOf(m, form) {
|
|
7869
8204
|
const pick = (k) => {
|
|
7870
8205
|
const s = m.sessions[k];
|
|
7871
8206
|
if (!s) throw new Error(`the loaded model has no ${k} graph; it has ${Object.keys(m.sessions).join(", ") || "none"}`);
|
|
7872
8207
|
return s;
|
|
7873
8208
|
};
|
|
7874
|
-
|
|
8209
|
+
const out = {
|
|
7875
8210
|
textEncoder: pick(form.graphs.textEncoder),
|
|
7876
8211
|
unet: pick(form.graphs.unet),
|
|
7877
8212
|
vaeDecoder: pick(form.graphs.vaeDecoder)
|
|
7878
8213
|
};
|
|
8214
|
+
if (form.pipeline === "sdxl") out.textEncoder2 = pick(form.graphs.textEncoder2 ?? "text_encoder_2");
|
|
8215
|
+
return out;
|
|
7879
8216
|
}
|
|
7880
8217
|
/** Clamp a request to what the form allows, with the form's defaults. */
|
|
7881
8218
|
function applyForm(form, o) {
|
|
@@ -7896,9 +8233,15 @@ function applyForm(form, o) {
|
|
|
7896
8233
|
/**
|
|
7897
8234
|
* Load an image model through the hub: a record id (`{ id: "sd-1.5" }`)
|
|
7898
8235
|
* or the capability ("imagegen", which the hub routes, or sends to the
|
|
7899
|
-
* stub in stub mode). A record
|
|
8236
|
+
* stub in stub mode). A picker record named by id (`sdxl-turbo`) is added
|
|
8237
|
+
* to the registry first. A record without a form (the stub) gets
|
|
8238
|
+
* SD-Turbo's. An SDXL form runs `generateXl` behind the same interface.
|
|
7900
8239
|
*/
|
|
7901
8240
|
async function loadImagegen(hub, what = "imagegen", o = {}) {
|
|
8241
|
+
if (typeof what === "object" && !hub.registry.get(what.id)) {
|
|
8242
|
+
const listed = IMAGEGEN_PICKER_RECORDS.find((r) => r.id === what.id);
|
|
8243
|
+
if (listed) hub.registry.add(listed);
|
|
8244
|
+
}
|
|
7902
8245
|
const loaded = await hub.use(what, o);
|
|
7903
8246
|
const form = formOf(loaded.record) ?? IMAGEGEN_RECORDS[0].form;
|
|
7904
8247
|
let sessions;
|
|
@@ -7913,16 +8256,21 @@ async function loadImagegen(hub, what = "imagegen", o = {}) {
|
|
|
7913
8256
|
record: loaded.record,
|
|
7914
8257
|
form,
|
|
7915
8258
|
sessions,
|
|
7916
|
-
generate(tokenize, req) {
|
|
8259
|
+
async generate(tokenize, req) {
|
|
7917
8260
|
const { runOptions, ...rest } = req;
|
|
7918
8261
|
const run = (fn) => hub.run(loaded, () => fn(), {
|
|
7919
8262
|
...runOptions ?? {},
|
|
7920
8263
|
...rest.signal ? { signal: rest.signal } : {}
|
|
7921
8264
|
});
|
|
7922
|
-
|
|
8265
|
+
const opts = {
|
|
7923
8266
|
...applyForm(form, rest),
|
|
7924
8267
|
run
|
|
7925
|
-
}
|
|
8268
|
+
};
|
|
8269
|
+
if (form.pipeline === "sdxl") return generateXl(loaded.value.ort, sessions, tokenize, form.xl ? {
|
|
8270
|
+
...opts,
|
|
8271
|
+
xl: form.xl
|
|
8272
|
+
} : opts);
|
|
8273
|
+
return generate(loaded.value.ort, sessions, tokenize, opts);
|
|
7926
8274
|
},
|
|
7927
8275
|
release() {
|
|
7928
8276
|
hub.release(loaded);
|
|
@@ -7969,6 +8317,7 @@ var FormError;
|
|
|
7969
8317
|
var init_hub = __esmMin((() => {
|
|
7970
8318
|
init_pipeline();
|
|
7971
8319
|
init_records$3();
|
|
8320
|
+
init_xl();
|
|
7972
8321
|
FormError = class extends Error {
|
|
7973
8322
|
name = "FormError";
|
|
7974
8323
|
};
|
|
@@ -7979,21 +8328,27 @@ var src_exports$5 = /* @__PURE__ */ __exportAll({
|
|
|
7979
8328
|
IMAGEGEN_PICKER_RECORDS: () => IMAGEGEN_PICKER_RECORDS,
|
|
7980
8329
|
IMAGEGEN_RECORDS: () => IMAGEGEN_RECORDS,
|
|
7981
8330
|
VAE_SCALE: () => VAE_SCALE,
|
|
8331
|
+
XL_IO: () => XL_IO,
|
|
7982
8332
|
applyForm: () => applyForm,
|
|
7983
8333
|
attachImagegen: () => attachImagegen,
|
|
7984
8334
|
euler: () => euler,
|
|
7985
8335
|
eulerPlan: () => eulerPlan,
|
|
8336
|
+
floatsOf: () => floatsOf,
|
|
7986
8337
|
formOf: () => formOf,
|
|
8338
|
+
fromHalf: () => fromHalf,
|
|
7987
8339
|
gaussian: () => gaussian,
|
|
7988
8340
|
generate: () => generate,
|
|
8341
|
+
generateXl: () => generateXl,
|
|
7989
8342
|
leadingTimesteps: () => leadingTimesteps,
|
|
7990
8343
|
loadImagegen: () => loadImagegen,
|
|
7991
8344
|
loadLabel: () => loadLabel,
|
|
7992
8345
|
mulberry32: () => mulberry32,
|
|
7993
8346
|
padIds: () => padIds,
|
|
8347
|
+
padIds2: () => padIds2,
|
|
7994
8348
|
sessionsOf: () => sessionsOf,
|
|
7995
8349
|
sigmas: () => sigmas,
|
|
7996
8350
|
stubImagegenModel: () => stubImagegenModel,
|
|
8351
|
+
toHalf: () => toHalf,
|
|
7997
8352
|
toRgba: () => toRgba,
|
|
7998
8353
|
trailingTimesteps: () => trailingTimesteps
|
|
7999
8354
|
});
|
|
@@ -8002,6 +8357,7 @@ var init_src$6 = __esmMin((() => {
|
|
|
8002
8357
|
init_pipeline();
|
|
8003
8358
|
init_records$3();
|
|
8004
8359
|
init_hub();
|
|
8360
|
+
init_xl();
|
|
8005
8361
|
}));
|
|
8006
8362
|
// packages/speak/src/text.ts
|
|
8007
8363
|
/**
|
|
@@ -12331,7 +12687,7 @@ var init_records = __esmMin((() => {
|
|
|
12331
12687
|
licence: "AGPL-3.0 upstream (Ultralytics FastSAM); the export's card says apache-2.0",
|
|
12332
12688
|
commercial: false,
|
|
12333
12689
|
place: "cpu-worker",
|
|
12334
|
-
notes: ["finds every mask in one pass on onnxruntime-web
|
|
12690
|
+
notes: ["finds every mask in one pass on onnxruntime-web; the original page loaded a 93 MB build (vendor, its review)", "segment everything: prototype masks, nms, then a pick by click (segmentEverything, instanceAt)"]
|
|
12335
12691
|
},
|
|
12336
12692
|
{
|
|
12337
12693
|
id: "scrfd-10g",
|
|
@@ -12717,7 +13073,47 @@ function decodeYolo(out, dims, lb, src, o = {}) {
|
|
|
12717
13073
|
}
|
|
12718
13074
|
return nms(dets, o.iou ?? .45, o.max ?? 13);
|
|
12719
13075
|
}
|
|
12720
|
-
/**
|
|
13076
|
+
/**
|
|
13077
|
+
* Decode an end to end output, [1, boxes, 6] as x1, y1, x2, y2, score and
|
|
13078
|
+
* class in letterbox pixels (the YOLOv10 export layout, after the
|
|
13079
|
+
* model's own one to one selection), into boxes in source pixels. The
|
|
13080
|
+
* per class NMS pass still runs, so an export that leaves near duplicates
|
|
13081
|
+
* gives one box per object.
|
|
13082
|
+
*/
|
|
13083
|
+
function decodeYoloEnd2End(out, dims, lb, src, o = {}) {
|
|
13084
|
+
const n = dims[dims.length - 2];
|
|
13085
|
+
const stride = dims[dims.length - 1];
|
|
13086
|
+
const thr = o.scoreThreshold ?? .25;
|
|
13087
|
+
const labels = o.labels ?? COCO_LABELS;
|
|
13088
|
+
const dets = [];
|
|
13089
|
+
for (let a = 0; a < n; a++) {
|
|
13090
|
+
const r = a * stride;
|
|
13091
|
+
const score = out[r + 4];
|
|
13092
|
+
if (!(score > thr)) continue;
|
|
13093
|
+
const c = Math.round(out[r + 5]);
|
|
13094
|
+
const x0 = Math.max(0, (out[r] - lb.padX) / lb.scale);
|
|
13095
|
+
const y0 = Math.max(0, (out[r + 1] - lb.padY) / lb.scale);
|
|
13096
|
+
const x1 = Math.min(src.width, (out[r + 2] - lb.padX) / lb.scale);
|
|
13097
|
+
const y1 = Math.min(src.height, (out[r + 3] - lb.padY) / lb.scale);
|
|
13098
|
+
if (x1 <= x0 || y1 <= y0) continue;
|
|
13099
|
+
dets.push({
|
|
13100
|
+
label: labels[c] ?? String(c),
|
|
13101
|
+
score,
|
|
13102
|
+
box: {
|
|
13103
|
+
x: x0,
|
|
13104
|
+
y: y0,
|
|
13105
|
+
w: x1 - x0,
|
|
13106
|
+
h: y1 - y0
|
|
13107
|
+
}
|
|
13108
|
+
});
|
|
13109
|
+
}
|
|
13110
|
+
return nms(dets, o.iou ?? .45, o.max ?? 13);
|
|
13111
|
+
}
|
|
13112
|
+
/** True when an output is the end to end layout (last axis 6, boxes on the axis before it), false for the [4 + classes, anchors] layout. */
|
|
13113
|
+
function isEnd2End(dims) {
|
|
13114
|
+
return dims[dims.length - 1] === 6 && dims[dims.length - 2] > 6;
|
|
13115
|
+
}
|
|
13116
|
+
/** Wrap an ort model as a Detector; the output layout (YOLOv8 anchors or YOLOv10 end to end) is read from its dims. Images must be Pixels, or `toPixels` must turn them into Pixels. */
|
|
12721
13117
|
function yoloDetector(m, o = {}) {
|
|
12722
13118
|
const size = o.size ?? 640;
|
|
12723
13119
|
return {
|
|
@@ -12731,11 +13127,13 @@ function yoloDetector(m, o = {}) {
|
|
|
12731
13127
|
size
|
|
12732
13128
|
]) };
|
|
12733
13129
|
const t = (await m.session.run(feeds))[m.session.outputNames[0]];
|
|
12734
|
-
|
|
13130
|
+
const d = {
|
|
12735
13131
|
...opts.scoreThreshold !== void 0 ? { scoreThreshold: opts.scoreThreshold } : {},
|
|
12736
13132
|
...opts.maxResults !== void 0 ? { max: opts.maxResults } : {},
|
|
12737
13133
|
...o.labels ? { labels: o.labels } : {}
|
|
12738
|
-
}
|
|
13134
|
+
};
|
|
13135
|
+
const out = t.data;
|
|
13136
|
+
return isEnd2End(t.dims) ? decodeYoloEnd2End(out, t.dims, lb, px, d) : decodeYolo(out, t.dims, lb, px, d);
|
|
12739
13137
|
},
|
|
12740
13138
|
dispose: () => m.dispose()
|
|
12741
13139
|
};
|
|
@@ -12744,9 +13142,165 @@ function yoloDetector(m, o = {}) {
|
|
|
12744
13142
|
function detectCadenceMs(lastLatencyMs) {
|
|
12745
13143
|
return Math.max(450, 4 * lastLatencyMs);
|
|
12746
13144
|
}
|
|
12747
|
-
var COCO_LABELS;
|
|
13145
|
+
var COCO_LABELS, DETECT_SIZES;
|
|
12748
13146
|
var init_yolo = __esmMin((() => {
|
|
12749
13147
|
COCO_LABELS = "person,bicycle,car,motorcycle,airplane,bus,train,truck,boat,traffic light,fire hydrant,stop sign,parking meter,bench,bird,cat,dog,horse,sheep,cow,elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase,frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove,skateboard,surfboard,tennis racket,bottle,wine glass,cup,fork,knife,spoon,bowl,banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,chair,couch,potted plant,bed,dining table,toilet,tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster,sink,refrigerator,book,clock,vase,scissors,teddy bear,hair drier,toothbrush".split(",");
|
|
13148
|
+
DETECT_SIZES = {
|
|
13149
|
+
n: "yolov10n",
|
|
13150
|
+
s: "yolov10s",
|
|
13151
|
+
m: "yolov10m"
|
|
13152
|
+
};
|
|
13153
|
+
}));
|
|
13154
|
+
// packages/vision/src/fastsam.ts
|
|
13155
|
+
function agnosticNms(items, thr, max) {
|
|
13156
|
+
const sorted = [...items].sort((a, b) => b.score - a.score);
|
|
13157
|
+
const keep = [];
|
|
13158
|
+
for (const d of sorted) {
|
|
13159
|
+
if (keep.some((k) => iou(k.box, d.box) > thr)) continue;
|
|
13160
|
+
keep.push(d);
|
|
13161
|
+
if (keep.length >= max) break;
|
|
13162
|
+
}
|
|
13163
|
+
return keep;
|
|
13164
|
+
}
|
|
13165
|
+
/**
|
|
13166
|
+
* Decode the two FastSAM outputs into instances in source pixels, best
|
|
13167
|
+
* first: boxes, scores, areas and one mask each (255 inside, `score` on
|
|
13168
|
+
* the mask too).
|
|
13169
|
+
*/
|
|
13170
|
+
function decodeFastSam(out, dims, protos, protoDims, lb, src, o = {}) {
|
|
13171
|
+
const rows = dims[dims.length - 2];
|
|
13172
|
+
const n = dims[dims.length - 1];
|
|
13173
|
+
const k = protoDims[protoDims.length - 3];
|
|
13174
|
+
const ph = protoDims[protoDims.length - 2];
|
|
13175
|
+
const pw = protoDims[protoDims.length - 1];
|
|
13176
|
+
const classes = rows - 4 - k;
|
|
13177
|
+
if (classes < 1) throw new RangeError(`decodeFastSam: ${rows} rows leave no class score beside ${k} mask coefficients`);
|
|
13178
|
+
const conf = o.confidence ?? .4;
|
|
13179
|
+
const cands = [];
|
|
13180
|
+
for (let a = 0; a < n; a++) {
|
|
13181
|
+
let score = 0;
|
|
13182
|
+
for (let c = 0; c < classes; c++) score = Math.max(score, out[(4 + c) * n + a]);
|
|
13183
|
+
if (!(score > conf)) continue;
|
|
13184
|
+
const cx = out[a];
|
|
13185
|
+
const cy = out[n + a];
|
|
13186
|
+
const w = out[2 * n + a];
|
|
13187
|
+
const h = out[3 * n + a];
|
|
13188
|
+
const x0 = Math.max(0, (cx - w / 2 - lb.padX) / lb.scale);
|
|
13189
|
+
const y0 = Math.max(0, (cy - h / 2 - lb.padY) / lb.scale);
|
|
13190
|
+
const x1 = Math.min(src.width, (cx + w / 2 - lb.padX) / lb.scale);
|
|
13191
|
+
const y1 = Math.min(src.height, (cy + h / 2 - lb.padY) / lb.scale);
|
|
13192
|
+
if (x1 <= x0 || y1 <= y0) continue;
|
|
13193
|
+
cands.push({
|
|
13194
|
+
box: {
|
|
13195
|
+
x: x0,
|
|
13196
|
+
y: y0,
|
|
13197
|
+
w: x1 - x0,
|
|
13198
|
+
h: y1 - y0
|
|
13199
|
+
},
|
|
13200
|
+
score,
|
|
13201
|
+
a,
|
|
13202
|
+
lx: cx,
|
|
13203
|
+
ly: cy,
|
|
13204
|
+
lw: w,
|
|
13205
|
+
lh: h
|
|
13206
|
+
});
|
|
13207
|
+
}
|
|
13208
|
+
const kept = agnosticNms(cands, o.iou ?? .9, o.max ?? 100);
|
|
13209
|
+
const sig = o.sigmoid ?? true;
|
|
13210
|
+
const cut = o.maskThreshold ?? .5;
|
|
13211
|
+
const minArea = o.minArea ?? 64;
|
|
13212
|
+
const plane = ph * pw;
|
|
13213
|
+
const toProto = pw / lb.size;
|
|
13214
|
+
const out_ = [];
|
|
13215
|
+
const coef = new Float32Array(k);
|
|
13216
|
+
const low = new Float32Array(plane);
|
|
13217
|
+
for (const d of kept) {
|
|
13218
|
+
for (let j = 0; j < k; j++) coef[j] = out[(4 + classes + j) * n + d.a];
|
|
13219
|
+
const px0 = Math.max(0, Math.floor((d.lx - d.lw / 2) * toProto) - 1);
|
|
13220
|
+
const py0 = Math.max(0, Math.floor((d.ly - d.lh / 2) * toProto) - 1);
|
|
13221
|
+
const px1 = Math.min(pw - 1, Math.ceil((d.lx + d.lw / 2) * toProto) + 1);
|
|
13222
|
+
const py1 = Math.min(ph - 1, Math.ceil((d.ly + d.lh / 2) * toProto) + 1);
|
|
13223
|
+
for (let y = py0; y <= py1; y++) for (let x = px0; x <= px1; x++) {
|
|
13224
|
+
let v = 0;
|
|
13225
|
+
const i = y * pw + x;
|
|
13226
|
+
for (let j = 0; j < k; j++) v += coef[j] * protos[j * plane + i];
|
|
13227
|
+
low[i] = sig ? 1 / (1 + Math.exp(-v)) : v;
|
|
13228
|
+
}
|
|
13229
|
+
const data = new Uint8Array(src.width * src.height);
|
|
13230
|
+
const bx0 = Math.floor(d.box.x);
|
|
13231
|
+
const by0 = Math.floor(d.box.y);
|
|
13232
|
+
const bx1 = Math.min(src.width, Math.ceil(d.box.x + d.box.w));
|
|
13233
|
+
const by1 = Math.min(src.height, Math.ceil(d.box.y + d.box.h));
|
|
13234
|
+
let area = 0;
|
|
13235
|
+
for (let y = by0; y < by1; y++) {
|
|
13236
|
+
const fy = Math.min(py1, Math.max(py0, ((y + .5) * lb.scale + lb.padY) * toProto - .5));
|
|
13237
|
+
const y0 = Math.floor(fy);
|
|
13238
|
+
const y1 = Math.min(py1, y0 + 1);
|
|
13239
|
+
const ty = fy - y0;
|
|
13240
|
+
for (let x = bx0; x < bx1; x++) {
|
|
13241
|
+
const fx = Math.min(px1, Math.max(px0, ((x + .5) * lb.scale + lb.padX) * toProto - .5));
|
|
13242
|
+
const x0 = Math.floor(fx);
|
|
13243
|
+
const x1 = Math.min(px1, x0 + 1);
|
|
13244
|
+
const tx = fx - x0;
|
|
13245
|
+
const top = low[y0 * pw + x0] * (1 - tx) + low[y0 * pw + x1] * tx;
|
|
13246
|
+
const bot = low[y1 * pw + x0] * (1 - tx) + low[y1 * pw + x1] * tx;
|
|
13247
|
+
if (top * (1 - ty) + bot * ty > cut) {
|
|
13248
|
+
data[y * src.width + x] = 255;
|
|
13249
|
+
area++;
|
|
13250
|
+
}
|
|
13251
|
+
}
|
|
13252
|
+
}
|
|
13253
|
+
if (area < minArea) continue;
|
|
13254
|
+
out_.push({
|
|
13255
|
+
box: d.box,
|
|
13256
|
+
score: d.score,
|
|
13257
|
+
area,
|
|
13258
|
+
mask: {
|
|
13259
|
+
width: src.width,
|
|
13260
|
+
height: src.height,
|
|
13261
|
+
data,
|
|
13262
|
+
score: d.score
|
|
13263
|
+
}
|
|
13264
|
+
});
|
|
13265
|
+
}
|
|
13266
|
+
return out_;
|
|
13267
|
+
}
|
|
13268
|
+
/** Wrap a FastSAM ort model as an {@link EverythingSegmenter}. Outputs are told apart by rank: the 4D one holds the prototypes. Images must be Pixels, or `toPixels` must turn them into Pixels. */
|
|
13269
|
+
function fastSamSegmenter(m, o = {}) {
|
|
13270
|
+
const size = o.size ?? 640;
|
|
13271
|
+
return {
|
|
13272
|
+
async segmentAll(image, opts = {}) {
|
|
13273
|
+
const px = o.toPixels ? o.toPixels(image) : image;
|
|
13274
|
+
const { data, lb } = letterboxTensor(px, size);
|
|
13275
|
+
const res = await m.session.run({ [m.session.inputNames[0]]: new m.ort.Tensor("float32", data, [
|
|
13276
|
+
1,
|
|
13277
|
+
3,
|
|
13278
|
+
size,
|
|
13279
|
+
size
|
|
13280
|
+
]) });
|
|
13281
|
+
const outs = m.session.outputNames.map((nm) => res[nm]);
|
|
13282
|
+
const proto = outs.find((t) => t.dims.length === 4);
|
|
13283
|
+
const det = outs.find((t) => t.dims.length === 3);
|
|
13284
|
+
if (!proto || !det) throw new Error(`fastsam: expected a 3D box output and a 4D prototype output, got ${outs.map((t) => `[${t.dims.join(", ")}]`).join(" and ")}`);
|
|
13285
|
+
return decodeFastSam(det.data, det.dims, proto.data, proto.dims, lb, px, opts);
|
|
13286
|
+
},
|
|
13287
|
+
dispose: () => m.dispose()
|
|
13288
|
+
};
|
|
13289
|
+
}
|
|
13290
|
+
/** The smallest instance whose mask holds the pixel at x, y, for hover and click picks; undefined off every mask. */
|
|
13291
|
+
function instanceAt(instances, x, y) {
|
|
13292
|
+
let best;
|
|
13293
|
+
for (const inst of instances) {
|
|
13294
|
+
const m = inst.mask;
|
|
13295
|
+
const xi = Math.floor(x);
|
|
13296
|
+
const yi = Math.floor(y);
|
|
13297
|
+
if (xi < 0 || yi < 0 || xi >= m.width || yi >= m.height || m.data[yi * m.width + xi] < 128) continue;
|
|
13298
|
+
if (!best || inst.area < best.area) best = inst;
|
|
13299
|
+
}
|
|
13300
|
+
return best;
|
|
13301
|
+
}
|
|
13302
|
+
var init_fastsam = __esmMin((() => {
|
|
13303
|
+
init_yolo();
|
|
12750
13304
|
}));
|
|
12751
13305
|
// packages/vision/src/api.ts
|
|
12752
13306
|
/** Add the vision records and register the transformers.js loaders. MediaPipe and ort factories are provided by the app. */
|
|
@@ -12789,6 +13343,51 @@ function detect(hub, image, o = {}) {
|
|
|
12789
13343
|
if (o.timestamp !== void 0) opts.timestamp = o.timestamp;
|
|
12790
13344
|
return withModel(hub, target("detect", o), (v) => asDetector(v, o.toPixels).detect(image, opts), o);
|
|
12791
13345
|
}
|
|
13346
|
+
/** Add a picker record to the hub's registry when it is missing, so a call that names it by id can load it. */
|
|
13347
|
+
function ensureRecord(hub, id) {
|
|
13348
|
+
const have = hub.registry.get(id);
|
|
13349
|
+
if (have) return have;
|
|
13350
|
+
const rec = PICKER_RECORDS.find((r) => r.id === id);
|
|
13351
|
+
if (!rec) throw new Error(`${id} is neither registered nor a picker record`);
|
|
13352
|
+
hub.registry.add(rec);
|
|
13353
|
+
return rec;
|
|
13354
|
+
}
|
|
13355
|
+
/**
|
|
13356
|
+
* Objects at a chosen detector size: YOLOv10 nano, small or medium
|
|
13357
|
+
* ({@link DETECT_SIZES}). Picking a size is the opt in to the AGPL-3.0
|
|
13358
|
+
* Ultralytics weights: the record is added to the registry on first use.
|
|
13359
|
+
* Boxes pass a per class NMS.
|
|
13360
|
+
*/
|
|
13361
|
+
function detectAt(hub, image, size, o = {}) {
|
|
13362
|
+
const id = Object.hasOwn(DETECT_SIZES, size) ? DETECT_SIZES[size] : void 0;
|
|
13363
|
+
if (!id) return Promise.reject(/* @__PURE__ */ new RangeError(`detector size ${String(size)}: pick n, s or m`));
|
|
13364
|
+
ensureRecord(hub, id);
|
|
13365
|
+
return detect(hub, image, {
|
|
13366
|
+
...o,
|
|
13367
|
+
model: id
|
|
13368
|
+
});
|
|
13369
|
+
}
|
|
13370
|
+
function asEverything(v, toPixels) {
|
|
13371
|
+
if (v && typeof v === "object" && "sessions" in v) return fastSamSegmenter(v, toPixels ? { toPixels } : {});
|
|
13372
|
+
return v;
|
|
13373
|
+
}
|
|
13374
|
+
/**
|
|
13375
|
+
* Every object in one pass (FastSAM-S by default): instances with a box,
|
|
13376
|
+
* score, area and mask, best first, after a class agnostic NMS. The
|
|
13377
|
+
* FastSAM record (AGPL-3.0 upstream) is added on first use unless `model`
|
|
13378
|
+
* names another record that provides "segment.everything".
|
|
13379
|
+
*/
|
|
13380
|
+
function segmentEverything(hub, image, o = {}) {
|
|
13381
|
+
const { model, size, maxMB, signal, schedule, toPixels, ...eo } = o;
|
|
13382
|
+
const id = model ?? "fastsam-s";
|
|
13383
|
+
ensureRecord(hub, id);
|
|
13384
|
+
const co = { model: id };
|
|
13385
|
+
if (size) co.size = size;
|
|
13386
|
+
if (maxMB !== void 0) co.maxMB = maxMB;
|
|
13387
|
+
if (signal) co.signal = signal;
|
|
13388
|
+
if (schedule) co.schedule = schedule;
|
|
13389
|
+
return withModel(hub, { id }, (v) => asEverything(v, toPixels).segmentAll(image, eo), co);
|
|
13390
|
+
}
|
|
12792
13391
|
/**
|
|
12793
13392
|
* Point, negative point and box prompted masks (SlimSAM), or a person mask
|
|
12794
13393
|
* without a prompt (selfie segmenter). Best first. The image is encoded
|
|
@@ -12846,6 +13445,7 @@ function ask(hub, image, question, o = {}) {
|
|
|
12846
13445
|
var target;
|
|
12847
13446
|
var init_api = __esmMin((() => {
|
|
12848
13447
|
init_loaders();
|
|
13448
|
+
init_fastsam();
|
|
12849
13449
|
init_records();
|
|
12850
13450
|
init_segment();
|
|
12851
13451
|
init_yolo();
|
|
@@ -13760,6 +14360,38 @@ function maskFromRle(r) {
|
|
|
13760
14360
|
data
|
|
13761
14361
|
};
|
|
13762
14362
|
}
|
|
14363
|
+
/** A selection as 1 in a byte a pixel (the hackbuild-ui mask tools' shape) to a {@link Mask}, 255 inside. */
|
|
14364
|
+
function selectionToMask(sel, width, height) {
|
|
14365
|
+
if (sel.length !== width * height) throw new RangeError(`selectionToMask: ${sel.length} values for a ${width} x ${height} mask`);
|
|
14366
|
+
return {
|
|
14367
|
+
width,
|
|
14368
|
+
height,
|
|
14369
|
+
data: Uint8Array.from(sel, (v) => v ? 255 : 0)
|
|
14370
|
+
};
|
|
14371
|
+
}
|
|
14372
|
+
/** A {@link Mask} to 1 in a byte a pixel (128 and up is inside), the shape the hackbuild-ui mask tools edit. */
|
|
14373
|
+
function maskToSelection(m) {
|
|
14374
|
+
return Uint8Array.from(m.data, (v) => v >= ON ? 1 : 0);
|
|
14375
|
+
}
|
|
14376
|
+
/** The tight box of the inside pixels in whole pixels, or null for an empty mask. */
|
|
14377
|
+
function maskBox(m) {
|
|
14378
|
+
let x0 = m.width;
|
|
14379
|
+
let y0 = m.height;
|
|
14380
|
+
let x1 = -1;
|
|
14381
|
+
let y1 = -1;
|
|
14382
|
+
for (let y = 0; y < m.height; y++) for (let x = 0; x < m.width; x++) if (m.data[y * m.width + x] >= ON) {
|
|
14383
|
+
if (x < x0) x0 = x;
|
|
14384
|
+
if (x > x1) x1 = x;
|
|
14385
|
+
if (y < y0) y0 = y;
|
|
14386
|
+
if (y > y1) y1 = y;
|
|
14387
|
+
}
|
|
14388
|
+
return x1 < 0 ? null : {
|
|
14389
|
+
x: x0,
|
|
14390
|
+
y: y0,
|
|
14391
|
+
w: x1 - x0 + 1,
|
|
14392
|
+
h: y1 - y0 + 1
|
|
14393
|
+
};
|
|
14394
|
+
}
|
|
13763
14395
|
var ON, make, INF, CRC;
|
|
13764
14396
|
var init_masks = __esmMin((() => {
|
|
13765
14397
|
ON = 128;
|
|
@@ -13779,6 +14411,257 @@ var init_masks = __esmMin((() => {
|
|
|
13779
14411
|
return t;
|
|
13780
14412
|
})();
|
|
13781
14413
|
}));
|
|
14414
|
+
// packages/vision/src/auto.ts
|
|
14415
|
+
/** Rec. 601 luma per pixel, 0 to 255. */
|
|
14416
|
+
function luma(px) {
|
|
14417
|
+
const n = px.width * px.height;
|
|
14418
|
+
const out = new Float32Array(n);
|
|
14419
|
+
for (let i = 0; i < n; i++) out[i] = .299 * px.data[i * 4] + .587 * px.data[i * 4 + 1] + .114 * px.data[i * 4 + 2];
|
|
14420
|
+
return out;
|
|
14421
|
+
}
|
|
14422
|
+
/** Edge pixels: the 3 by 3 Sobel gradient magnitude of luma above `threshold` (default 50). The one pixel border stays out. */
|
|
14423
|
+
function edgeMask(px, threshold = 50) {
|
|
14424
|
+
const { width: w, height: h } = px;
|
|
14425
|
+
const g = luma(px);
|
|
14426
|
+
const data = new Uint8Array(w * h);
|
|
14427
|
+
for (let y = 1; y < h - 1; y++) for (let x = 1; x < w - 1; x++) {
|
|
14428
|
+
const i = y * w + x;
|
|
14429
|
+
const a = g[i - w - 1], b = g[i - w], c = g[i - w + 1], d = g[i - 1], f = g[i + 1], p = g[i + w - 1], q = g[i + w], r = g[i + w + 1];
|
|
14430
|
+
const gx = c + 2 * f + r - a - 2 * d - p;
|
|
14431
|
+
const gy = p + 2 * q + r - a - 2 * b - c;
|
|
14432
|
+
if (Math.sqrt(gx * gx + gy * gy) > threshold) data[i] = 255;
|
|
14433
|
+
}
|
|
14434
|
+
return {
|
|
14435
|
+
width: w,
|
|
14436
|
+
height: h,
|
|
14437
|
+
data
|
|
14438
|
+
};
|
|
14439
|
+
}
|
|
14440
|
+
/** Pixels brighter than `threshold` (0 to 255, default 128); `invert` keeps the darker ones. */
|
|
14441
|
+
function thresholdMask(px, threshold = 128, invert = false) {
|
|
14442
|
+
const g = luma(px);
|
|
14443
|
+
return {
|
|
14444
|
+
width: px.width,
|
|
14445
|
+
height: px.height,
|
|
14446
|
+
data: Uint8Array.from(g, (v) => v > threshold !== invert ? 255 : 0)
|
|
14447
|
+
};
|
|
14448
|
+
}
|
|
14449
|
+
/** Otsu's threshold over the luma histogram: the cut that maximizes the between class variance, 0 to 255. */
|
|
14450
|
+
function otsuThreshold(px) {
|
|
14451
|
+
const hist = /* @__PURE__ */ new Float64Array(256);
|
|
14452
|
+
for (const v of luma(px)) hist[Math.round(v)]++;
|
|
14453
|
+
const total = px.width * px.height;
|
|
14454
|
+
let sum = 0;
|
|
14455
|
+
for (let i = 0; i < 256; i++) sum += i * hist[i];
|
|
14456
|
+
let sumB = 0;
|
|
14457
|
+
let wB = 0;
|
|
14458
|
+
let best = 0;
|
|
14459
|
+
let th = 0;
|
|
14460
|
+
for (let t = 0; t < 256; t++) {
|
|
14461
|
+
wB += hist[t];
|
|
14462
|
+
if (!wB) continue;
|
|
14463
|
+
const wF = total - wB;
|
|
14464
|
+
if (!wF) break;
|
|
14465
|
+
sumB += t * hist[t];
|
|
14466
|
+
const d = sumB / wB - (sum - sumB) / wF;
|
|
14467
|
+
const v = wB * wF * d * d;
|
|
14468
|
+
if (v > best) {
|
|
14469
|
+
best = v;
|
|
14470
|
+
th = t;
|
|
14471
|
+
}
|
|
14472
|
+
}
|
|
14473
|
+
return th;
|
|
14474
|
+
}
|
|
14475
|
+
/** The threshold mask at Otsu's cut, with the cut it chose. */
|
|
14476
|
+
function autoThresholdMask(px, invert = false) {
|
|
14477
|
+
const threshold = otsuThreshold(px);
|
|
14478
|
+
return {
|
|
14479
|
+
mask: thresholdMask(px, threshold, invert),
|
|
14480
|
+
threshold
|
|
14481
|
+
};
|
|
14482
|
+
}
|
|
14483
|
+
/** K-means on RGB: `k` clusters (2 to 16, default 4), `iterations` passes (default 8), centres seeded from pixels picked by `rng`. */
|
|
14484
|
+
function colourClusters(px, o = {}) {
|
|
14485
|
+
const k = Math.max(2, Math.min(16, Math.round(o.k ?? 4)));
|
|
14486
|
+
const rng = o.rng ?? seeded$1(1);
|
|
14487
|
+
const n = px.width * px.height;
|
|
14488
|
+
const d = px.data;
|
|
14489
|
+
const centres = [];
|
|
14490
|
+
for (let c = 0; c < k; c++) {
|
|
14491
|
+
const i = Math.min(n - 1, Math.floor(rng() * n)) * 4;
|
|
14492
|
+
centres.push([
|
|
14493
|
+
d[i],
|
|
14494
|
+
d[i + 1],
|
|
14495
|
+
d[i + 2]
|
|
14496
|
+
]);
|
|
14497
|
+
}
|
|
14498
|
+
const labels = new Uint8Array(n);
|
|
14499
|
+
const assign = () => {
|
|
14500
|
+
for (let i = 0; i < n; i++) {
|
|
14501
|
+
let best = Infinity;
|
|
14502
|
+
let l = 0;
|
|
14503
|
+
for (let c = 0; c < k; c++) {
|
|
14504
|
+
const e = centres[c];
|
|
14505
|
+
const dd = (d[i * 4] - e[0]) ** 2 + (d[i * 4 + 1] - e[1]) ** 2 + (d[i * 4 + 2] - e[2]) ** 2;
|
|
14506
|
+
if (dd < best) {
|
|
14507
|
+
best = dd;
|
|
14508
|
+
l = c;
|
|
14509
|
+
}
|
|
14510
|
+
}
|
|
14511
|
+
labels[i] = l;
|
|
14512
|
+
}
|
|
14513
|
+
};
|
|
14514
|
+
for (let it = 0; it < (o.iterations ?? 8); it++) {
|
|
14515
|
+
assign();
|
|
14516
|
+
const s = Array.from({ length: k }, () => [
|
|
14517
|
+
0,
|
|
14518
|
+
0,
|
|
14519
|
+
0,
|
|
14520
|
+
0
|
|
14521
|
+
]);
|
|
14522
|
+
for (let i = 0; i < n; i++) {
|
|
14523
|
+
const t = s[labels[i]];
|
|
14524
|
+
t[0] += d[i * 4];
|
|
14525
|
+
t[1] += d[i * 4 + 1];
|
|
14526
|
+
t[2] += d[i * 4 + 2];
|
|
14527
|
+
t[3]++;
|
|
14528
|
+
}
|
|
14529
|
+
s.forEach((t, c) => {
|
|
14530
|
+
if (t[3]) centres[c] = [
|
|
14531
|
+
t[0] / t[3],
|
|
14532
|
+
t[1] / t[3],
|
|
14533
|
+
t[2] / t[3]
|
|
14534
|
+
];
|
|
14535
|
+
});
|
|
14536
|
+
}
|
|
14537
|
+
assign();
|
|
14538
|
+
const counts = new Array(k).fill(0);
|
|
14539
|
+
for (const l of labels) counts[l]++;
|
|
14540
|
+
return {
|
|
14541
|
+
width: px.width,
|
|
14542
|
+
height: px.height,
|
|
14543
|
+
labels,
|
|
14544
|
+
centres,
|
|
14545
|
+
counts,
|
|
14546
|
+
background: counts.indexOf(Math.max(...counts))
|
|
14547
|
+
};
|
|
14548
|
+
}
|
|
14549
|
+
/** Everything but the largest colour cluster, as the mask studio's cluster button: `colourClusters` then a mask. */
|
|
14550
|
+
function clusterMask(px, o = {}) {
|
|
14551
|
+
const c = colourClusters(px, o);
|
|
14552
|
+
return {
|
|
14553
|
+
width: c.width,
|
|
14554
|
+
height: c.height,
|
|
14555
|
+
data: Uint8Array.from(c.labels, (l) => l !== c.background ? 255 : 0)
|
|
14556
|
+
};
|
|
14557
|
+
}
|
|
14558
|
+
/** One mask per cluster, largest first. */
|
|
14559
|
+
function clusterMasks(c) {
|
|
14560
|
+
return c.counts.map((count, i) => ({
|
|
14561
|
+
count,
|
|
14562
|
+
i
|
|
14563
|
+
})).filter((e) => e.count > 0).sort((a, b) => b.count - a.count).map(({ i }) => ({
|
|
14564
|
+
width: c.width,
|
|
14565
|
+
height: c.height,
|
|
14566
|
+
data: Uint8Array.from(c.labels, (l) => l === i ? 255 : 0)
|
|
14567
|
+
}));
|
|
14568
|
+
}
|
|
14569
|
+
function seeded$1(seed) {
|
|
14570
|
+
let a = seed >>> 0;
|
|
14571
|
+
return () => {
|
|
14572
|
+
a = a + 1831565813 >>> 0;
|
|
14573
|
+
let t = a;
|
|
14574
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
14575
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
14576
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
14577
|
+
};
|
|
14578
|
+
}
|
|
14579
|
+
var init_auto = __esmMin((() => {}));
|
|
14580
|
+
// packages/vision/src/instances.ts
|
|
14581
|
+
function cropPixels(px, b) {
|
|
14582
|
+
const data = new Uint8ClampedArray(b.w * b.h * 4);
|
|
14583
|
+
for (let y = 0; y < b.h; y++) data.set(px.data.subarray(((b.y + y) * px.width + b.x) * 4, ((b.y + y) * px.width + b.x + b.w) * 4), y * b.w * 4);
|
|
14584
|
+
return {
|
|
14585
|
+
width: b.w,
|
|
14586
|
+
height: b.h,
|
|
14587
|
+
data
|
|
14588
|
+
};
|
|
14589
|
+
}
|
|
14590
|
+
function cropMask(m, b) {
|
|
14591
|
+
const data = new Uint8Array(b.w * b.h);
|
|
14592
|
+
for (let y = 0; y < b.h; y++) data.set(m.data.subarray((b.y + y) * m.width + b.x, (b.y + y) * m.width + b.x + b.w), y * b.w);
|
|
14593
|
+
return {
|
|
14594
|
+
width: b.w,
|
|
14595
|
+
height: b.h,
|
|
14596
|
+
data
|
|
14597
|
+
};
|
|
14598
|
+
}
|
|
14599
|
+
/** The files for a set of instances: `<prefix>_<n>_cutout.png` and `<prefix>_<n>_mask.png` per non empty instance, then `instances.json`. */
|
|
14600
|
+
async function exportInstances(image, instances, o = {}) {
|
|
14601
|
+
const prefix = o.prefix ?? "instance";
|
|
14602
|
+
const crop = o.crop ?? true;
|
|
14603
|
+
const files = [];
|
|
14604
|
+
const rows = [];
|
|
14605
|
+
const po = o.compress === void 0 ? {} : { compress: o.compress };
|
|
14606
|
+
for (let i = 0; i < instances.length; i++) {
|
|
14607
|
+
const inst = instances[i];
|
|
14608
|
+
if (inst.mask.width !== image.width || inst.mask.height !== image.height) throw new RangeError(`exportInstances: instance ${i + 1} mask is ${inst.mask.width} x ${inst.mask.height}, image ${image.width} x ${image.height}`);
|
|
14609
|
+
const full = {
|
|
14610
|
+
x: 0,
|
|
14611
|
+
y: 0,
|
|
14612
|
+
w: image.width,
|
|
14613
|
+
h: image.height
|
|
14614
|
+
};
|
|
14615
|
+
const b = crop ? maskBox(inst.mask) : full;
|
|
14616
|
+
if (!b) continue;
|
|
14617
|
+
const n = i + 1;
|
|
14618
|
+
const cut = `${prefix}_${n}_cutout.png`;
|
|
14619
|
+
const mk = `${prefix}_${n}_mask.png`;
|
|
14620
|
+
const m = crop ? cropMask(inst.mask, b) : inst.mask;
|
|
14621
|
+
files.push({
|
|
14622
|
+
name: cut,
|
|
14623
|
+
data: await cutoutPng(crop ? cropPixels(image, b) : image, m, po),
|
|
14624
|
+
type: "image/png"
|
|
14625
|
+
});
|
|
14626
|
+
files.push({
|
|
14627
|
+
name: mk,
|
|
14628
|
+
data: await maskToPng(m, po),
|
|
14629
|
+
type: "image/png"
|
|
14630
|
+
});
|
|
14631
|
+
const row = {
|
|
14632
|
+
index: n,
|
|
14633
|
+
score: r2(inst.score),
|
|
14634
|
+
area: inst.area,
|
|
14635
|
+
box: {
|
|
14636
|
+
x: r2(inst.box.x),
|
|
14637
|
+
y: r2(inst.box.y),
|
|
14638
|
+
w: r2(inst.box.w),
|
|
14639
|
+
h: r2(inst.box.h)
|
|
14640
|
+
},
|
|
14641
|
+
crop: b,
|
|
14642
|
+
cutout: cut,
|
|
14643
|
+
mask: mk
|
|
14644
|
+
};
|
|
14645
|
+
if (inst.label !== void 0) row.label = inst.label;
|
|
14646
|
+
rows.push(row);
|
|
14647
|
+
}
|
|
14648
|
+
const json = JSON.stringify({
|
|
14649
|
+
width: image.width,
|
|
14650
|
+
height: image.height,
|
|
14651
|
+
instances: rows
|
|
14652
|
+
}, null, 2);
|
|
14653
|
+
files.push({
|
|
14654
|
+
name: `${prefix}s.json`,
|
|
14655
|
+
data: new TextEncoder().encode(json),
|
|
14656
|
+
type: "application/json"
|
|
14657
|
+
});
|
|
14658
|
+
return files;
|
|
14659
|
+
}
|
|
14660
|
+
var r2;
|
|
14661
|
+
var init_instances = __esmMin((() => {
|
|
14662
|
+
init_masks();
|
|
14663
|
+
r2 = (v) => Math.round(v * 100) / 100;
|
|
14664
|
+
}));
|
|
13782
14665
|
// packages/vision/src/signals.ts
|
|
13783
14666
|
/** One raw face from a face landmark result, or null. Expressions read 0 without blendshapes. */
|
|
13784
14667
|
function rawFace(lm, index = 0) {
|
|
@@ -14138,6 +15021,7 @@ var src_exports$1 = /* @__PURE__ */ __exportAll({
|
|
|
14138
15021
|
COCO_LABELS: () => COCO_LABELS,
|
|
14139
15022
|
DEFAULT_REACTIONS: () => DEFAULT_REACTIONS,
|
|
14140
15023
|
DEPTH_WORKER_SOURCE: () => DEPTH_WORKER_SOURCE,
|
|
15024
|
+
DETECT_SIZES: () => DETECT_SIZES,
|
|
14141
15025
|
EmbeddingCache: () => EmbeddingCache,
|
|
14142
15026
|
LOOK_PATTERN: () => LOOK_PATTERN,
|
|
14143
15027
|
MP_MODELS: () => MP_MODELS,
|
|
@@ -14149,11 +15033,17 @@ var src_exports$1 = /* @__PURE__ */ __exportAll({
|
|
|
14149
15033
|
VISION_RECORDS: () => VISION_RECORDS,
|
|
14150
15034
|
ask: () => ask,
|
|
14151
15035
|
attachVision: () => attachVision,
|
|
15036
|
+
autoThresholdMask: () => autoThresholdMask,
|
|
14152
15037
|
caption: () => caption,
|
|
14153
15038
|
centreDetector: () => centreDetector,
|
|
15039
|
+
clusterMask: () => clusterMask,
|
|
15040
|
+
clusterMasks: () => clusterMasks,
|
|
15041
|
+
colourClusters: () => colourClusters,
|
|
14154
15042
|
cutoutPng: () => cutoutPng,
|
|
14155
15043
|
cutoutRgba: () => cutoutRgba,
|
|
15044
|
+
decodeFastSam: () => decodeFastSam,
|
|
14156
15045
|
decodeYolo: () => decodeYolo,
|
|
15046
|
+
decodeYoloEnd2End: () => decodeYoloEnd2End,
|
|
14157
15047
|
depth: () => depth,
|
|
14158
15048
|
depthFromOutput: () => depthFromOutput,
|
|
14159
15049
|
depthLoader: () => depthLoader,
|
|
@@ -14161,24 +15051,33 @@ var src_exports$1 = /* @__PURE__ */ __exportAll({
|
|
|
14161
15051
|
depthWorker: () => depthWorker,
|
|
14162
15052
|
describePerception: () => describePerception,
|
|
14163
15053
|
detect: () => detect,
|
|
15054
|
+
detectAt: () => detectAt,
|
|
14164
15055
|
detectCadenceMs: () => detectCadenceMs,
|
|
14165
15056
|
discSegmenter: () => discSegmenter,
|
|
15057
|
+
edgeMask: () => edgeMask,
|
|
14166
15058
|
emptyMask: () => emptyMask,
|
|
14167
15059
|
encodePng: () => encodePng,
|
|
15060
|
+
ensureRecord: () => ensureRecord,
|
|
15061
|
+
exportInstances: () => exportInstances,
|
|
14168
15062
|
faceApiProvider: () => faceApiProvider,
|
|
14169
15063
|
faceApiRecord: () => faceApiRecord,
|
|
15064
|
+
fastSamSegmenter: () => fastSamSegmenter,
|
|
14170
15065
|
fitSize: () => fitSize,
|
|
14171
15066
|
fixedLandmarker: () => fixedLandmarker,
|
|
14172
15067
|
fixedVlm: () => fixedVlm,
|
|
14173
15068
|
gazeTarget: () => gazeTarget,
|
|
14174
15069
|
hasPrompt: () => hasPrompt,
|
|
15070
|
+
instanceAt: () => instanceAt,
|
|
14175
15071
|
iou: () => iou,
|
|
15072
|
+
isEnd2End: () => isEnd2End,
|
|
14176
15073
|
landmarkChain: () => landmarkChain,
|
|
14177
15074
|
landmarks: () => landmarks,
|
|
14178
15075
|
letterboxParams: () => letterboxParams,
|
|
14179
15076
|
letterboxTensor: () => letterboxTensor,
|
|
14180
15077
|
loadTasksVision: () => loadTasksVision,
|
|
15078
|
+
luma: () => luma,
|
|
14181
15079
|
maskArea: () => maskArea,
|
|
15080
|
+
maskBox: () => maskBox,
|
|
14182
15081
|
maskDilate: () => maskDilate,
|
|
14183
15082
|
maskErode: () => maskErode,
|
|
14184
15083
|
maskFeather: () => maskFeather,
|
|
@@ -14192,11 +15091,13 @@ var src_exports$1 = /* @__PURE__ */ __exportAll({
|
|
|
14192
15091
|
maskToPng: () => maskToPng,
|
|
14193
15092
|
maskToRgba: () => maskToRgba,
|
|
14194
15093
|
maskToRle: () => maskToRle,
|
|
15094
|
+
maskToSelection: () => maskToSelection,
|
|
14195
15095
|
maskToSvg: () => maskToSvg,
|
|
14196
15096
|
maskUnion: () => maskUnion,
|
|
14197
15097
|
mediapipeLadder: () => mediapipeLadder,
|
|
14198
15098
|
mediapipeProvider: () => mediapipeProvider,
|
|
14199
15099
|
nms: () => nms,
|
|
15100
|
+
otsuThreshold: () => otsuThreshold,
|
|
14200
15101
|
perceptionLoop: () => perceptionLoop,
|
|
14201
15102
|
promptSegmenter: () => promptSegmenter,
|
|
14202
15103
|
rampDepth: () => rampDepth,
|
|
@@ -14207,8 +15108,11 @@ var src_exports$1 = /* @__PURE__ */ __exportAll({
|
|
|
14207
15108
|
readbackLoop: () => readbackLoop,
|
|
14208
15109
|
samLoader: () => samLoader,
|
|
14209
15110
|
segment: () => segment,
|
|
15111
|
+
segmentEverything: () => segmentEverything,
|
|
15112
|
+
selectionToMask: () => selectionToMask,
|
|
14210
15113
|
tasksVisionUrls: () => tasksVisionUrls,
|
|
14211
15114
|
tasksVisionWasm: () => tasksVisionWasm,
|
|
15115
|
+
thresholdMask: () => thresholdMask,
|
|
14212
15116
|
visionStubs: () => visionStubs,
|
|
14213
15117
|
vlmLoader: () => vlmLoader,
|
|
14214
15118
|
wantsLook: () => wantsLook,
|
|
@@ -14227,6 +15131,9 @@ var init_src$2 = __esmMin((() => {
|
|
|
14227
15131
|
init_stubs();
|
|
14228
15132
|
init_segment();
|
|
14229
15133
|
init_masks();
|
|
15134
|
+
init_fastsam();
|
|
15135
|
+
init_auto();
|
|
15136
|
+
init_instances();
|
|
14230
15137
|
init_signals();
|
|
14231
15138
|
}));
|
|
14232
15139
|
// packages/ai-views/src/jobs.ts
|
|
@@ -14349,22 +15256,22 @@ function isJob(s) {
|
|
|
14349
15256
|
"[tabindex]",
|
|
14350
15257
|
"[contenteditable=\"true\"]"
|
|
14351
15258
|
].join(",");
|
|
14352
|
-
function
|
|
15259
|
+
function _o(e) {
|
|
14353
15260
|
if (!e) return "";
|
|
14354
15261
|
const t = [];
|
|
14355
15262
|
return e.tokPerSec !== null && e.tokPerSec !== void 0 && t.push(`${e.tokPerSec.toFixed(1)} tok/s`), e.firstMs !== null && e.firstMs !== void 0 && t.push(`first word ${(e.firstMs / 1e3).toFixed(2)} s`), e.tokens !== null && e.tokens !== void 0 && t.push(`${e.tokens} tok`), t.length ? `${t.join(", ")} (${e.source ?? "measured"})` : "";
|
|
14356
15263
|
}
|
|
14357
|
-
function
|
|
15264
|
+
function ie(e) {
|
|
14358
15265
|
if (!Number.isFinite(e)) return "--:--";
|
|
14359
15266
|
const t = Math.max(0, Math.floor(e)), r = Math.floor(t / 3600), n = Math.floor(t % 3600 / 60), o = t % 60, i = (s) => String(s).padStart(2, "0");
|
|
14360
15267
|
return r ? `${r}:${i(n)}:${i(o)}` : `${i(n)}:${i(o)}`;
|
|
14361
15268
|
}
|
|
14362
|
-
function
|
|
15269
|
+
function Vn(e) {
|
|
14363
15270
|
return e >= 1e9 ? `${(e / 1e9).toFixed(1)} gb` : e >= 1e6 ? `${Math.round(e / 1e6)} mb` : `${Math.max(1, Math.round(e / 1e3))} kb`;
|
|
14364
15271
|
}
|
|
14365
|
-
function
|
|
15272
|
+
function xi(e) {
|
|
14366
15273
|
const t = [];
|
|
14367
|
-
return e.duration && t.push({ value:
|
|
15274
|
+
return e.duration && t.push({ value: ie(e.duration) }), e.size !== void 0 && t.push({ value: Vn(e.size) }), e.snrDb !== void 0 && t.push({
|
|
14368
15275
|
label: "snr",
|
|
14369
15276
|
value: `${e.snrDb.toFixed(1)} dB`,
|
|
14370
15277
|
tone: e.snrDb < 8 ? "lo" : e.snrDb > 20 ? "hi" : void 0
|
|
@@ -14376,22 +15283,22 @@ function fi(e) {
|
|
|
14376
15283
|
value: `${Math.round(e.density * 100)}%`
|
|
14377
15284
|
}), e.rtf && t.push({ value: `${e.rtf.toFixed(1)}x rt` }), e.lines !== void 0 && t.push({ value: `${e.lines} ${e.lines === 1 ? "line" : "lines"}` }), t;
|
|
14378
15285
|
}
|
|
14379
|
-
|
|
14380
|
-
function
|
|
15286
|
+
Y("wand", "add"), Y("brush", "add"), Y("eraser", "add"), Y("lasso", "add"), Y("polygon", "add"), Y("rect", "add"), Y("ellipse", "add"), Y("flood", "add"), Y("pan", "add");
|
|
15287
|
+
function Y(e, t) {
|
|
14381
15288
|
const r = t === "add", n = `x swaps to ${r ? "erase" : "add"}`;
|
|
14382
15289
|
return e === "wand" ? `tap a colour to ${r ? "add" : "cut"} it. shift ${r ? "cuts" : "adds"}, ${n}` : e === "brush" ? `paint to ${r ? "add" : "erase"}. [ and ] size, ${n}` : e === "eraser" ? "paint to erase. shift adds, [ and ] size" : e === "polygon" ? `press corner by corner, enter or the first corner ${r ? "fills" : "cuts"}. backspace drops a corner, esc all` : e === "rect" || e === "ellipse" ? `drag a ${e === "rect" ? "box" : "ring"} to ${r ? "add" : "cut"} it. shift ${r ? "cuts" : "adds"}, ${n}` : e === "flood" ? `tap to fill the patch of one colour. shift ${r ? "cuts" : "adds"}, ${n}` : e === "pan" ? "drag to move the picture. space pans from any tool" : `drag around a shape, lift to ${r ? "fill" : "cut"}. esc drops it, ${n}`;
|
|
14383
15290
|
}
|
|
14384
|
-
function
|
|
15291
|
+
function Re(e) {
|
|
14385
15292
|
return !!e && !e.unfit && e.state !== "loaded" && e.state !== "loading";
|
|
14386
15293
|
}
|
|
14387
|
-
function
|
|
15294
|
+
function Dr(e) {
|
|
14388
15295
|
return !Number.isFinite(e) || e <= 0 ? "0 mb" : e >= 1e9 ? `${(e / 1e9).toFixed(1).replace(/\.0$/, "")} gb` : `${Math.max(1, Math.round(e / 1e6))} mb`;
|
|
14389
15296
|
}
|
|
14390
|
-
function
|
|
14391
|
-
const t = e.filter((n) =>
|
|
15297
|
+
function Zs(e) {
|
|
15298
|
+
const t = e.filter((n) => Re(n)), r = t.filter((n) => n.state !== "stored").reduce((n, o) => n + (o.bytes ?? 0), 0);
|
|
14392
15299
|
return {
|
|
14393
15300
|
ids: t.map((n) => n.id),
|
|
14394
|
-
label: r > 0 ? `load all, ${
|
|
15301
|
+
label: r > 0 ? `load all, ${Dr(r)}` : "load all"
|
|
14395
15302
|
};
|
|
14396
15303
|
}
|
|
14397
15304
|
// packages/ai-views/src/rows.ts
|
|
@@ -14438,7 +15345,7 @@ function backendOf(r, env) {
|
|
|
14438
15345
|
/** Download words: "1.7 gb". Null for a record with no download or an unknown size. */
|
|
14439
15346
|
function sizeWords(r) {
|
|
14440
15347
|
if (isNoDownload(r) || r.sizeMB === null) return void 0;
|
|
14441
|
-
return
|
|
15348
|
+
return Dr(r.sizeMB * MIB);
|
|
14442
15349
|
}
|
|
14443
15350
|
/** The record's label, else its id made readable: "hermes 3 llama 3.2 3b". */
|
|
14444
15351
|
const pretty = (r) => r.label ?? r.id.replace(/-(mlc|gguf)$/, "").replace(/-/g, " ");
|
|
@@ -14457,11 +15364,11 @@ function detailOf(r) {
|
|
|
14457
15364
|
});
|
|
14458
15365
|
if (r.sizeMB !== null && !isNoDownload(r)) out.push({
|
|
14459
15366
|
label: "size",
|
|
14460
|
-
value: `${
|
|
15367
|
+
value: `${Dr(r.sizeMB * MIB)}, ${r.sizeSource}`
|
|
14461
15368
|
});
|
|
14462
15369
|
if (r.vramMB != null) out.push({
|
|
14463
15370
|
label: "gpu memory",
|
|
14464
|
-
value: `${
|
|
15371
|
+
value: `${Dr(r.vramMB * MIB)}, ${r.vramSource ?? "unknown"}`
|
|
14465
15372
|
});
|
|
14466
15373
|
if (r.ctx) out.push({
|
|
14467
15374
|
label: "context",
|
|
@@ -14557,7 +15464,7 @@ function rowOf(r, ctx = {}) {
|
|
|
14557
15464
|
state
|
|
14558
15465
|
};
|
|
14559
15466
|
if (size) row.size = size;
|
|
14560
|
-
if (r.vramMB != null) row.memory = `needs ${
|
|
15467
|
+
if (r.vramMB != null) row.memory = `needs ${Dr(r.vramMB * MIB)}`;
|
|
14561
15468
|
if (r.sizeMB !== null && !isNoDownload(r)) row.bytes = r.sizeMB * MIB;
|
|
14562
15469
|
const speed = fit.fit === "fits on cpu, slow" ? "slow" : r.speed;
|
|
14563
15470
|
if (speed) row.speed = speed;
|
|
@@ -14576,6 +15483,73 @@ function rowOf(r, ctx = {}) {
|
|
|
14576
15483
|
// packages/ai-views/src/pick.ts
|
|
14577
15484
|
init_src$11();
|
|
14578
15485
|
/**
|
|
15486
|
+
* The ids of records whose files are already in a weight store: the cache
|
|
15487
|
+
* badge of a picker. A record counts when every file key it names is kept,
|
|
15488
|
+
* or, for one its runtime fetches by model id, when any kept url sits under
|
|
15489
|
+
* that id on the hub. Records cached elsewhere (WebLLM's own cache) are not seen.
|
|
15490
|
+
*/
|
|
15491
|
+
async function storedIds(store, records) {
|
|
15492
|
+
const keys = await store.keys().catch(() => []);
|
|
15493
|
+
if (!keys.length) return [];
|
|
15494
|
+
const set = new Set(keys);
|
|
15495
|
+
return records.filter((r) => {
|
|
15496
|
+
const ks = weightKeys(r);
|
|
15497
|
+
if (ks.length) return ks.every((k) => set.has(k));
|
|
15498
|
+
const id = r.source.modelId;
|
|
15499
|
+
return !!id && keys.some((k) => k.includes(`/${id}/resolve/`));
|
|
15500
|
+
}).map((r) => r.id);
|
|
15501
|
+
}
|
|
15502
|
+
/**
|
|
15503
|
+
* Checks a hub repo id before it is used as your own model: it exists, it is
|
|
15504
|
+
* public, and it carries onnx files transformers.js can run. One request to
|
|
15505
|
+
* the hub's model api; no weights are fetched.
|
|
15506
|
+
*/
|
|
15507
|
+
async function verifyRepo(repo, o = {}) {
|
|
15508
|
+
const id = repo.trim().replace(/^https?:\/\/huggingface\.co\//, "").replace(/\/+$/, "");
|
|
15509
|
+
if (!/^[\w.-]+\/[\w.-]+$/.test(id)) return {
|
|
15510
|
+
ok: false,
|
|
15511
|
+
onnx: 0,
|
|
15512
|
+
note: "write it as owner/name."
|
|
15513
|
+
};
|
|
15514
|
+
const f = o.fetch ?? globalThis.fetch.bind(globalThis);
|
|
15515
|
+
let res;
|
|
15516
|
+
try {
|
|
15517
|
+
res = await f(`${o.host ?? "https://huggingface.co"}/api/models/${id}`);
|
|
15518
|
+
} catch {
|
|
15519
|
+
return {
|
|
15520
|
+
ok: false,
|
|
15521
|
+
onnx: 0,
|
|
15522
|
+
note: "the hub did not answer. check the connection."
|
|
15523
|
+
};
|
|
15524
|
+
}
|
|
15525
|
+
if (res.status === 401 || res.status === 403) return {
|
|
15526
|
+
ok: false,
|
|
15527
|
+
onnx: 0,
|
|
15528
|
+
note: `${id} is gated or private. it cannot load here.`
|
|
15529
|
+
};
|
|
15530
|
+
if (!res.ok) return {
|
|
15531
|
+
ok: false,
|
|
15532
|
+
onnx: 0,
|
|
15533
|
+
note: `${id} was not found on the hub.`
|
|
15534
|
+
};
|
|
15535
|
+
const body = await res.json().catch(() => ({}));
|
|
15536
|
+
if (body.gated) return {
|
|
15537
|
+
ok: false,
|
|
15538
|
+
onnx: 0,
|
|
15539
|
+
note: `${id} is gated. accept its terms on the hub first.`
|
|
15540
|
+
};
|
|
15541
|
+
const onnx = (body.siblings ?? []).filter((x) => x.rfilename.endsWith(".onnx")).length;
|
|
15542
|
+
return onnx ? {
|
|
15543
|
+
ok: true,
|
|
15544
|
+
onnx,
|
|
15545
|
+
note: `${id} found, ${onnx} onnx files.`
|
|
15546
|
+
} : {
|
|
15547
|
+
ok: false,
|
|
15548
|
+
onnx: 0,
|
|
15549
|
+
note: `${id} has no onnx files. pick an onnx export.`
|
|
15550
|
+
};
|
|
15551
|
+
}
|
|
15552
|
+
/**
|
|
14579
15553
|
* A loader over a weight store: a record with a url fetches that file, a
|
|
14580
15554
|
* record with a repo and files fetches each, resuming and checking the
|
|
14581
15555
|
* SHA-256 when the record has one. Records a runtime fetches itself (WebLLM,
|
|
@@ -14764,7 +15738,7 @@ function useModelPick(job, o = {}) {
|
|
|
14764
15738
|
estimate,
|
|
14765
15739
|
state: state.get(id)
|
|
14766
15740
|
});
|
|
14767
|
-
if (!rec || !
|
|
15741
|
+
if (!rec || !Re(row)) return;
|
|
14768
15742
|
if (!loader) {
|
|
14769
15743
|
state.set(id, "error");
|
|
14770
15744
|
errors.set(id, "no loader. the page passes one to load a model.");
|
|
@@ -14833,6 +15807,7 @@ function useModelPick(job, o = {}) {
|
|
|
14833
15807
|
if (n.records) records = jobRecords(job, n.records);
|
|
14834
15808
|
if (n.loader) loader = n.loader;
|
|
14835
15809
|
if (n.onRemove) onRemove = n.onRemove;
|
|
15810
|
+
for (const id of n.stored ?? []) if (!state.has(id) || state.get(id) === "none") state.set(id, "stored");
|
|
14836
15811
|
emit();
|
|
14837
15812
|
},
|
|
14838
15813
|
result: (id) => results.get(id),
|
|
@@ -14867,7 +15842,7 @@ function useJobsPick(jobs, initialPresets = [], shared = {}) {
|
|
|
14867
15842
|
return {
|
|
14868
15843
|
id: pr.id,
|
|
14869
15844
|
label: pr.label,
|
|
14870
|
-
total: bytes > 0 ? `about ${
|
|
15845
|
+
total: bytes > 0 ? `about ${Dr(bytes)}` : "nothing to fetch"
|
|
14871
15846
|
};
|
|
14872
15847
|
}),
|
|
14873
15848
|
applyPreset(id) {
|
|
@@ -14875,7 +15850,7 @@ function useJobsPick(jobs, initialPresets = [], shared = {}) {
|
|
|
14875
15850
|
if (!pr) return;
|
|
14876
15851
|
for (const [job, mid] of Object.entries(pr.picks)) picks.get(job)?.choose(mid);
|
|
14877
15852
|
},
|
|
14878
|
-
all: () =>
|
|
15853
|
+
all: () => Zs(chosenRows()),
|
|
14879
15854
|
async loadAll() {
|
|
14880
15855
|
await Promise.all([...picks.values()].map((p) => p.load().catch(() => void 0)));
|
|
14881
15856
|
},
|
|
@@ -14949,25 +15924,30 @@ function chatThread(o = {}) {
|
|
|
14949
15924
|
try {
|
|
14950
15925
|
if (o.recall && lastUser) mem = await o.recall(lastUser.text);
|
|
14951
15926
|
if (mem.length) patch(live.id, { mem });
|
|
14952
|
-
const
|
|
15927
|
+
const base = typeof o.system === "function" ? o.system() : o.system;
|
|
15928
|
+
const sys = o.systemFor ? o.systemFor(mem) : [base, mem.length ? `things you remember:\n${mem.map((m) => `- ${m.label}`).join("\n")}` : ""].filter(Boolean).join("\n\n");
|
|
15929
|
+
const past = turns.filter((t) => t.id !== live.id && t.text);
|
|
15930
|
+
const keep = o.history?.();
|
|
15931
|
+
const sent = keep !== void 0 && keep >= 0 ? past.slice(Math.max(0, past.length - 1 - keep)) : past;
|
|
14953
15932
|
const history = [...sys ? [{
|
|
14954
15933
|
role: "system",
|
|
14955
15934
|
content: sys
|
|
14956
|
-
}] : [], ...
|
|
15935
|
+
}] : [], ...sent.map((t) => ({
|
|
14957
15936
|
role: t.role,
|
|
14958
15937
|
content: t.text
|
|
14959
15938
|
}))];
|
|
14960
15939
|
let text = "";
|
|
14961
15940
|
let think = "";
|
|
14962
15941
|
const it = engine.stream(history, {
|
|
14963
|
-
|
|
14964
|
-
...o.
|
|
15942
|
+
...o.maxTokens ? { maxTokens: o.maxTokens } : {},
|
|
15943
|
+
...o.stream?.(),
|
|
15944
|
+
signal
|
|
14965
15945
|
});
|
|
14966
15946
|
for (;;) {
|
|
14967
15947
|
const step = await it.next();
|
|
14968
15948
|
if (step.done) {
|
|
14969
15949
|
const s = step.value?.stats;
|
|
14970
|
-
const stats = s ?
|
|
15950
|
+
const stats = s ? _o({
|
|
14971
15951
|
tokPerSec: s.tokensPerSecond,
|
|
14972
15952
|
firstMs: s.firstTokenMs,
|
|
14973
15953
|
tokens: s.tokens,
|
|
@@ -14977,6 +15957,8 @@ function chatThread(o = {}) {
|
|
|
14977
15957
|
done: true,
|
|
14978
15958
|
...stats ? { stats } : {}
|
|
14979
15959
|
});
|
|
15960
|
+
const fin = turns.find((t) => t.id === live.id);
|
|
15961
|
+
if (fin?.text) o.onReply?.(fin, lastUser?.text ?? "");
|
|
14980
15962
|
break;
|
|
14981
15963
|
}
|
|
14982
15964
|
const ev = step.value;
|
|
@@ -15042,6 +16024,15 @@ function chatThread(o = {}) {
|
|
|
15042
16024
|
engine = e;
|
|
15043
16025
|
emit();
|
|
15044
16026
|
},
|
|
16027
|
+
load(next) {
|
|
16028
|
+
ac?.abort();
|
|
16029
|
+
turns = next.map((t) => ({
|
|
16030
|
+
...t,
|
|
16031
|
+
id: ++seq,
|
|
16032
|
+
done: true
|
|
16033
|
+
}));
|
|
16034
|
+
emit();
|
|
16035
|
+
},
|
|
15045
16036
|
markdown(title = "chat") {
|
|
15046
16037
|
const lines = [`# ${title}`, ""];
|
|
15047
16038
|
for (const t of turns) lines.push(`## ${t.role === "user" ? "you" : "reply"}`, "", t.text, "");
|
|
@@ -15100,7 +16091,8 @@ function chatList(o = {}) {
|
|
|
15100
16091
|
remove: (id) => chats.get(current).t.remove(id),
|
|
15101
16092
|
clear: () => chats.get(current).t.clear(),
|
|
15102
16093
|
setEngine: (e) => list.setEngine(e),
|
|
15103
|
-
markdown: (title) => chats.get(current).t.markdown(title ?? chats.get(current).title)
|
|
16094
|
+
markdown: (title) => chats.get(current).t.markdown(title ?? chats.get(current).title),
|
|
16095
|
+
load: (t) => chats.get(current).t.load(t)
|
|
15104
16096
|
},
|
|
15105
16097
|
snapshot: snap,
|
|
15106
16098
|
subscribe(fn) {
|
|
@@ -15148,6 +16140,50 @@ function chatList(o = {}) {
|
|
|
15148
16140
|
setEngine(e) {
|
|
15149
16141
|
engine = e;
|
|
15150
16142
|
for (const c of chats.values()) c.t.setEngine(e);
|
|
16143
|
+
},
|
|
16144
|
+
dump() {
|
|
16145
|
+
return {
|
|
16146
|
+
format: "hackbuild-chats",
|
|
16147
|
+
version: 1,
|
|
16148
|
+
current,
|
|
16149
|
+
chats: [...chats].map(([id, c]) => ({
|
|
16150
|
+
id,
|
|
16151
|
+
title: c.title,
|
|
16152
|
+
updated: c.updated,
|
|
16153
|
+
named: c.named,
|
|
16154
|
+
turns: c.t.snapshot().turns.filter((t) => t.done).map((t) => ({ ...t }))
|
|
16155
|
+
}))
|
|
16156
|
+
};
|
|
16157
|
+
},
|
|
16158
|
+
restore(data, ro = {}) {
|
|
16159
|
+
if (data?.format !== "hackbuild-chats" || data.version !== 1 || !Array.isArray(data.chats)) throw new Error("not a hackbuild-chats version 1 file.");
|
|
16160
|
+
const empty = [...chats.values()].every((c) => !c.t.snapshot().turns.length);
|
|
16161
|
+
if (ro.replace || empty) {
|
|
16162
|
+
for (const c of chats.values()) c.t.stop();
|
|
16163
|
+
chats.clear();
|
|
16164
|
+
}
|
|
16165
|
+
let open = "";
|
|
16166
|
+
for (const c of data.chats) {
|
|
16167
|
+
const id = `chat-${++seq}`;
|
|
16168
|
+
const t = chatThread({
|
|
16169
|
+
...o,
|
|
16170
|
+
engine
|
|
16171
|
+
});
|
|
16172
|
+
t.load((c.turns ?? []).filter((x) => (x.role === "user" || x.role === "assistant") && typeof x.text === "string").map((x) => ({
|
|
16173
|
+
...x,
|
|
16174
|
+
think: x.think ?? ""
|
|
16175
|
+
})));
|
|
16176
|
+
chats.set(id, {
|
|
16177
|
+
t,
|
|
16178
|
+
title: String(c.title || "chat"),
|
|
16179
|
+
updated: +c.updated || now(),
|
|
16180
|
+
named: !!c.named
|
|
16181
|
+
});
|
|
16182
|
+
if (c.id === data.current) open = id;
|
|
16183
|
+
}
|
|
16184
|
+
if (!chats.size) return void list.create();
|
|
16185
|
+
current = open || (chats.has(current) ? current : snap().items[0].id);
|
|
16186
|
+
follow();
|
|
15151
16187
|
}
|
|
15152
16188
|
};
|
|
15153
16189
|
list.create();
|
|
@@ -15213,11 +16249,20 @@ function transcribeQueue(o) {
|
|
|
15213
16249
|
} : c);
|
|
15214
16250
|
emit();
|
|
15215
16251
|
};
|
|
15216
|
-
const metaOf = (c) =>
|
|
15217
|
-
|
|
15218
|
-
|
|
15219
|
-
|
|
15220
|
-
|
|
16252
|
+
const metaOf = (c) => {
|
|
16253
|
+
const s = c.stats;
|
|
16254
|
+
return xi({
|
|
16255
|
+
size: c.size,
|
|
16256
|
+
...c.duration ? { duration: c.duration } : {},
|
|
16257
|
+
...s ? {
|
|
16258
|
+
snrDb: s.snrDb,
|
|
16259
|
+
gainDb: s.gainDb,
|
|
16260
|
+
density: s.density,
|
|
16261
|
+
rtf: s.rtf
|
|
16262
|
+
} : {},
|
|
16263
|
+
...c.status === "done" ? { lines: c.segments.length } : {}
|
|
16264
|
+
});
|
|
16265
|
+
};
|
|
15221
16266
|
const q = {
|
|
15222
16267
|
snapshot: snap,
|
|
15223
16268
|
subscribe(fn) {
|
|
@@ -15262,7 +16307,8 @@ function transcribeQueue(o) {
|
|
|
15262
16307
|
try {
|
|
15263
16308
|
const r = await run(file, {
|
|
15264
16309
|
signal: ac.signal,
|
|
15265
|
-
onProgress: (f) => patch(next.id, { pct: Math.round(Math.min(1, Math.max(0, f)) * 100) })
|
|
16310
|
+
onProgress: (f) => patch(next.id, { pct: Math.round(Math.min(1, Math.max(0, f)) * 100) }),
|
|
16311
|
+
...o.options ? { options: o.options() } : {}
|
|
15266
16312
|
});
|
|
15267
16313
|
const c = channels.find((x) => x.id === next.id);
|
|
15268
16314
|
if (!c) continue;
|
|
@@ -15276,6 +16322,8 @@ function transcribeQueue(o) {
|
|
|
15276
16322
|
if (r.duration !== void 0) done.duration = r.duration;
|
|
15277
16323
|
if (r.language) done.language = r.language;
|
|
15278
16324
|
if (r.model) done.model = r.model;
|
|
16325
|
+
if (r.stats) done.stats = r.stats;
|
|
16326
|
+
if (r.rail) done.rail = r.rail;
|
|
15279
16327
|
done.meta = metaOf(done);
|
|
15280
16328
|
patch(next.id, done);
|
|
15281
16329
|
if (selected === null || channels.find((x) => x.id === selected)?.status !== "done") selected = next.id;
|
|
@@ -15378,6 +16426,30 @@ function transcribeQueue(o) {
|
|
|
15378
16426
|
type: TYPES[kind]
|
|
15379
16427
|
};
|
|
15380
16428
|
},
|
|
16429
|
+
zip(zo = {}) {
|
|
16430
|
+
const done = channels.filter((c) => c.segments.length);
|
|
16431
|
+
if (!done.length) return null;
|
|
16432
|
+
const docs = done.map((c) => ({
|
|
16433
|
+
source: c.name,
|
|
16434
|
+
segments: c.segments,
|
|
16435
|
+
dropped: c.dropped,
|
|
16436
|
+
model: c.model ?? "unknown",
|
|
16437
|
+
language: c.language ?? "unknown",
|
|
16438
|
+
...c.stats ? { stats: c.stats } : {},
|
|
16439
|
+
...c.duration !== void 0 ? { duration: c.duration } : {}
|
|
16440
|
+
}));
|
|
16441
|
+
const capture = o.options?.() ?? null;
|
|
16442
|
+
const out = bundle(docs, {
|
|
16443
|
+
...zo.tool ? { tool: zo.tool } : {},
|
|
16444
|
+
capture,
|
|
16445
|
+
...zo.date ? { date: zo.date } : {}
|
|
16446
|
+
});
|
|
16447
|
+
return {
|
|
16448
|
+
name: `transcripts-${(zo.date ?? /* @__PURE__ */ new Date()).toISOString().slice(0, 10)}.zip`,
|
|
16449
|
+
data: out.zip,
|
|
16450
|
+
manifest: out.manifest
|
|
16451
|
+
};
|
|
16452
|
+
},
|
|
15381
16453
|
setTranscriber(t) {
|
|
15382
16454
|
run = t;
|
|
15383
16455
|
}
|
|
@@ -17463,16 +18535,35 @@ function asImage(img) {
|
|
|
17463
18535
|
c.getContext("2d").putImageData(new ImageData(new Uint8ClampedArray(p.data), p.width, p.height), 0, 0);
|
|
17464
18536
|
return c;
|
|
17465
18537
|
}
|
|
17466
|
-
/** A file as 16 kHz
|
|
18538
|
+
/** A file as 16 kHz samples per channel (at most two), decoded by the browser. */
|
|
17467
18539
|
async function pcm16k(file) {
|
|
17468
18540
|
const bytes = await file.arrayBuffer();
|
|
17469
18541
|
const buf = await new OfflineAudioContext(1, 1, 16e3).decodeAudioData(bytes);
|
|
17470
|
-
const
|
|
18542
|
+
const ch = Math.min(2, buf.numberOfChannels);
|
|
18543
|
+
const off = new OfflineAudioContext(ch, Math.max(1, Math.ceil(buf.duration * 16e3)), 16e3);
|
|
17471
18544
|
const src = off.createBufferSource();
|
|
17472
18545
|
src.buffer = buf;
|
|
17473
18546
|
src.connect(off.destination);
|
|
17474
18547
|
src.start();
|
|
17475
|
-
|
|
18548
|
+
const out = await off.startRendering();
|
|
18549
|
+
return Array.from({ length: ch }, (_, i) => out.getChannelData(i));
|
|
18550
|
+
}
|
|
18551
|
+
/** A level rail folded to at most `bins` bins: loudest frame and whether the gate kept most of it. */
|
|
18552
|
+
function railOf(rail, bins = 400) {
|
|
18553
|
+
const n = Math.max(1, Math.min(bins, rail.db.length));
|
|
18554
|
+
const db = new Array(n).fill(-90);
|
|
18555
|
+
const kept = new Array(n).fill(0);
|
|
18556
|
+
const count = new Array(n).fill(0);
|
|
18557
|
+
for (let i = 0; i < rail.db.length; i++) {
|
|
18558
|
+
const b = Math.min(n - 1, Math.floor(i * n / rail.db.length));
|
|
18559
|
+
db[b] = Math.max(db[b], rail.db[i]);
|
|
18560
|
+
kept[b] += rail.open[i];
|
|
18561
|
+
count[b]++;
|
|
18562
|
+
}
|
|
18563
|
+
return {
|
|
18564
|
+
db: db.map((x) => Math.round(x * 10) / 10),
|
|
18565
|
+
kept: kept.map((k, b) => count[b] && k / count[b] >= .5 ? 1 : 0)
|
|
18566
|
+
};
|
|
17476
18567
|
}
|
|
17477
18568
|
async function dspOf(rt) {
|
|
17478
18569
|
rt.dsp ??= rt.modules.dsp ? rt.modules.dsp() : Promise.all([Promise.resolve().then(() => (init_src$5(), src_exports$4)), Promise.resolve().then(() => (init_inline(), inline_exports))]).then(([a, i]) => a.loadDsp({ base64: i.AUDIO_DSP_WASM_BASE64 }));
|
|
@@ -17515,9 +18606,11 @@ async function adapt(job, rec, value, rt, ctx) {
|
|
|
17515
18606
|
case "transcribe": {
|
|
17516
18607
|
const t = await Promise.resolve().then(() => (init_src$3(), src_exports$2));
|
|
17517
18608
|
const run = async (file, c) => {
|
|
17518
|
-
const [dsp,
|
|
17519
|
-
const
|
|
17520
|
-
const
|
|
18609
|
+
const [dsp, chans] = await Promise.all([dspOf(rt), pcm16k(file)]);
|
|
18610
|
+
const { channel, ...run } = c.options ?? {};
|
|
18611
|
+
const lang = run.language ?? ctx.options.language;
|
|
18612
|
+
const out = await t.transcribe(hub, dsp, t.toMono(chans, channel ?? "auto"), {
|
|
18613
|
+
...run,
|
|
17521
18614
|
model: rec.id,
|
|
17522
18615
|
signal: c.signal,
|
|
17523
18616
|
...lang ? { language: lang } : {},
|
|
@@ -17528,7 +18621,9 @@ async function adapt(job, rec, value, rt, ctx) {
|
|
|
17528
18621
|
dropped: out.dropped,
|
|
17529
18622
|
duration: out.stats.dur,
|
|
17530
18623
|
language: out.language,
|
|
17531
|
-
model: rec.label ?? rec.id
|
|
18624
|
+
model: rec.label ?? rec.id,
|
|
18625
|
+
stats: out.stats,
|
|
18626
|
+
rail: railOf(out.rail)
|
|
17532
18627
|
};
|
|
17533
18628
|
};
|
|
17534
18629
|
return run;
|
|
@@ -17568,6 +18663,30 @@ async function adapt(job, rec, value, rt, ctx) {
|
|
|
17568
18663
|
model: rec.id,
|
|
17569
18664
|
key: keyOf(image)
|
|
17570
18665
|
}))[0];
|
|
18666
|
+
if (rec.provides?.includes("segment.everything")) {
|
|
18667
|
+
let last = null;
|
|
18668
|
+
const all = async (image, o = {}) => {
|
|
18669
|
+
const key = JSON.stringify(o);
|
|
18670
|
+
if (last?.img !== image || last.key !== key) last = {
|
|
18671
|
+
img: image,
|
|
18672
|
+
key,
|
|
18673
|
+
list: await v.segmentEverything(hub, asImage(image), {
|
|
18674
|
+
...o,
|
|
18675
|
+
model: rec.id
|
|
18676
|
+
})
|
|
18677
|
+
};
|
|
18678
|
+
return last.list;
|
|
18679
|
+
};
|
|
18680
|
+
return {
|
|
18681
|
+
segment: async (image, p) => v.instanceAt(await all(image), p.x, p.y)?.mask.data ?? null,
|
|
18682
|
+
everything: async (image, o) => (await all(image, o)).map((i, k) => ({
|
|
18683
|
+
mask: i.mask.data,
|
|
18684
|
+
label: i.label ?? `object ${k + 1}`,
|
|
18685
|
+
score: i.score,
|
|
18686
|
+
box: i.box
|
|
18687
|
+
}))
|
|
18688
|
+
};
|
|
18689
|
+
}
|
|
17571
18690
|
return {
|
|
17572
18691
|
segment: async (image, p) => (await first(image, p.x, p.y))?.data ?? null,
|
|
17573
18692
|
everything: async (image) => {
|
|
@@ -17646,4 +18765,301 @@ function jobLoader(job, o = {}) {
|
|
|
17646
18765
|
}
|
|
17647
18766
|
};
|
|
17648
18767
|
}
|
|
17649
|
-
|
|
18768
|
+
// packages/ai-views/src/speaker.ts
|
|
18769
|
+
init_src$4();
|
|
18770
|
+
/** True when the engine clones a voice from reference audio. */
|
|
18771
|
+
const canClone = (e) => !!e && typeof e.cloneVoice === "function";
|
|
18772
|
+
/** Pocket takes its reference at this rate (vendor). */
|
|
18773
|
+
const RATE = 24e3;
|
|
18774
|
+
/** A speaker over the engine `engine()` returns at each line (null: text only, timed by `play`). */
|
|
18775
|
+
function speaker(engine, o = {}) {
|
|
18776
|
+
let ac = null;
|
|
18777
|
+
let chain = Promise.resolve();
|
|
18778
|
+
let ctl = new AbortController();
|
|
18779
|
+
let ref = null;
|
|
18780
|
+
const subs = /* @__PURE__ */ new Set();
|
|
18781
|
+
const emit = () => subs.forEach((f) => f());
|
|
18782
|
+
const audio = () => ac ??= new AudioContext();
|
|
18783
|
+
const config = () => ref ? { voice: "custom" } : o.config?.() ?? {};
|
|
18784
|
+
const speak = async (text, signal) => {
|
|
18785
|
+
if (signal.aborted) return;
|
|
18786
|
+
o.onLine?.(text);
|
|
18787
|
+
const e = engine();
|
|
18788
|
+
const play = o.play ?? (async () => void 0);
|
|
18789
|
+
if (!e) return void await play({ text });
|
|
18790
|
+
if (e.kind === "native") {
|
|
18791
|
+
e.speak(text, config(), { onBoundary: () => void 0 }, signal);
|
|
18792
|
+
await play({ text });
|
|
18793
|
+
return;
|
|
18794
|
+
}
|
|
18795
|
+
const r = await renderSpeech(e, text, {
|
|
18796
|
+
voice: config(),
|
|
18797
|
+
signal
|
|
18798
|
+
});
|
|
18799
|
+
if (signal.aborted) return;
|
|
18800
|
+
await play({
|
|
18801
|
+
text,
|
|
18802
|
+
audio: renderedBuffer(audio(), r),
|
|
18803
|
+
words: r.words,
|
|
18804
|
+
phonemes: r.phonemes
|
|
18805
|
+
});
|
|
18806
|
+
};
|
|
18807
|
+
const decode = async (blob) => {
|
|
18808
|
+
const buf = await audio().decodeAudioData(await blob.arrayBuffer());
|
|
18809
|
+
return {
|
|
18810
|
+
samples: toMono$1(Array.from({ length: buf.numberOfChannels }, (_, i) => buf.getChannelData(i))),
|
|
18811
|
+
rate: buf.sampleRate
|
|
18812
|
+
};
|
|
18813
|
+
};
|
|
18814
|
+
const clone = async (samples, rate) => {
|
|
18815
|
+
const e = engine();
|
|
18816
|
+
if (!canClone(e)) throw new Error("this voice cannot clone. load pocket in the voice list first.");
|
|
18817
|
+
const next = prepareClone(samples, rate);
|
|
18818
|
+
await e.cloneVoice(next, RATE);
|
|
18819
|
+
ref = next;
|
|
18820
|
+
emit();
|
|
18821
|
+
return ref.length / RATE;
|
|
18822
|
+
};
|
|
18823
|
+
return {
|
|
18824
|
+
get cloned() {
|
|
18825
|
+
return !!ref;
|
|
18826
|
+
},
|
|
18827
|
+
subscribe(fn) {
|
|
18828
|
+
subs.add(fn);
|
|
18829
|
+
return () => subs.delete(fn);
|
|
18830
|
+
},
|
|
18831
|
+
say(text) {
|
|
18832
|
+
const signal = ctl.signal;
|
|
18833
|
+
chain = chain.then(() => speak(text, signal)).catch(() => void 0);
|
|
18834
|
+
return chain;
|
|
18835
|
+
},
|
|
18836
|
+
stop() {
|
|
18837
|
+
ctl.abort();
|
|
18838
|
+
ctl = new AbortController();
|
|
18839
|
+
chain = Promise.resolve();
|
|
18840
|
+
o.halt?.();
|
|
18841
|
+
},
|
|
18842
|
+
async cloneFile(file) {
|
|
18843
|
+
const { samples, rate } = await decode(file);
|
|
18844
|
+
return clone(samples, rate);
|
|
18845
|
+
},
|
|
18846
|
+
async cloneMic(seconds, onTick) {
|
|
18847
|
+
const media = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
18848
|
+
try {
|
|
18849
|
+
const rec = new MediaRecorder(media);
|
|
18850
|
+
const parts = [];
|
|
18851
|
+
rec.ondataavailable = (ev) => parts.push(ev.data);
|
|
18852
|
+
const done = new Promise((ok) => rec.onstop = () => ok());
|
|
18853
|
+
rec.start();
|
|
18854
|
+
for (let left = seconds; left > 0; left--) {
|
|
18855
|
+
onTick?.(left);
|
|
18856
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
18857
|
+
}
|
|
18858
|
+
rec.stop();
|
|
18859
|
+
await done;
|
|
18860
|
+
const { samples, rate } = await decode(new Blob(parts, { type: rec.mimeType }));
|
|
18861
|
+
return await clone(samples, rate);
|
|
18862
|
+
} finally {
|
|
18863
|
+
media.getTracks().forEach((t) => t.stop());
|
|
18864
|
+
}
|
|
18865
|
+
},
|
|
18866
|
+
cloneWav() {
|
|
18867
|
+
if (!ref) return null;
|
|
18868
|
+
return renderedWav({
|
|
18869
|
+
samples: ref,
|
|
18870
|
+
sampleRate: RATE,
|
|
18871
|
+
duration: ref.length / RATE,
|
|
18872
|
+
engine: "clone",
|
|
18873
|
+
words: [],
|
|
18874
|
+
phonemes: []
|
|
18875
|
+
});
|
|
18876
|
+
},
|
|
18877
|
+
clearClone() {
|
|
18878
|
+
ref = null;
|
|
18879
|
+
emit();
|
|
18880
|
+
}
|
|
18881
|
+
};
|
|
18882
|
+
}
|
|
18883
|
+
// packages/ai-views/src/talk.ts
|
|
18884
|
+
init_src$8();
|
|
18885
|
+
const blank = () => ({
|
|
18886
|
+
name: "assistant",
|
|
18887
|
+
profile: "",
|
|
18888
|
+
notes: "",
|
|
18889
|
+
rules: ""
|
|
18890
|
+
});
|
|
18891
|
+
/** Creates a persona rack with built in presets and any saved ones; `onSave` keeps the saved ones. */
|
|
18892
|
+
function personaRack(o = {}) {
|
|
18893
|
+
const builtIn = [...o.presets ?? []];
|
|
18894
|
+
let saved = [...o.saved ?? []];
|
|
18895
|
+
let persona = {
|
|
18896
|
+
...blank(),
|
|
18897
|
+
...o.persona ?? builtIn[0]?.persona ?? {}
|
|
18898
|
+
};
|
|
18899
|
+
let on = o.persona ? -1 : builtIn.length ? 0 : -1;
|
|
18900
|
+
const subs = /* @__PURE__ */ new Set();
|
|
18901
|
+
const all = () => [...builtIn, ...saved];
|
|
18902
|
+
const snap = () => ({
|
|
18903
|
+
persona,
|
|
18904
|
+
presets: all(),
|
|
18905
|
+
on,
|
|
18906
|
+
builtIn: builtIn.length
|
|
18907
|
+
});
|
|
18908
|
+
const emit = () => subs.forEach((f) => f(snap()));
|
|
18909
|
+
return {
|
|
18910
|
+
snapshot: snap,
|
|
18911
|
+
subscribe(fn) {
|
|
18912
|
+
subs.add(fn);
|
|
18913
|
+
fn(snap());
|
|
18914
|
+
return () => subs.delete(fn);
|
|
18915
|
+
},
|
|
18916
|
+
set(p) {
|
|
18917
|
+
persona = {
|
|
18918
|
+
...persona,
|
|
18919
|
+
...p
|
|
18920
|
+
};
|
|
18921
|
+
on = -1;
|
|
18922
|
+
emit();
|
|
18923
|
+
},
|
|
18924
|
+
load(i) {
|
|
18925
|
+
const p = all()[i];
|
|
18926
|
+
if (!p) return;
|
|
18927
|
+
persona = {
|
|
18928
|
+
...blank(),
|
|
18929
|
+
...p.persona
|
|
18930
|
+
};
|
|
18931
|
+
on = i;
|
|
18932
|
+
emit();
|
|
18933
|
+
},
|
|
18934
|
+
save(label) {
|
|
18935
|
+
const name = label.trim() || persona.name || "preset";
|
|
18936
|
+
saved = [...saved.filter((s) => s.label !== name), {
|
|
18937
|
+
label: name,
|
|
18938
|
+
persona: { ...persona }
|
|
18939
|
+
}];
|
|
18940
|
+
on = builtIn.length + saved.length - 1;
|
|
18941
|
+
o.onSave?.(saved);
|
|
18942
|
+
emit();
|
|
18943
|
+
},
|
|
18944
|
+
removePreset(i) {
|
|
18945
|
+
const j = i - builtIn.length;
|
|
18946
|
+
if (j < 0 || j >= saved.length) return;
|
|
18947
|
+
saved = saved.filter((_, k) => k !== j);
|
|
18948
|
+
if (on === i) on = -1;
|
|
18949
|
+
o.onSave?.(saved);
|
|
18950
|
+
emit();
|
|
18951
|
+
},
|
|
18952
|
+
system(mem = [], budgetTokens = 3e3) {
|
|
18953
|
+
const a = budget({
|
|
18954
|
+
budgetTokens,
|
|
18955
|
+
sections: [
|
|
18956
|
+
{
|
|
18957
|
+
name: "profile",
|
|
18958
|
+
priority: 40,
|
|
18959
|
+
text: persona.profile.trim()
|
|
18960
|
+
},
|
|
18961
|
+
{
|
|
18962
|
+
name: "memory",
|
|
18963
|
+
priority: 30,
|
|
18964
|
+
text: mem.length ? `things you remember:\n${mem.map((m) => `- ${m.label}`).join("\n")}` : ""
|
|
18965
|
+
},
|
|
18966
|
+
{
|
|
18967
|
+
name: "notes",
|
|
18968
|
+
priority: 20,
|
|
18969
|
+
text: persona.notes.trim() ? `notes:\n${persona.notes.trim()}` : ""
|
|
18970
|
+
},
|
|
18971
|
+
{
|
|
18972
|
+
name: "rules",
|
|
18973
|
+
priority: 10,
|
|
18974
|
+
text: persona.rules.trim() ? `rules:\n${persona.rules.trim()}` : ""
|
|
18975
|
+
}
|
|
18976
|
+
].filter((s) => s.text)
|
|
18977
|
+
});
|
|
18978
|
+
return {
|
|
18979
|
+
text: a.kept.includes("profile") || !persona.profile.trim() ? a.text : [persona.profile.trim(), a.text].filter(Boolean).join("\n\n"),
|
|
18980
|
+
dropped: a.dropped
|
|
18981
|
+
};
|
|
18982
|
+
}
|
|
18983
|
+
};
|
|
18984
|
+
}
|
|
18985
|
+
/** Creates the voice state over a native engine (`webSpeechEngine()` of @hackbuild/speak). */
|
|
18986
|
+
function voiceRow(engine, o = {}) {
|
|
18987
|
+
let s = {
|
|
18988
|
+
auto: o.auto ?? false,
|
|
18989
|
+
voice: o.voice ?? "",
|
|
18990
|
+
rate: o.rate ?? 1,
|
|
18991
|
+
pitch: o.pitch ?? 1,
|
|
18992
|
+
speaking: false,
|
|
18993
|
+
error: ""
|
|
18994
|
+
};
|
|
18995
|
+
let ac = null;
|
|
18996
|
+
const subs = /* @__PURE__ */ new Set();
|
|
18997
|
+
const snap = () => ({
|
|
18998
|
+
...s,
|
|
18999
|
+
voices: engine.voices?.() ?? []
|
|
19000
|
+
});
|
|
19001
|
+
const emit = () => subs.forEach((f) => f(snap()));
|
|
19002
|
+
globalThis.speechSynthesis?.addEventListener?.("voiceschanged", emit);
|
|
19003
|
+
const row = {
|
|
19004
|
+
snapshot: snap,
|
|
19005
|
+
subscribe(fn) {
|
|
19006
|
+
subs.add(fn);
|
|
19007
|
+
fn(snap());
|
|
19008
|
+
return () => subs.delete(fn);
|
|
19009
|
+
},
|
|
19010
|
+
set(p) {
|
|
19011
|
+
s = {
|
|
19012
|
+
...s,
|
|
19013
|
+
...p
|
|
19014
|
+
};
|
|
19015
|
+
o.onChange?.({
|
|
19016
|
+
auto: s.auto,
|
|
19017
|
+
voice: s.voice,
|
|
19018
|
+
rate: s.rate,
|
|
19019
|
+
pitch: s.pitch
|
|
19020
|
+
});
|
|
19021
|
+
emit();
|
|
19022
|
+
},
|
|
19023
|
+
async speak(text) {
|
|
19024
|
+
row.stop();
|
|
19025
|
+
const mine = ac = new AbortController();
|
|
19026
|
+
s = {
|
|
19027
|
+
...s,
|
|
19028
|
+
speaking: true,
|
|
19029
|
+
error: ""
|
|
19030
|
+
};
|
|
19031
|
+
emit();
|
|
19032
|
+
try {
|
|
19033
|
+
await engine.load({ signal: mine.signal });
|
|
19034
|
+
await engine.speak(text, {
|
|
19035
|
+
...s.voice ? { voice: s.voice } : {},
|
|
19036
|
+
rate: s.rate,
|
|
19037
|
+
pitch: s.pitch
|
|
19038
|
+
}, { onBoundary: () => void 0 }, mine.signal);
|
|
19039
|
+
} catch (e) {
|
|
19040
|
+
if (!mine.signal.aborted) s = {
|
|
19041
|
+
...s,
|
|
19042
|
+
error: e instanceof Error ? e.message : String(e)
|
|
19043
|
+
};
|
|
19044
|
+
} finally {
|
|
19045
|
+
if (ac === mine) {
|
|
19046
|
+
ac = null;
|
|
19047
|
+
s = {
|
|
19048
|
+
...s,
|
|
19049
|
+
speaking: false
|
|
19050
|
+
};
|
|
19051
|
+
emit();
|
|
19052
|
+
}
|
|
19053
|
+
}
|
|
19054
|
+
},
|
|
19055
|
+
stop() {
|
|
19056
|
+
ac?.abort();
|
|
19057
|
+
engine.stop();
|
|
19058
|
+
},
|
|
19059
|
+
reply(text) {
|
|
19060
|
+
if (s.auto && text.trim()) row.speak(text);
|
|
19061
|
+
}
|
|
19062
|
+
};
|
|
19063
|
+
return row;
|
|
19064
|
+
}
|
|
19065
|
+
export { ALL_RECORDS, FACE_API_BASE, JOBS, JOB_NAMES, NO_ENGINE, RUNTIME_URLS, asImage, backendOf, boxWords, canClone, chatList, chatThread, contextSegments, defaultPick, detailOf, fitMap, frameTag, groupOf, inspectorHtml, isJob, isNoDownload, isUncensored, jobLoader, jobRecords, labelColour, optionsOf, ownRecord, personaRack, rowOf, sizeWords, speaker, storedIds, toView, traceRows, transcribeQueue, useJobsPick, useModelPick, verifyRepo, voiceRow, weightKeys, weightsLoader, weightsRemover };
|