@stabrise/scaledp 0.1.0

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.
Files changed (60) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +218 -0
  3. package/dist/box-DAfzwfhA.d.ts +119 -0
  4. package/dist/config-g6IrKlDC.d.ts +80 -0
  5. package/dist/data-to-image-DoZ4jQ3R.js +54 -0
  6. package/dist/data-to-image-DoZ4jQ3R.js.map +1 -0
  7. package/dist/detect/index.d.ts +71 -0
  8. package/dist/detect/index.js +2 -0
  9. package/dist/detect-q8AI_Jdj.js +274 -0
  10. package/dist/detect-q8AI_Jdj.js.map +1 -0
  11. package/dist/detector-output-C0Qt-jEq.d.ts +13 -0
  12. package/dist/detector-output-lyF1Mqb8.js +13 -0
  13. package/dist/detector-output-lyF1Mqb8.js.map +1 -0
  14. package/dist/display/index.d.ts +66 -0
  15. package/dist/display/index.js +237 -0
  16. package/dist/display/index.js.map +1 -0
  17. package/dist/document-B8I61TiY.d.ts +16 -0
  18. package/dist/entity-CedtRhU1.d.ts +22 -0
  19. package/dist/entity-D6Hxaugj.js +13 -0
  20. package/dist/entity-D6Hxaugj.js.map +1 -0
  21. package/dist/image-CAH2rLv9.js +511 -0
  22. package/dist/image-CAH2rLv9.js.map +1 -0
  23. package/dist/image-Dc5TSg46.d.ts +18 -0
  24. package/dist/image-DoZDJkcR.js +37 -0
  25. package/dist/image-DoZDJkcR.js.map +1 -0
  26. package/dist/image-draw-boxes-De0QbFv9.js +285 -0
  27. package/dist/image-draw-boxes-De0QbFv9.js.map +1 -0
  28. package/dist/index.d.ts +269 -0
  29. package/dist/index.js +11 -0
  30. package/dist/model-cache-BEaqqRZ9.js +182 -0
  31. package/dist/model-cache-BEaqqRZ9.js.map +1 -0
  32. package/dist/model-cache-BhFYpfZz.d.ts +36 -0
  33. package/dist/ner/index.d.ts +293 -0
  34. package/dist/ner/index.js +2 -0
  35. package/dist/ner-SsZLZ6ed.js +1028 -0
  36. package/dist/ner-SsZLZ6ed.js.map +1 -0
  37. package/dist/ocr/index.d.ts +440 -0
  38. package/dist/ocr/index.js +3 -0
  39. package/dist/ocr-OHX2WM3e.js +1294 -0
  40. package/dist/ocr-OHX2WM3e.js.map +1 -0
  41. package/dist/ort-CXDoPrtw.js +73 -0
  42. package/dist/ort-CXDoPrtw.js.map +1 -0
  43. package/dist/params-DapwK9Ns.js +37 -0
  44. package/dist/params-DapwK9Ns.js.map +1 -0
  45. package/dist/pdf/index.d.ts +123 -0
  46. package/dist/pdf/index.js +2 -0
  47. package/dist/pdf-BQl0dneD.js +417 -0
  48. package/dist/pdf-BQl0dneD.js.map +1 -0
  49. package/dist/pipeline-DACqGkpN.js +240 -0
  50. package/dist/pipeline-DACqGkpN.js.map +1 -0
  51. package/dist/pipeline-DeLO-OCE.d.ts +139 -0
  52. package/dist/registry/index.d.ts +169 -0
  53. package/dist/registry/index.js +1061 -0
  54. package/dist/registry/index.js.map +1 -0
  55. package/dist/text-ahMLpxN9.js +109 -0
  56. package/dist/text-ahMLpxN9.js.map +1 -0
  57. package/dist/worker/index.d.ts +105 -0
  58. package/dist/worker/index.js +180 -0
  59. package/dist/worker/index.js.map +1 -0
  60. package/package.json +135 -0
