@try-works/tool-mode-runtime-litert 0.0.0-stage → 0.1.1
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/lib/ear-frontend.d.ts +113 -0
- package/lib/ear-frontend.d.ts.map +1 -0
- package/lib/ear-frontend.js +258 -0
- package/lib/ear.d.ts +51 -0
- package/lib/ear.d.ts.map +1 -0
- package/lib/ear.js +296 -0
- package/lib/emo-tokenizer.d.ts +87 -0
- package/lib/emo-tokenizer.d.ts.map +1 -0
- package/lib/emo-tokenizer.js +365 -0
- package/lib/emo.d.ts +53 -0
- package/lib/emo.d.ts.map +1 -0
- package/lib/emo.js +218 -0
- package/lib/errors.d.ts +14 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +29 -0
- package/lib/index.d.ts +28 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +34 -0
- package/lib/model.d.ts +103 -0
- package/lib/model.d.ts.map +1 -0
- package/lib/model.js +112 -0
- package/lib/moderator.d.ts +59 -0
- package/lib/moderator.d.ts.map +1 -0
- package/lib/moderator.js +189 -0
- package/lib/pillow.d.ts +75 -0
- package/lib/pillow.d.ts.map +1 -0
- package/lib/pillow.js +187 -0
- package/lib/png.d.ts +27 -0
- package/lib/png.d.ts.map +1 -0
- package/lib/png.js +176 -0
- package/lib/processor.d.ts +43 -0
- package/lib/processor.d.ts.map +1 -0
- package/lib/processor.js +48 -0
- package/lib/provider-factory.d.ts +44 -0
- package/lib/provider-factory.d.ts.map +1 -0
- package/lib/provider-factory.js +219 -0
- package/lib/runtime.d.ts +41 -0
- package/lib/runtime.d.ts.map +1 -0
- package/lib/runtime.js +66 -0
- package/lib/shapes.d.ts +28 -0
- package/lib/shapes.d.ts.map +1 -0
- package/lib/shapes.js +187 -0
- package/lib/shim.d.ts +45 -0
- package/lib/shim.d.ts.map +1 -0
- package/lib/shim.js +215 -0
- package/lib/wav.d.ts +39 -0
- package/lib/wav.d.ts.map +1 -0
- package/lib/wav.js +134 -0
- package/package.json +47 -4
- package/README.md +0 -3
package/lib/errors.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed runtime failures, re-exported from the shared contract package.
|
|
3
|
+
*
|
|
4
|
+
* `@try-works/tool-mode-runtime-core` owns the tagged-error classes for the whole runtime
|
|
5
|
+
* layer. This package imports the same class VALUES rather than redeclaring them, so an
|
|
6
|
+
* `instanceof` or `catchTag` in any consumer matches the error it saw — two structurally
|
|
7
|
+
* identical copies would not. `describeCause` stays local: it is a message helper, not part
|
|
8
|
+
* of the error protocol.
|
|
9
|
+
*/
|
|
10
|
+
import { Schema } from 'effect';
|
|
11
|
+
import { InferenceFailed, RuntimeUnavailable } from '@try-works/tool-mode-runtime-core';
|
|
12
|
+
export { InferenceFailed, RuntimeUnavailable };
|
|
13
|
+
/** A short, always-defined description of an unknown thrown value. */
|
|
14
|
+
export const describeCause = (cause) => {
|
|
15
|
+
if (cause instanceof Error)
|
|
16
|
+
return cause.message === '' ? cause.name : cause.message;
|
|
17
|
+
if (Schema.is(Schema.String)(cause))
|
|
18
|
+
return cause;
|
|
19
|
+
if (cause === undefined || cause === null)
|
|
20
|
+
return String(cause);
|
|
21
|
+
try {
|
|
22
|
+
const encoded = JSON.stringify(cause);
|
|
23
|
+
return encoded ?? String(cause);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return String(cause);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=errors.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@try-works/tool-mode-runtime-litert` — a socket-free LiteRT.js runtime.
|
|
3
|
+
*
|
|
4
|
+
* Public surface:
|
|
5
|
+
* - `installLitertFetchShim` / `resolveLitertWasmDir` — the proven node boot shim.
|
|
6
|
+
* - `createNodeLitertBoot` — the real boot, or inject a fake `LitertBoot` in tests.
|
|
7
|
+
* - `LitertSession` / `litertLayer` — the Effect service over one compiled model.
|
|
8
|
+
* - `createProviderFromRecipe` — the Phase 2 factory a code-free model recipe names; it owns
|
|
9
|
+
* the engine and the processors this package implements (shapes/points-v1,
|
|
10
|
+
* moderator/crops-v1, ear/mel-v1 and emo/text-v1).
|
|
11
|
+
* - `InferenceFailed` / `RuntimeUnavailable` — the typed failures.
|
|
12
|
+
*
|
|
13
|
+
* The redact extension calls `litertLayer({ wasmDir, modelBytes })` to provide
|
|
14
|
+
* `LitertSession`; it never talks to `@litertjs/core` directly.
|
|
15
|
+
*/
|
|
16
|
+
export { InferenceFailed, RuntimeUnavailable } from '@try-works/tool-mode-runtime-core';
|
|
17
|
+
export { describeCause } from './errors.ts';
|
|
18
|
+
export { createLoadLiteRtGuard, createNodeLitertBoot, processLoadLiteRtGuard, type CompiledLitertModel, type LiteRtCompileOptions, type LiteRtCompiledModelLike, type LiteRtRuntimeModule, type LiteRtTensorDetail, type LiteRtTensorLike, type LitertBoot, type LitertFeed, type LitertLoadOptions, type LitertOutput, type LoadLiteRtGuard, type NodeLitertBootOptions, } from './model.ts';
|
|
19
|
+
export { SHAPES_CLASSES, SHAPES_CLASS_COUNT, SHAPES_POINT_COUNT, SHAPES_POINTS_PROCESSOR, shapesPointsProcessor, type ShapesClass, } from './shapes.ts';
|
|
20
|
+
export { MODERATOR_DEFAULT_POLICY, MODERATOR_DEFAULT_QUALITY, MODERATOR_DEFAULT_THRESHOLD, MODERATOR_HEADS, MODERATOR_POLICIES, MODERATOR_PROCESSOR, MODERATOR_QUALITIES, MODERATOR_SCORE_COUNT, MODERATOR_SIDE, moderatorCropsProcessor, type ModeratorHead, type ModeratorPolicy, } from './moderator.ts';
|
|
21
|
+
export type { ModeratorQuality } from './pillow.ts';
|
|
22
|
+
export { EMO_DEFAULT_LIMIT, EMO_FMAX, EMO_LABEL_COUNT, EMO_MAX_LIMIT, EMO_MAX_TEXT_CHARACTERS, EMO_N_BUCKETS, EMO_N_HASHES, EMO_N_IMPORTANCE, EMO_PROCESSOR, EMO_SEM_PAD_INDEX, EMO_SMAX, emoTextProcessor, } from './emo.ts';
|
|
23
|
+
export { EAR_CONFUSABLE_LANGUAGES, EAR_LANGUAGE_ALIASES, EAR_LANGUAGE_COUNT, EAR_MAX_WINDOWS, EAR_PROCESSOR, EAR_RELIABLE_MARGIN, earMelProcessor, } from './ear.ts';
|
|
24
|
+
export { EAR_AFFINE_ADD, EAR_AFFINE_DIVIDE, EAR_BINS, EAR_CLAMP_MIN, EAR_DEFAULT_WINDOWS, EAR_FLOOR_DECADES, EAR_FRAMES, EAR_HANN, EAR_HOP, EAR_MELS, EAR_N_FFT, EAR_SAMPLE_RATE, EAR_WINDOW_SAMPLES, } from './ear-frontend.ts';
|
|
25
|
+
export { createProviderFromRecipe, type ProviderFactoryOptions, } from './provider-factory.ts';
|
|
26
|
+
export { LITERT_WASM_PREFIX, installLitertFetchShim, resolveLitertWasmDir, type LitertShim, } from './shim.ts';
|
|
27
|
+
export { LitertSession, litertLayer, type LitertLayerOptions, type LitertOutputs, type LitertSessionApi, } from './runtime.ts';
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAUH,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAA;AACvF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,EACzB,KAAK,uBAAuB,EAC5B,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,K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js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@try-works/tool-mode-runtime-litert` — a socket-free LiteRT.js runtime.
|
|
3
|
+
*
|
|
4
|
+
* Public surface:
|
|
5
|
+
* - `installLitertFetchShim` / `resolveLitertWasmDir` — the proven node boot shim.
|
|
6
|
+
* - `createNodeLitertBoot` — the real boot, or inject a fake `LitertBoot` in tests.
|
|
7
|
+
* - `LitertSession` / `litertLayer` — the Effect service over one compiled model.
|
|
8
|
+
* - `createProviderFromRecipe` — the Phase 2 factory a code-free model recipe names; it owns
|
|
9
|
+
* the engine and the processors this package implements (shapes/points-v1,
|
|
10
|
+
* moderator/crops-v1, ear/mel-v1 and emo/text-v1).
|
|
11
|
+
* - `InferenceFailed` / `RuntimeUnavailable` — the typed failures.
|
|
12
|
+
*
|
|
13
|
+
* The redact extension calls `litertLayer({ wasmDir, modelBytes })` to provide
|
|
14
|
+
* `LitertSession`; it never talks to `@litertjs/core` directly.
|
|
15
|
+
*/
|
|
16
|
+
/*
|
|
17
|
+
* The anti-slop no-shape rule targets structural "shape" placeholders in type names. The
|
|
18
|
+
* exported SHAPES_* constants and ShapesClass name the DOMAIN of the shapes/points-v1
|
|
19
|
+
* processor (2D geometric shapes, and the tool schema's "shape" field), so the rule is
|
|
20
|
+
* disabled for this file only.
|
|
21
|
+
*/
|
|
22
|
+
/* oxlint-disable anti-slop/no-shape-in-symbol-names */
|
|
23
|
+
export { InferenceFailed, RuntimeUnavailable } from '@try-works/tool-mode-runtime-core';
|
|
24
|
+
export { describeCause } from "./errors.js";
|
|
25
|
+
export { createLoadLiteRtGuard, createNodeLitertBoot, processLoadLiteRtGuard, } from "./model.js";
|
|
26
|
+
export { SHAPES_CLASSES, SHAPES_CLASS_COUNT, SHAPES_POINT_COUNT, SHAPES_POINTS_PROCESSOR, shapesPointsProcessor, } from "./shapes.js";
|
|
27
|
+
export { MODERATOR_DEFAULT_POLICY, MODERATOR_DEFAULT_QUALITY, MODERATOR_DEFAULT_THRESHOLD, MODERATOR_HEADS, MODERATOR_POLICIES, MODERATOR_PROCESSOR, MODERATOR_QUALITIES, MODERATOR_SCORE_COUNT, MODERATOR_SIDE, moderatorCropsProcessor, } from "./moderator.js";
|
|
28
|
+
export { EMO_DEFAULT_LIMIT, EMO_FMAX, EMO_LABEL_COUNT, EMO_MAX_LIMIT, EMO_MAX_TEXT_CHARACTERS, EMO_N_BUCKETS, EMO_N_HASHES, EMO_N_IMPORTANCE, EMO_PROCESSOR, EMO_SEM_PAD_INDEX, EMO_SMAX, emoTextProcessor, } from "./emo.js";
|
|
29
|
+
export { EAR_CONFUSABLE_LANGUAGES, EAR_LANGUAGE_ALIASES, EAR_LANGUAGE_COUNT, EAR_MAX_WINDOWS, EAR_PROCESSOR, EAR_RELIABLE_MARGIN, earMelProcessor, } from "./ear.js";
|
|
30
|
+
export { EAR_AFFINE_ADD, EAR_AFFINE_DIVIDE, EAR_BINS, EAR_CLAMP_MIN, EAR_DEFAULT_WINDOWS, EAR_FLOOR_DECADES, EAR_FRAMES, EAR_HANN, EAR_HOP, EAR_MELS, EAR_N_FFT, EAR_SAMPLE_RATE, EAR_WINDOW_SAMPLES, } from "./ear-frontend.js";
|
|
31
|
+
export { createProviderFromRecipe, } from "./provider-factory.js";
|
|
32
|
+
export { LITERT_WASM_PREFIX, installLitertFetchShim, resolveLitertWasmDir, } from "./shim.js";
|
|
33
|
+
export { LitertSession, litertLayer, } from "./runtime.js";
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
package/lib/model.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The injectable LiteRT boot boundary and the real node implementation.
|
|
3
|
+
*
|
|
4
|
+
* The Effect service in runtime.ts depends on `LitertBoot`, not on `@litertjs/core`.
|
|
5
|
+
* Unit tests inject a fake compiled model, so they prove the lifecycle, the Tensor
|
|
6
|
+
* mapping and the serialization without loading Wasm. `createNodeLitertBoot` is the real
|
|
7
|
+
* path: it installs the socket-free shim (shim.ts), loads the Wasm module exactly once per
|
|
8
|
+
* process, compiles one model, and maps feeds to Tensors.
|
|
9
|
+
*/
|
|
10
|
+
import { type LitertShim } from './shim.ts';
|
|
11
|
+
/** One named model input: its typed data and its dimensions. */
|
|
12
|
+
export interface LitertFeed {
|
|
13
|
+
readonly data: Float32Array | Int32Array;
|
|
14
|
+
readonly dims: readonly number[];
|
|
15
|
+
}
|
|
16
|
+
/** A model output this runtime can hand to processors. */
|
|
17
|
+
export type LitertOutput = Float32Array | Int32Array;
|
|
18
|
+
/** A compiled model, independent of the `@litertjs/core` class shapes. */
|
|
19
|
+
export interface CompiledLitertModel {
|
|
20
|
+
readonly inputNames: readonly string[];
|
|
21
|
+
readonly outputNames: readonly string[];
|
|
22
|
+
readonly run: (feeds: Readonly<Record<string, LitertFeed>>) => Promise<Readonly<Record<string, LitertOutput>>>;
|
|
23
|
+
readonly close: () => void;
|
|
24
|
+
}
|
|
25
|
+
/** The injectable boot boundary: load and compile one model. */
|
|
26
|
+
export interface LitertBoot {
|
|
27
|
+
readonly load: (options: LitertLoadOptions) => Promise<CompiledLitertModel>;
|
|
28
|
+
}
|
|
29
|
+
/** Options for one boot. */
|
|
30
|
+
export interface LitertLoadOptions {
|
|
31
|
+
readonly modelBytes: Uint8Array;
|
|
32
|
+
readonly threads?: number;
|
|
33
|
+
}
|
|
34
|
+
/** Compile options passed to `loadAndCompile`; cpu/Wasm only, by contract. */
|
|
35
|
+
export interface LiteRtCompileOptions {
|
|
36
|
+
readonly accelerator?: 'wasm' | 'webgpu' | 'webnn';
|
|
37
|
+
readonly cpuOptions?: {
|
|
38
|
+
readonly numThreads?: number;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** The subset of a `@litertjs/core` Tensor this runtime uses. */
|
|
42
|
+
export interface LiteRtTensorLike {
|
|
43
|
+
toTypedArray: () => Float32Array | Int32Array;
|
|
44
|
+
delete: () => void;
|
|
45
|
+
}
|
|
46
|
+
/** One model input/output detail as `@litertjs/core` reports it. */
|
|
47
|
+
export interface LiteRtTensorDetail {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
}
|
|
50
|
+
/** The subset of a `@litertjs/core` CompiledModel this runtime uses. */
|
|
51
|
+
export interface LiteRtCompiledModelLike {
|
|
52
|
+
getInputDetails: () => readonly LiteRtTensorDetail[];
|
|
53
|
+
getOutputDetails: () => readonly LiteRtTensorDetail[];
|
|
54
|
+
run: (input: Record<string, LiteRtTensorLike>) => Promise<Record<string, LiteRtTensorLike>>;
|
|
55
|
+
delete: () => void;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The subset of the `@litertjs/core` module this runtime calls. Injected in tests so the
|
|
59
|
+
* real Wasm module is never imported by the unit suite.
|
|
60
|
+
*/
|
|
61
|
+
export interface LiteRtRuntimeModule {
|
|
62
|
+
loadLiteRt: (path: string, options?: {
|
|
63
|
+
readonly jspi?: boolean;
|
|
64
|
+
readonly threads?: boolean;
|
|
65
|
+
}) => Promise<void>;
|
|
66
|
+
loadAndCompile: (model: Uint8Array, options?: LiteRtCompileOptions) => Promise<LiteRtCompiledModelLike>;
|
|
67
|
+
readonly Tensor: new (data: Float32Array | Int32Array, dims: number[]) => LiteRtTensorLike;
|
|
68
|
+
}
|
|
69
|
+
/** A one-shot guard around `loadLiteRt`. */
|
|
70
|
+
export interface LoadLiteRtGuard {
|
|
71
|
+
readonly run: <A>(load: () => Promise<A>) => Promise<A>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* `loadLiteRt` installs a process-global Wasm module and a default environment. Calling it
|
|
75
|
+
* a second time would rebind that state under the first model, so this guard runs the first
|
|
76
|
+
* load and refuses every later one for the life of the process. A failed load also keeps
|
|
77
|
+
* the guard closed: the module state is unknown, and retrying is not safe.
|
|
78
|
+
*/
|
|
79
|
+
export declare const createLoadLiteRtGuard: () => LoadLiteRtGuard;
|
|
80
|
+
/** The guard shared by every real boot in this process. */
|
|
81
|
+
export declare const processLoadLiteRtGuard: LoadLiteRtGuard;
|
|
82
|
+
/** Test seams for `createNodeLitertBoot`; production callers omit all of them. */
|
|
83
|
+
export interface NodeLitertBootOptions {
|
|
84
|
+
/** Defaults to `resolveLitertWasmDir()`, resolved lazily on load. */
|
|
85
|
+
readonly wasmDir?: string;
|
|
86
|
+
/** Default thread count for `loadAndCompile` when a load does not pass one. */
|
|
87
|
+
readonly threads?: number;
|
|
88
|
+
/** Inject a fake module instead of importing `@litertjs/core`. */
|
|
89
|
+
readonly loadRuntime?: () => Promise<LiteRtRuntimeModule>;
|
|
90
|
+
/** Inject a fake shim installer. */
|
|
91
|
+
readonly installShim?: (options: {
|
|
92
|
+
wasmDir: string;
|
|
93
|
+
}) => LitertShim;
|
|
94
|
+
/** Inject a fresh guard so a test does not consume the process default. */
|
|
95
|
+
readonly guard?: LoadLiteRtGuard;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The real boot: shim → `loadLiteRt` (once) → `loadAndCompile` (one model) → a Tensor
|
|
99
|
+
* mapping. Every failure is reported as `RuntimeUnavailable`, including the refusal of a
|
|
100
|
+
* second `loadLiteRt` in the same process.
|
|
101
|
+
*/
|
|
102
|
+
export declare const createNodeLitertBoot: (options?: NodeLitertBootOptions) => LitertBoot;
|
|
103
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH,OAAO,EAIL,KAAK,UAAU,EAChB,MAAM,WAAW,CAAA;AAIlB,gEAAgE;AAChE,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,UAAU,CAAA;IACxC,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAA;CACjC;AAED,0DAA0D;AAC1D,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,UAAU,CAAA;AAEpD,0EAA0E;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B,CAAC,CAAA;IACzD,oCAAoC;IACpC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,KAAK,UAAU,CAAA;IACnE,2EAA2E;IAC3E,QAAQ,CAAC,KAAK,CAAC,EAAE,eAAe,CAAA;CACjC;AA+CD;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,GAAI,UAAS,qBAA0B,KAAG,UA4B1E,CAAA"}
|
package/lib/model.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The injectable LiteRT boot boundary and the real node implementation.
|
|
3
|
+
*
|
|
4
|
+
* The Effect service in runtime.ts depends on `LitertBoot`, not on `@litertjs/core`.
|
|
5
|
+
* Unit tests inject a fake compiled model, so they prove the lifecycle, the Tensor
|
|
6
|
+
* mapping and the serialization without loading Wasm. `createNodeLitertBoot` is the real
|
|
7
|
+
* path: it installs the socket-free shim (shim.ts), loads the Wasm module exactly once per
|
|
8
|
+
* process, compiles one model, and maps feeds to Tensors.
|
|
9
|
+
*/
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { RuntimeUnavailable, describeCause } from "./errors.js";
|
|
12
|
+
import { LITERT_WASM_PREFIX, installLitertFetchShim, resolveLitertWasmDir, } from "./shim.js";
|
|
13
|
+
const PROVIDER = 'litert';
|
|
14
|
+
/**
|
|
15
|
+
* `loadLiteRt` installs a process-global Wasm module and a default environment. Calling it
|
|
16
|
+
* a second time would rebind that state under the first model, so this guard runs the first
|
|
17
|
+
* load and refuses every later one for the life of the process. A failed load also keeps
|
|
18
|
+
* the guard closed: the module state is unknown, and retrying is not safe.
|
|
19
|
+
*/
|
|
20
|
+
export const createLoadLiteRtGuard = () => {
|
|
21
|
+
let used = false;
|
|
22
|
+
return {
|
|
23
|
+
run: async (load) => {
|
|
24
|
+
if (used) {
|
|
25
|
+
throw new RuntimeUnavailable({
|
|
26
|
+
provider: PROVIDER,
|
|
27
|
+
reason: 'loadLiteRt has already been called in this process; the LiteRT Wasm module keeps process-global state and cannot be loaded twice',
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
used = true;
|
|
31
|
+
return load();
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
/** The guard shared by every real boot in this process. */
|
|
36
|
+
export const processLoadLiteRtGuard = createLoadLiteRtGuard();
|
|
37
|
+
const defaultLoadRuntime = async () => {
|
|
38
|
+
const loaded = await import('@litertjs/core');
|
|
39
|
+
// SAFETY: the real module exposes Tensor, loadLiteRt and loadAndCompile with wider
|
|
40
|
+
// overloads; this narrows the value to the surface used here, with identical runtime values.
|
|
41
|
+
return loaded;
|
|
42
|
+
};
|
|
43
|
+
const wrapCompiledModel = (runtime, compiled) => {
|
|
44
|
+
const inputDetails = compiled.getInputDetails();
|
|
45
|
+
const outputDetails = compiled.getOutputDetails();
|
|
46
|
+
const inputNames = inputDetails.map((detail) => detail.name);
|
|
47
|
+
const outputNames = outputDetails.map((detail) => detail.name);
|
|
48
|
+
return {
|
|
49
|
+
inputNames,
|
|
50
|
+
outputNames,
|
|
51
|
+
run: async (feeds) => {
|
|
52
|
+
const tensors = {};
|
|
53
|
+
try {
|
|
54
|
+
for (const [name, feed] of Object.entries(feeds)) {
|
|
55
|
+
if (!inputNames.includes(name)) {
|
|
56
|
+
throw new Error(`the compiled model has no input named ${name}`);
|
|
57
|
+
}
|
|
58
|
+
tensors[name] = new runtime.Tensor(feed.data, [...feed.dims]);
|
|
59
|
+
}
|
|
60
|
+
const produced = await compiled.run(tensors);
|
|
61
|
+
const outputs = {};
|
|
62
|
+
for (const [name, tensor] of Object.entries(produced)) {
|
|
63
|
+
outputs[name] = tensor.toTypedArray();
|
|
64
|
+
tensor.delete();
|
|
65
|
+
}
|
|
66
|
+
return outputs;
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
for (const tensor of Object.values(tensors))
|
|
70
|
+
tensor.delete();
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
close: () => {
|
|
74
|
+
compiled.delete();
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* The real boot: shim → `loadLiteRt` (once) → `loadAndCompile` (one model) → a Tensor
|
|
80
|
+
* mapping. Every failure is reported as `RuntimeUnavailable`, including the refusal of a
|
|
81
|
+
* second `loadLiteRt` in the same process.
|
|
82
|
+
*/
|
|
83
|
+
export const createNodeLitertBoot = (options = {}) => {
|
|
84
|
+
const guard = options.guard ?? processLoadLiteRtGuard;
|
|
85
|
+
const loadRuntime = options.loadRuntime ?? defaultLoadRuntime;
|
|
86
|
+
const installShim = options.installShim ?? installLitertFetchShim;
|
|
87
|
+
return {
|
|
88
|
+
load: async ({ modelBytes, threads }) => {
|
|
89
|
+
const effectiveThreads = threads ?? options.threads;
|
|
90
|
+
try {
|
|
91
|
+
const wasmDir = path.resolve(options.wasmDir ?? resolveLitertWasmDir());
|
|
92
|
+
installShim({ wasmDir });
|
|
93
|
+
const runtime = await guard.run(async () => {
|
|
94
|
+
const loaded = await loadRuntime();
|
|
95
|
+
await loaded.loadLiteRt(LITERT_WASM_PREFIX, { jspi: false });
|
|
96
|
+
return loaded;
|
|
97
|
+
});
|
|
98
|
+
const compileOptions = effectiveThreads === undefined
|
|
99
|
+
? { accelerator: 'wasm' }
|
|
100
|
+
: { accelerator: 'wasm', cpuOptions: { numThreads: effectiveThreads } };
|
|
101
|
+
const compiled = await runtime.loadAndCompile(modelBytes, compileOptions);
|
|
102
|
+
return wrapCompiledModel(runtime, compiled);
|
|
103
|
+
}
|
|
104
|
+
catch (cause) {
|
|
105
|
+
if (cause instanceof RuntimeUnavailable)
|
|
106
|
+
throw cause;
|
|
107
|
+
throw new RuntimeUnavailable({ provider: PROVIDER, reason: describeCause(cause) });
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Processor moderator/crops-v1: on-device NSFW classification over 384px Pillow-bilinear crops.
|
|
3
|
+
*
|
|
4
|
+
* PRIMARY SOURCE (Desert-Ant-Labs/desert-ant-core @ main, fetched 2026-11-14). Every constant
|
|
5
|
+
* and decision rule below is taken from it; the file:line references are the contract:
|
|
6
|
+
* - Sources/Moderator/Preprocess.swift:9-10 side 384, zooms [0.85, 0.70, 0.55]
|
|
7
|
+
* - Sources/Moderator/Preprocess.swift:26-27 accurate = tiles followed by their mirrors
|
|
8
|
+
* - Sources/Moderator/Preprocess.swift:32-38 fast/balanced windows (letterbox + zoom squares)
|
|
9
|
+
* - Sources/Moderator/Preprocess.swift:54-162 Pillow BILINEAR, bit-exact (see pillow.ts)
|
|
10
|
+
* - Sources/Moderator/Model.swift:24 head order nipples, genitals, buttocks, nude, sexAct
|
|
11
|
+
* - Sources/Moderator/Model.swift:29-30 input "image" [1,384,384,3] float32 raw RGB,
|
|
12
|
+
* output "scores" (normalisation is inside the model)
|
|
13
|
+
* - Sources/Moderator/Model.swift:45-47 per-head max over crops
|
|
14
|
+
* - Sources/Moderator/Moderator.swift:36-55 Quality (fast/balanced/accurate) and Policy
|
|
15
|
+
* (standard/allowTopless)
|
|
16
|
+
* - Sources/Moderator/Moderator.swift:66-71 upstream defaults: threshold 0.5, policy standard,
|
|
17
|
+
* quality accurate; OUR tool contract defaults quality
|
|
18
|
+
* to balanced per docs/runtime/PHASE4.md:29.
|
|
19
|
+
* - Sources/Moderator/Moderator.swift:96-101 score = max over the heads the policy counts
|
|
20
|
+
* - Sources/Moderator/Moderator.swift:113-117 isNSFW = score >= threshold
|
|
21
|
+
* - Sources/Moderator/Moderator.swift:3-31 ImagePixels: 8-bit RGB/RGBA, alpha ignored
|
|
22
|
+
*
|
|
23
|
+
* INPUT (closed): { image_path, quality?, threshold?, policy? }. v1 decodes PNG only, through
|
|
24
|
+
* the host's resolvePath authority; JPEG is refused by name (PHASE4:27-29).
|
|
25
|
+
* OUTPUT (exactly): { isNSFW, score, threshold, policy, quality, regions, crops } where
|
|
26
|
+
* regions = { nipples, genitals, buttocks, nude, sexAct } (per-head max) and crops is the
|
|
27
|
+
* integer number of crops scored: 1 fast, 4 balanced, 8 accurate.
|
|
28
|
+
*
|
|
29
|
+
* HONEST LIMITS: the model is an adult-content classifier. This processor makes no claim
|
|
30
|
+
* about minors or non-adult content, and the scores are decision scores, not calibrated
|
|
31
|
+
* probabilities. Vendor per-class fit gates from shapes/moderator metadata are not applied.
|
|
32
|
+
*/
|
|
33
|
+
import { type ModeratorQuality } from './pillow.ts';
|
|
34
|
+
import { type RecipeProcessor } from './processor.ts';
|
|
35
|
+
/** The processor id this module owns. */
|
|
36
|
+
export declare const MODERATOR_PROCESSOR = "moderator/crops-v1";
|
|
37
|
+
/** The square side the model consumes (Preprocess.swift:9). */
|
|
38
|
+
export declare const MODERATOR_SIDE = 384;
|
|
39
|
+
/** The graph's score head order (Model.swift:24). */
|
|
40
|
+
export declare const MODERATOR_HEADS: readonly ["nipples", "genitals", "buttocks", "nude", "sexAct"];
|
|
41
|
+
/** One region head. */
|
|
42
|
+
export type ModeratorHead = (typeof MODERATOR_HEADS)[number];
|
|
43
|
+
/** The number of heads the graph emits (Model.swift:9). */
|
|
44
|
+
export declare const MODERATOR_SCORE_COUNT: 5;
|
|
45
|
+
/** Which region heads count toward the decision (Moderator.swift:49-55). */
|
|
46
|
+
export type ModeratorPolicy = 'standard' | 'allowTopless';
|
|
47
|
+
/** The supported quality dials (Moderator.swift:36-46). */
|
|
48
|
+
export declare const MODERATOR_QUALITIES: readonly ["fast", "balanced", "accurate"];
|
|
49
|
+
/** The supported policies. */
|
|
50
|
+
export declare const MODERATOR_POLICIES: readonly ["standard", "allowTopless"];
|
|
51
|
+
/** Default decision threshold (Moderator.swift:66). */
|
|
52
|
+
export declare const MODERATOR_DEFAULT_THRESHOLD = 0.5;
|
|
53
|
+
/** Our tool contract's default quality (PHASE4:29; upstream Options defaults to accurate). */
|
|
54
|
+
export declare const MODERATOR_DEFAULT_QUALITY: ModeratorQuality;
|
|
55
|
+
/** Default policy (Moderator.swift:66). */
|
|
56
|
+
export declare const MODERATOR_DEFAULT_POLICY: ModeratorPolicy;
|
|
57
|
+
/** The moderator processor. */
|
|
58
|
+
export declare const moderatorCropsProcessor: RecipeProcessor;
|
|
59
|
+
//# sourceMappingURL=moderator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moderator.d.ts","sourceRoot":"","sources":["../src/moderator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAUH,OAAO,EAIL,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAA;AAEpB,OAAO,EAAyC,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAG5F,yCAAyC;AACzC,eAAO,MAAM,mBAAmB,uBAAuB,CAAA;AAEvD,+DAA+D;AAC/D,eAAO,MAAM,cAAc,MAAY,CAAA;AAEvC,qDAAqD;AACrD,eAAO,MAAM,eAAe,gEAAiE,CAAA;AAE7F,uBAAuB;AACvB,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAA;AAE5D,2DAA2D;AAC3D,eAAO,MAAM,qBAAqB,GAAyB,CAAA;AAE3D,4EAA4E;AAC5E,MAAM,MAAM,eAAe,GAAG,UAAU,GAAG,cAAc,CAAA;AAEzD,2DAA2D;AAC3D,eAAO,MAAM,mBAAmB,2CAA4C,CAAA;AAE5E,8BAA8B;AAC9B,eAAO,MAAM,kBAAkB,uCAAwC,CAAA;AAEvE,uDAAuD;AACvD,eAAO,MAAM,2BAA2B,MAAM,CAAA;AAE9C,8FAA8F;AAC9F,eAAO,MAAM,yBAAyB,EAAE,gBAA6B,CAAA;AAErE,2CAA2C;AAC3C,eAAO,MAAM,wBAAwB,EAAE,eAA4B,CAAA;AAgInE,+BAA+B;AAC/B,eAAO,MAAM,uBAAuB,EAAE,eAsBrC,CAAA"}
|
package/lib/moderator.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Processor moderator/crops-v1: on-device NSFW classification over 384px Pillow-bilinear crops.
|
|
3
|
+
*
|
|
4
|
+
* PRIMARY SOURCE (Desert-Ant-Labs/desert-ant-core @ main, fetched 2026-11-14). Every constant
|
|
5
|
+
* and decision rule below is taken from it; the file:line references are the contract:
|
|
6
|
+
* - Sources/Moderator/Preprocess.swift:9-10 side 384, zooms [0.85, 0.70, 0.55]
|
|
7
|
+
* - Sources/Moderator/Preprocess.swift:26-27 accurate = tiles followed by their mirrors
|
|
8
|
+
* - Sources/Moderator/Preprocess.swift:32-38 fast/balanced windows (letterbox + zoom squares)
|
|
9
|
+
* - Sources/Moderator/Preprocess.swift:54-162 Pillow BILINEAR, bit-exact (see pillow.ts)
|
|
10
|
+
* - Sources/Moderator/Model.swift:24 head order nipples, genitals, buttocks, nude, sexAct
|
|
11
|
+
* - Sources/Moderator/Model.swift:29-30 input "image" [1,384,384,3] float32 raw RGB,
|
|
12
|
+
* output "scores" (normalisation is inside the model)
|
|
13
|
+
* - Sources/Moderator/Model.swift:45-47 per-head max over crops
|
|
14
|
+
* - Sources/Moderator/Moderator.swift:36-55 Quality (fast/balanced/accurate) and Policy
|
|
15
|
+
* (standard/allowTopless)
|
|
16
|
+
* - Sources/Moderator/Moderator.swift:66-71 upstream defaults: threshold 0.5, policy standard,
|
|
17
|
+
* quality accurate; OUR tool contract defaults quality
|
|
18
|
+
* to balanced per docs/runtime/PHASE4.md:29.
|
|
19
|
+
* - Sources/Moderator/Moderator.swift:96-101 score = max over the heads the policy counts
|
|
20
|
+
* - Sources/Moderator/Moderator.swift:113-117 isNSFW = score >= threshold
|
|
21
|
+
* - Sources/Moderator/Moderator.swift:3-31 ImagePixels: 8-bit RGB/RGBA, alpha ignored
|
|
22
|
+
*
|
|
23
|
+
* INPUT (closed): { image_path, quality?, threshold?, policy? }. v1 decodes PNG only, through
|
|
24
|
+
* the host's resolvePath authority; JPEG is refused by name (PHASE4:27-29).
|
|
25
|
+
* OUTPUT (exactly): { isNSFW, score, threshold, policy, quality, regions, crops } where
|
|
26
|
+
* regions = { nipples, genitals, buttocks, nude, sexAct } (per-head max) and crops is the
|
|
27
|
+
* integer number of crops scored: 1 fast, 4 balanced, 8 accurate.
|
|
28
|
+
*
|
|
29
|
+
* HONEST LIMITS: the model is an adult-content classifier. This processor makes no claim
|
|
30
|
+
* about minors or non-adult content, and the scores are decision scores, not calibrated
|
|
31
|
+
* probabilities. Vendor per-class fit gates from shapes/moderator metadata are not applied.
|
|
32
|
+
*/
|
|
33
|
+
import { readFile } from 'node:fs/promises';
|
|
34
|
+
import { Effect, Schema } from 'effect';
|
|
35
|
+
import { InferenceFailed, InvalidInput } from '@try-works/tool-mode-runtime-core';
|
|
36
|
+
import { describeCause } from "./errors.js";
|
|
37
|
+
import { TILE_SIDE, cropTiles, } from "./pillow.js";
|
|
38
|
+
import { decodePngToRgb } from "./png.js";
|
|
39
|
+
import { closedKeyFailure, inputCeilingFailure } from "./processor.js";
|
|
40
|
+
import { LitertSession } from "./runtime.js";
|
|
41
|
+
/** The processor id this module owns. */
|
|
42
|
+
export const MODERATOR_PROCESSOR = 'moderator/crops-v1';
|
|
43
|
+
/** The square side the model consumes (Preprocess.swift:9). */
|
|
44
|
+
export const MODERATOR_SIDE = TILE_SIDE;
|
|
45
|
+
/** The graph's score head order (Model.swift:24). */
|
|
46
|
+
export const MODERATOR_HEADS = ['nipples', 'genitals', 'buttocks', 'nude', 'sexAct'];
|
|
47
|
+
/** The number of heads the graph emits (Model.swift:9). */
|
|
48
|
+
export const MODERATOR_SCORE_COUNT = MODERATOR_HEADS.length;
|
|
49
|
+
/** The supported quality dials (Moderator.swift:36-46). */
|
|
50
|
+
export const MODERATOR_QUALITIES = ['fast', 'balanced', 'accurate'];
|
|
51
|
+
/** The supported policies. */
|
|
52
|
+
export const MODERATOR_POLICIES = ['standard', 'allowTopless'];
|
|
53
|
+
/** Default decision threshold (Moderator.swift:66). */
|
|
54
|
+
export const MODERATOR_DEFAULT_THRESHOLD = 0.5;
|
|
55
|
+
/** Our tool contract's default quality (PHASE4:29; upstream Options defaults to accurate). */
|
|
56
|
+
export const MODERATOR_DEFAULT_QUALITY = 'balanced';
|
|
57
|
+
/** Default policy (Moderator.swift:66). */
|
|
58
|
+
export const MODERATOR_DEFAULT_POLICY = 'standard';
|
|
59
|
+
/** The longest image path this tool accepts. */
|
|
60
|
+
const MAX_PATH_CHARACTERS = 4096;
|
|
61
|
+
const INPUT_KEYS = new Set(['image_path', 'quality', 'threshold', 'policy']);
|
|
62
|
+
const QualitySchema = Schema.Literals(['fast', 'balanced', 'accurate']);
|
|
63
|
+
const PolicySchema = Schema.Literals(['standard', 'allowTopless']);
|
|
64
|
+
const fail = (field, message) => new InvalidInput({ field, message });
|
|
65
|
+
/** Validate the closed input and fill the PHASE4 defaults. */
|
|
66
|
+
function parseInput(input, maxInputBytes) {
|
|
67
|
+
const ceiling = inputCeilingFailure(input, { maxInputBytes, timeoutMs: 0 });
|
|
68
|
+
if (ceiling !== undefined)
|
|
69
|
+
return Effect.fail(ceiling);
|
|
70
|
+
const keys = closedKeyFailure(input, INPUT_KEYS, 'image_path, quality, threshold and policy');
|
|
71
|
+
if (keys !== undefined)
|
|
72
|
+
return Effect.fail(keys);
|
|
73
|
+
const imagePath = input['image_path'];
|
|
74
|
+
if (imagePath === undefined)
|
|
75
|
+
return Effect.fail(fail('image_path', 'image_path is required'));
|
|
76
|
+
if (!Schema.is(Schema.String)(imagePath) || imagePath.length === 0) {
|
|
77
|
+
return Effect.fail(fail('image_path', 'image_path must be a non-empty string'));
|
|
78
|
+
}
|
|
79
|
+
if (imagePath.length > MAX_PATH_CHARACTERS) {
|
|
80
|
+
return Effect.fail(fail('image_path', 'image_path must be at most ' + String(MAX_PATH_CHARACTERS) + ' characters'));
|
|
81
|
+
}
|
|
82
|
+
const qualityValue = input['quality'];
|
|
83
|
+
if (qualityValue !== undefined && !Schema.is(QualitySchema)(qualityValue)) {
|
|
84
|
+
return Effect.fail(fail('quality', 'quality must be one of fast, balanced, accurate'));
|
|
85
|
+
}
|
|
86
|
+
const policyValue = input['policy'];
|
|
87
|
+
if (policyValue !== undefined && !Schema.is(PolicySchema)(policyValue)) {
|
|
88
|
+
return Effect.fail(fail('policy', 'policy must be standard or allowTopless'));
|
|
89
|
+
}
|
|
90
|
+
const thresholdValue = input['threshold'];
|
|
91
|
+
if (thresholdValue !== undefined && !Schema.is(Schema.Finite)(thresholdValue)) {
|
|
92
|
+
return Effect.fail(fail('threshold', 'threshold must be a finite number'));
|
|
93
|
+
}
|
|
94
|
+
if (thresholdValue !== undefined && (thresholdValue < 0 || thresholdValue > 1)) {
|
|
95
|
+
return Effect.fail(fail('threshold', 'threshold must be between 0 and 1'));
|
|
96
|
+
}
|
|
97
|
+
return Effect.succeed({
|
|
98
|
+
imagePath,
|
|
99
|
+
quality: qualityValue ?? MODERATOR_DEFAULT_QUALITY,
|
|
100
|
+
threshold: thresholdValue ?? MODERATOR_DEFAULT_THRESHOLD,
|
|
101
|
+
policy: policyValue ?? MODERATOR_DEFAULT_POLICY,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Resolve the caller's path through the host authority and decode it as PNG.
|
|
106
|
+
*
|
|
107
|
+
* Any failure -- a denied path, an unreadable file, JPEG bytes, an unsupported PNG variant --
|
|
108
|
+
* is an InvalidInput naming image_path, because the caller can fix all of them.
|
|
109
|
+
*/
|
|
110
|
+
function loadImage(path, context) {
|
|
111
|
+
return Effect.tryPromise({
|
|
112
|
+
try: async () => {
|
|
113
|
+
const resolved = context.resolvePath(path);
|
|
114
|
+
const decoded = decodePngToRgb(new Uint8Array(await readFile(resolved)));
|
|
115
|
+
return { width: decoded.width, height: decoded.height, channels: 3, pixels: decoded.rgb };
|
|
116
|
+
},
|
|
117
|
+
catch: (cause) => fail('image_path', 'image_path cannot be used: ' + describeCause(cause)),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/** Crop the image; a preprocessing failure is an invalid image rather than an inference failure. */
|
|
121
|
+
function tilesFor(image, quality) {
|
|
122
|
+
return Effect.try({
|
|
123
|
+
try: () => cropTiles(image, quality),
|
|
124
|
+
catch: (cause) => fail('image_path', 'the image cannot be cropped: ' + describeCause(cause)),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/** Read and validate one score vector (Model.swift:31-33). */
|
|
128
|
+
function scoreVector(raw) {
|
|
129
|
+
return Effect.try({
|
|
130
|
+
try: () => {
|
|
131
|
+
const scores = raw['scores'];
|
|
132
|
+
if (!(scores instanceof Float32Array) || scores.length !== MODERATOR_SCORE_COUNT) {
|
|
133
|
+
throw new Error('the model returned scores with ' + String(scores?.length ?? 'no') + ' values; expected ' + String(MODERATOR_SCORE_COUNT));
|
|
134
|
+
}
|
|
135
|
+
for (const value of scores) {
|
|
136
|
+
if (!Number.isFinite(value))
|
|
137
|
+
throw new Error('the model returned a non-finite score');
|
|
138
|
+
}
|
|
139
|
+
return scores;
|
|
140
|
+
},
|
|
141
|
+
catch: (cause) => new InferenceFailed({ provider: 'litert', phase: 'post', message: describeCause(cause) }),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/** Build exactly the tool schema's seven keys (Moderator.swift:96-117). */
|
|
145
|
+
function composeOutput(options, peaks, crops) {
|
|
146
|
+
const regions = {
|
|
147
|
+
nipples: peaks[0] ?? 0,
|
|
148
|
+
genitals: peaks[1] ?? 0,
|
|
149
|
+
buttocks: peaks[2] ?? 0,
|
|
150
|
+
nude: peaks[3] ?? 0,
|
|
151
|
+
sexAct: peaks[4] ?? 0,
|
|
152
|
+
};
|
|
153
|
+
const score = options.policy === 'standard'
|
|
154
|
+
? Math.max(regions.nipples, regions.genitals, regions.buttocks, regions.nude, regions.sexAct)
|
|
155
|
+
: Math.max(regions.genitals, regions.buttocks, regions.nude, regions.sexAct);
|
|
156
|
+
return {
|
|
157
|
+
isNSFW: score >= options.threshold,
|
|
158
|
+
score,
|
|
159
|
+
threshold: options.threshold,
|
|
160
|
+
policy: options.policy,
|
|
161
|
+
quality: options.quality,
|
|
162
|
+
regions,
|
|
163
|
+
crops,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/** The moderator processor. */
|
|
167
|
+
export const moderatorCropsProcessor = {
|
|
168
|
+
id: MODERATOR_PROCESSOR,
|
|
169
|
+
features: [MODERATOR_PROCESSOR],
|
|
170
|
+
invoke: ({ input, context, limits }) => Effect.gen(function* () {
|
|
171
|
+
const options = yield* parseInput(input, limits.maxInputBytes);
|
|
172
|
+
const image = yield* loadImage(options.imagePath, context);
|
|
173
|
+
const tiles = yield* tilesFor(image, options.quality);
|
|
174
|
+
const session = yield* LitertSession;
|
|
175
|
+
const peaks = [0, 0, 0, 0, 0];
|
|
176
|
+
for (const tile of tiles) {
|
|
177
|
+
const data = Float32Array.from(tile);
|
|
178
|
+
const raw = yield* session.run({
|
|
179
|
+
image: { data, dims: [1, MODERATOR_SIDE, MODERATOR_SIDE, 3] },
|
|
180
|
+
});
|
|
181
|
+
const scores = yield* scoreVector(raw);
|
|
182
|
+
for (let head = 0; head < MODERATOR_SCORE_COUNT; head += 1) {
|
|
183
|
+
peaks[head] = Math.max(peaks[head] ?? 0, scores[head] ?? 0);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return composeOutput(options, peaks, tiles.length);
|
|
187
|
+
}),
|
|
188
|
+
};
|
|
189
|
+
//# sourceMappingURL=moderator.js.map
|
package/lib/pillow.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bit-exact port of The Pillow Group's BILINEAR resampler as used by the moderator
|
|
3
|
+
* preprocessing (libImaging/Resample.c), plus the crop geometry around it.
|
|
4
|
+
*
|
|
5
|
+
* PRIMARY SOURCE (Desert-Ant-Labs/desert-ant-core @ main, fetched 2026-11-14):
|
|
6
|
+
* - Sources/Moderator/Preprocess.swift:9 side = 384
|
|
7
|
+
* - Sources/Moderator/Preprocess.swift:10 zooms = [0.85, 0.70, 0.55]
|
|
8
|
+
* - Sources/Moderator/Preprocess.swift:32-38 windows: fast = center square of the short
|
|
9
|
+
* side; balanced = the whole image letterboxed onto a black square, plus center squares
|
|
10
|
+
* at 85/70/55% of the short side; accurate adds each tile's mirror.
|
|
11
|
+
* - Sources/Moderator/Preprocess.swift:40-50 mirror (horizontal RGB flip)
|
|
12
|
+
* - Sources/Moderator/Preprocess.swift:54 precisionBits = 32 - 8 - 2 = 22
|
|
13
|
+
* - Sources/Moderator/Preprocess.swift:65-94 fixed-point taps, including the exact
|
|
14
|
+
* operation order and the Int32 rounding of each weight.
|
|
15
|
+
* - Sources/Moderator/Preprocess.swift:96-100 clip8: arithmetic shift then clamp 0..255.
|
|
16
|
+
* - Sources/Moderator/Preprocess.swift:104-162 two-pass resample; pixels outside the
|
|
17
|
+
* source window read as black, which is how the letterbox is produced.
|
|
18
|
+
*
|
|
19
|
+
* THE CONTRACT IS BIT-EXACTNESS, so this file preserves the Swift operation order,
|
|
20
|
+
* integer truncation and fixed-point precision rather than "improving" any of it.
|
|
21
|
+
*/
|
|
22
|
+
/** The square side every crop is resampled to (Preprocess.swift:9). */
|
|
23
|
+
export declare const TILE_SIDE = 384;
|
|
24
|
+
/** Pillow's fixed-point precision for BILINEAR (Preprocess.swift:54). */
|
|
25
|
+
export declare const PILLOW_PRECISION_BITS: number;
|
|
26
|
+
/** Balanced-mode zoom factors of the short side (Preprocess.swift:10). */
|
|
27
|
+
export declare const MODERATOR_ZOOMS: readonly [0.85, 0.7, 0.55];
|
|
28
|
+
/** The moderator quality dial (Moderator.swift:36-46). */
|
|
29
|
+
export type ModeratorQuality = 'fast' | 'balanced' | 'accurate';
|
|
30
|
+
/** Decoded 8-bit image; the moderator only reads the first three channels (alpha ignored). */
|
|
31
|
+
export interface ImagePixels {
|
|
32
|
+
readonly width: number;
|
|
33
|
+
readonly height: number;
|
|
34
|
+
readonly channels: 3 | 4;
|
|
35
|
+
readonly pixels: Uint8Array;
|
|
36
|
+
}
|
|
37
|
+
/** One square source window, possibly extending outside the image (the letterbox). */
|
|
38
|
+
export interface CropWindow {
|
|
39
|
+
readonly x0: number;
|
|
40
|
+
readonly y0: number;
|
|
41
|
+
readonly size: number;
|
|
42
|
+
}
|
|
43
|
+
/** Pillow's fixed-point filter taps for one output size. */
|
|
44
|
+
export interface BilinearTaps {
|
|
45
|
+
readonly start: readonly number[];
|
|
46
|
+
readonly count: readonly number[];
|
|
47
|
+
readonly weights: readonly number[];
|
|
48
|
+
readonly ksize: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Precompute the fixed-point tap table (Preprocess.swift:65-94).
|
|
52
|
+
*
|
|
53
|
+
* The arithmetic is deliberately the same as the Swift (and Pillow's C): double-precision
|
|
54
|
+
* weights accumulated in the same order, divided by the total, scaled by 1 << 22 and rounded
|
|
55
|
+
* with +0.5 (or -0.5 for the impossible negative case) before Int32 truncation.
|
|
56
|
+
*/
|
|
57
|
+
export declare const bilinearTaps: (inSize: number, outSize: number) => BilinearTaps;
|
|
58
|
+
/**
|
|
59
|
+
* Resample one square source window to TILE_SIDE x TILE_SIDE RGB.
|
|
60
|
+
*
|
|
61
|
+
* Two separable passes, horizontal into a scratch band of only the rows the vertical pass
|
|
62
|
+
* reads, then vertical, exactly as Preprocess.swift:104-162. Source pixels outside the image
|
|
63
|
+
* are skipped (black); out-of-window rows leave the scratch band zeroed.
|
|
64
|
+
*/
|
|
65
|
+
export declare const resampleWindow: (image: ImagePixels, x0: number, y0: number, size: number) => Uint8Array;
|
|
66
|
+
/** Horizontal mirror of one RGB tile (Preprocess.swift:40-50). */
|
|
67
|
+
export declare const mirrorTile: (tile: Uint8Array, side: number) => Uint8Array;
|
|
68
|
+
/** The square source windows for one quality (Preprocess.swift:32-38). */
|
|
69
|
+
export declare const cropWindows: (width: number, height: number, quality: ModeratorQuality) => readonly CropWindow[];
|
|
70
|
+
/**
|
|
71
|
+
* The RGB tiles one image is scored on: resample every window, and for accurate follow each
|
|
72
|
+
* tile with its mirror (Preprocess.swift:14-28).
|
|
73
|
+
*/
|
|
74
|
+
export declare const cropTiles: (image: ImagePixels, quality: ModeratorQuality) => readonly Uint8Array[];
|
|
75
|
+
//# sourceMappingURL=pillow.d.ts.map
|