@@ -0,0 +1,293 @@
1
+ import { l as Stage, m as BaseStageParams, s as Row } from "../pipeline-DeLO-OCE.js";
2
+ import { n as Box } from "../box-DAfzwfhA.js";
3
+ import { t as Document } from "../document-B8I61TiY.js";
4
+ import { n as NerOutput, t as Entity } from "../entity-CedtRhU1.js";
5
+ import { n as ModelFiles, t as ModelFile } from "../model-cache-BhFYpfZz.js";
6
+ //#region src/ner/vendor/splitter.d.ts
7
+ /** Word splitting for GLiNER. Adapted from @lmoe/gliner-onnx (MIT). */
8
+ /**
9
+ * Unicode word classes (`\p{L}\p{N}` with the `u` flag), deliberately not `\w`.
10
+ *
11
+ * JavaScript's `\w` is ASCII-only even under `/u`, so it would shatter accented
12
+ * names -- "Müller", "García" -- into single-character tokens and wreck
13
+ * multi-word span detection in German, Polish and Spanish text. Python's `\w`
14
+ * is Unicode-aware, so this restores parity with the reference tokenizer.
15
+ */
16
+ declare const WORD_PATTERN: RegExp;
17
+ /**
18
+ * As above, plus leading branches for URLs, emails and @mentions so they stay
19
+ * whole. Those branches are ASCII on purpose, mirroring the Python pattern.
20
+ */
21
+ declare const RICH_WORD_PATTERN: RegExp;
22
+ type SplitWord = [text: string, start: number, end: number];
23
+ /**
24
+ * Split text into words with their character offsets.
25
+ *
26
+ * A fresh RegExp per call: the `g` flag makes `lastIndex` stateful, so sharing
27
+ * one instance across calls silently skips matches.
28
+ */
29
+ declare function splitWords(text: string, pattern?: RegExp): SplitWord[];
30
+ //#endregion
31
+ //#region src/ner/vendor/span-decoder.d.ts
32
+ interface DecodedSpan {
33
+ text: string;
34
+ label: string;
35
+ start: number;
36
+ end: number;
37
+ score: number;
38
+ }
39
+ interface DecodeOptions {
40
+ threshold?: number;
41
+ /** Flat NER forbids nesting; set false to allow a span inside another. */
42
+ flatNer?: boolean;
43
+ /** Allow the same span to carry more than one label. */
44
+ multiLabel?: boolean;
45
+ }
46
+ declare function decodeSpans(logits: ArrayLike<number>, params: {
47
+ batchSize: number;
48
+ /** Words per sequence, i.e. the model's span-start axis. */
49
+ inputLength: number;
50
+ maxWidth: number;
51
+ entityCount: number;
52
+ texts: readonly string[];
53
+ batchWords: readonly SplitWord[][];
54
+ idToClass: Record<number, string>;
55
+ }, options?: DecodeOptions): DecodedSpan[][];
56
+ //#endregion
57
+ //#region src/ner/backend.d.ts
58
+ interface NerBackendLoadOptions {
59
+ /** A `@huggingface/transformers` tokenizer instance. */
60
+ tokenizer: unknown;
61
+ executionProviders?: readonly string[];
62
+ }
63
+ interface NerBackend {
64
+ readonly arch: 'gliner1' | 'gliner2';
65
+ load(files: ModelFiles, options: NerBackendLoadOptions): Promise<void>;
66
+ /** Entities with character offsets into `text`. */
67
+ extract(text: string, labels: readonly string[], threshold: number): Promise<DecodedSpan[]>;
68
+ dispose(): Promise<void>;
69
+ }
70
+ //#endregion
71
+ //#region src/ner/chunking.d.ts
72
+ /** Python's `split_text` default, and what the cloud /ner/text endpoint uses. */
73
+ declare const DEFAULT_CHUNK_LENGTH = 500;
74
+ /** 500 - 480 leaves a 20-character overlap so entities on a seam survive. */
75
+ declare const DEFAULT_CHUNK_STRIDE = 480;
76
+ interface Chunk {
77
+ text: string;
78
+ /** Character offset of this chunk within the original text. */
79
+ offset: number;
80
+ }
81
+ declare function chunkText(text: string, maxLength?: number, stride?: number): Chunk[];
82
+ /** Shift a chunk-local span onto the original text's coordinates. */
83
+ declare function rebaseSpan(span: DecodedSpan, offset: number): DecodedSpan;
84
+ /**
85
+ * Drop duplicates produced by the chunk overlap, keeping the highest score for
86
+ * each distinct (start, end, label).
87
+ */
88
+ declare function dedupeSpans(spans: readonly DecodedSpan[]): DecodedSpan[];
89
+ declare function isMostlyUppercase(text: string): boolean;
90
+ /**
91
+ * Title-case runs of capitals, preserving length.
92
+ *
93
+ * GLiNER1 models are cased and scanned documents are frequently set in all
94
+ * caps, which reads to the model as unlike anything in training. Length
95
+ * preservation is essential: every character offset the decoder returns is used
96
+ * to index the original text.
97
+ */
98
+ declare function titleCaseAllCapsWords(text: string): string;
99
+ /** Apply the casing fix only when the text is predominantly uppercase. */
100
+ declare function normaliseCasing(text: string): string;
101
+ //#endregion
102
+ //#region src/ner/gliner-ner.d.ts
103
+ interface GlinerNerParams extends BaseStageParams {
104
+ /** Registry id, e.g. 'gliner-multi-pii'. */
105
+ model: string;
106
+ /** Entity types to look for. GLiNER scores a label by its prompt text. */
107
+ labels: readonly string[];
108
+ /** Minimum score an entity must reach (0-1). */
109
+ threshold: number;
110
+ /** Only keep these entity groups; empty keeps everything. */
111
+ whiteList: readonly string[];
112
+ chunkLength: number;
113
+ chunkStride: number;
114
+ /**
115
+ * Title-case runs of capitals before inference. GLiNER1 models are cased
116
+ * and scanned documents are often set in all caps.
117
+ */
118
+ normaliseCasing: boolean;
119
+ }
120
+ declare const GLINER_NER_DEFAULTS: GlinerNerParams;
121
+ /**
122
+ * Map each character of the joined document text to the box it came from.
123
+ *
124
+ * Built from the *actual* text the OCR stage produced rather than assuming one
125
+ * separator per box. Python derives the mapping from `len(box.text) + 1`, which
126
+ * silently drifts whenever `keepFormatting` inserted several spaces or a
127
+ * newline, shifting every entity's boxes after the first wide gap.
128
+ */
129
+ declare function buildCharToBoxMap(text: string, boxes: readonly Box[]): Int32Array;
130
+ /** Boxes a character range touches, in document order and without repeats. */
131
+ declare function boxesForRange(mapping: Int32Array, boxes: readonly Box[], start: number, end: number): Box[];
132
+ declare class GlinerNer extends Stage<GlinerNerParams> {
133
+ readonly name = "GlinerNer";
134
+ private backend;
135
+ private loading;
136
+ constructor(options?: Partial<GlinerNerParams>);
137
+ init(): Promise<void>;
138
+ private getBackend;
139
+ protected apply(input: unknown, row: Row): Promise<NerOutput>;
140
+ /** Run NER over a document and attach boxes to every entity found. */
141
+ extract(document: Document): Promise<Entity[]>;
142
+ protected onError(message: string, row: Row): NerOutput;
143
+ dispose(): Promise<void>;
144
+ }
145
+ //#endregion
146
+ //#region src/ner/gliner1-backend.d.ts
147
+ declare class Gliner1Backend implements NerBackend {
148
+ readonly arch: "gliner1";
149
+ private session;
150
+ private processor;
151
+ private config;
152
+ load(files: ModelFiles, options: NerBackendLoadOptions): Promise<void>;
153
+ extract(text: string, labels: readonly string[], threshold: number): Promise<DecodedSpan[]>;
154
+ dispose(): Promise<void>;
155
+ }
156
+ //#endregion
157
+ //#region src/ner/gliner2-backend.d.ts
158
+ declare class Gliner2Backend implements NerBackend {
159
+ readonly arch: "gliner2";
160
+ private config;
161
+ private encoder;
162
+ private spanRep;
163
+ private countEmbed;
164
+ private tokenize;
165
+ load(files: ModelFiles, options: NerBackendLoadOptions): Promise<void>;
166
+ extract(text: string, labels: readonly string[], threshold: number): Promise<DecodedSpan[]>;
167
+ /** `( [P] entities ( [E] label1 [E] label2 ) ) [SEP_TEXT]` */
168
+ private buildSchema;
169
+ /**
170
+ * Word-split and tokenize the text.
171
+ *
172
+ * Lower-cased before splitting, matching the reference implementation --
173
+ * offsets stay valid because `toLowerCase` is length-preserving for the
174
+ * scripts these models cover.
175
+ */
176
+ private tokenizeWords;
177
+ private encode;
178
+ private runSpanRep;
179
+ private runCountEmbed;
180
+ dispose(): Promise<void>;
181
+ }
182
+ //#endregion
183
+ //#region src/ner/registry.d.ts
184
+ type NerArchitecture = 'gliner1' | 'gliner2';
185
+ interface NerModel {
186
+ /** Short id callers pass to GlinerNer. */
187
+ id: string;
188
+ /** Human-readable name including the download size. */
189
+ name: string;
190
+ arch: NerArchitecture;
191
+ /** Hugging Face repo id. */
192
+ repo: string;
193
+ files: ModelFile[];
194
+ /** Labels this model was tuned for. Using others still works, less well. */
195
+ labels: readonly string[];
196
+ languages: readonly string[];
197
+ /** Private repos need `configure({ auth })` to supply a bearer token. */
198
+ private?: boolean;
199
+ /** Execution providers this model requires; overrides the global config. */
200
+ executionProviders?: readonly string[];
201
+ }
202
+ /** Generic PII labels, matching the pdftools prototype's default set. */
203
+ declare const DEFAULT_PII_LABELS: readonly string[];
204
+ /**
205
+ * Label prompts the StabRise GLiNER2 PII model was fine-tuned on.
206
+ *
207
+ * These must match the cloud endpoint's tag list (scaledp-api
208
+ * deidentify/views.py) or scores drop: GLiNER scores a label by its prompt
209
+ * text, so a renamed label is a different label.
210
+ */
211
+ declare const GLINER2_PII_LABELS: readonly string[];
212
+ declare const NER_MODELS: readonly NerModel[];
213
+ /** Public, zero-configuration default. */
214
+ declare const DEFAULT_NER_MODEL_ID = "gliner-multi-pii";
215
+ declare function getNerModel(id: string): NerModel | undefined;
216
+ /** Total download size in bytes, for a progress estimate before fetching. */
217
+ declare function modelSizeBytes(model: NerModel): number;
218
+ //#endregion
219
+ //#region src/ner/tokenizer.d.ts
220
+ /**
221
+ * Tokenizer loading and adaptation for the GLiNER runtimes.
222
+ *
223
+ * `@huggingface/transformers` provides the tokenizer only; no model runs
224
+ * through it. Its remote host is configurable so gated repos can be proxied
225
+ * through the consuming application's own origin.
226
+ */
227
+ type Transformers = typeof import('@huggingface/transformers');
228
+ type PretrainedTokenizer = Awaited<ReturnType<Transformers['AutoTokenizer']['from_pretrained']>>;
229
+ /** Reset the cached module. Tests only. */
230
+ declare function resetTransformers(): void;
231
+ declare function loadTokenizer(repo: string): Promise<PretrainedTokenizer>;
232
+ /** Adapt to the callable form the GLiNER2 runtime expects. */
233
+ declare function toCallableTokenizer(tokenizer: PretrainedTokenizer): (text: string) => number[];
234
+ //#endregion
235
+ //#region src/ner/vendor/span-processor.d.ts
236
+ /** Minimal tokenizer surface the processor needs. */
237
+ interface SpanTokenizer {
238
+ encode(text: string): number[];
239
+ clsTokenId: number;
240
+ sepTokenId: number;
241
+ }
242
+ //#endregion
243
+ //#region src/ner/tokenizer-types.d.ts
244
+ type PretrainedTokenizerLike = (text: string, options?: {
245
+ add_special_tokens?: boolean;
246
+ }) => {
247
+ input_ids: {
248
+ tolist(): (bigint | number)[][] | (bigint | number)[];
249
+ };
250
+ };
251
+ /**
252
+ * Adapt a transformers.js tokenizer to the span processor's interface.
253
+ *
254
+ * CLS/SEP ids are derived empirically -- encode a throwaway token, read the
255
+ * first and last id -- rather than read from `cls_token_id`. Not every GLiNER
256
+ * repo populates those fields, and a wrong id corrupts every sequence silently
257
+ * instead of failing loudly.
258
+ */
259
+ declare function toSpanTokenizer(tokenizer: PretrainedTokenizerLike): SpanTokenizer;
260
+ //#endregion
261
+ //#region src/ner/vendor/gliner2-decoder.d.ts
262
+ type WordOffset = [start: number, end: number];
263
+ /**
264
+ * Every span up to `maxWidth` words.
265
+ *
266
+ * Out-of-range slots are filled with (0, 0) rather than dropped, because the
267
+ * span axis must stay a fixed `seqLen * maxWidth` for the ONNX graph. The
268
+ * decoder skips them by re-checking the word bounds.
269
+ */
270
+ declare function generateSpans(seqLen: number, maxWidth: number): {
271
+ spanStart: number[];
272
+ spanEnd: number[];
273
+ spanCount: number;
274
+ };
275
+ /** Sigmoid of the dot product between each span and each label embedding. */
276
+ declare function computeDotProductScores(spanRep: Float32Array, labelRep: Float32Array, spanCount: number, labelCount: number, hiddenSize: number): Float32Array;
277
+ interface NerScoreData {
278
+ scores: Float32Array;
279
+ wordSpanStart: number[];
280
+ wordSpanEnd: number[];
281
+ spanCount: number;
282
+ }
283
+ /** Score matrix -> entities, then drop same-label overlaps keeping the best. */
284
+ declare function decodeEntities(scoreData: NerScoreData, wordCount: number, labels: readonly string[], wordOffsets: readonly WordOffset[], text: string, threshold: number): DecodedSpan[];
285
+ //#endregion
286
+ //#region src/ner/vendor/math.d.ts
287
+ /** Numeric helpers for the GLiNER runtimes. Adapted from @lmoe/gliner-onnx (MIT). */
288
+ /** Numerically stable sigmoid: exp(-x) overflows for large negative x. */
289
+ declare function sigmoid(x: number): number;
290
+ declare function softmax(values: ArrayLike<number>): Float32Array;
291
+ //#endregion
292
+ export { type Chunk, DEFAULT_CHUNK_LENGTH, DEFAULT_CHUNK_STRIDE, DEFAULT_NER_MODEL_ID, DEFAULT_PII_LABELS, type DecodedSpan, GLINER2_PII_LABELS, GLINER_NER_DEFAULTS, Gliner1Backend, Gliner2Backend, GlinerNer, type GlinerNerParams, NER_MODELS, type NerArchitecture, type NerBackend, type NerBackendLoadOptions, type NerModel, RICH_WORD_PATTERN, type SplitWord, WORD_PATTERN, boxesForRange, buildCharToBoxMap, chunkText, computeDotProductScores, decodeEntities, decodeSpans, dedupeSpans, generateSpans, getNerModel, isMostlyUppercase, loadTokenizer, modelSizeBytes, normaliseCasing, rebaseSpan, resetTransformers, sigmoid, softmax, splitWords, titleCaseAllCapsWords, toCallableTokenizer, toSpanTokenizer };
293
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,2 @@
1
+ import { A as isMostlyUppercase, C as toSpanTokenizer, D as DEFAULT_CHUNK_STRIDE, E as DEFAULT_CHUNK_LENGTH, M as rebaseSpan, N as titleCaseAllCapsWords, O as chunkText, S as decodeSpans, T as softmax, _ as generateSpans, a as loadTokenizer, b as WORD_PATTERN, c as DEFAULT_NER_MODEL_ID, d as NER_MODELS, f as getNerModel, g as decodeEntities, h as computeDotProductScores, i as buildCharToBoxMap, j as normaliseCasing, k as dedupeSpans, l as DEFAULT_PII_LABELS, m as Gliner2Backend, n as GlinerNer, o as resetTransformers, p as modelSizeBytes, r as boxesForRange, s as toCallableTokenizer, t as GLINER_NER_DEFAULTS, u as GLINER2_PII_LABELS, v as Gliner1Backend, w as sigmoid, x as splitWords, y as RICH_WORD_PATTERN } from "../ner-SsZLZ6ed.js";
2
+ export { DEFAULT_CHUNK_LENGTH, DEFAULT_CHUNK_STRIDE, DEFAULT_NER_MODEL_ID, DEFAULT_PII_LABELS, GLINER2_PII_LABELS, GLINER_NER_DEFAULTS, Gliner1Backend, Gliner2Backend, GlinerNer, NER_MODELS, RICH_WORD_PATTERN, WORD_PATTERN, boxesForRange, buildCharToBoxMap, chunkText, computeDotProductScores, decodeEntities, decodeSpans, dedupeSpans, generateSpans, getNerModel, isMostlyUppercase, loadTokenizer, modelSizeBytes, normaliseCasing, rebaseSpan, resetTransformers, sigmoid, softmax, splitWords, titleCaseAllCapsWords, toCallableTokenizer, toSpanTokenizer };