@localmode/transformers 1.0.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.
- package/README.md +379 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +992 -0
- package/dist/index.d.ts +992 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +79 -0
package/README.md
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# @localmode/transformers
|
|
2
|
+
|
|
3
|
+
HuggingFace Transformers.js provider for LocalMode AI Engine - run ML models locally in the browser.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@localmode/transformers)
|
|
6
|
+
[](../../LICENSE)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- 🚀 **Browser-Native** - Run ML models directly in the browser with WebGPU/WASM
|
|
11
|
+
- 🔒 **Privacy-First** - All processing happens locally, no data leaves the device
|
|
12
|
+
- 📦 **Model Caching** - Models are cached in IndexedDB for instant subsequent loads
|
|
13
|
+
- ⚡ **Optimized** - Uses quantized models for smaller size and faster inference
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Preferred: pnpm
|
|
19
|
+
pnpm install @localmode/transformers @localmode/core @xenova/transformers
|
|
20
|
+
|
|
21
|
+
# Alternative: npm
|
|
22
|
+
npm install @localmode/transformers @localmode/core @xenova/transformers
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { transformers } from '@localmode/transformers';
|
|
29
|
+
import {
|
|
30
|
+
classify,
|
|
31
|
+
extractEntities,
|
|
32
|
+
transcribe,
|
|
33
|
+
classifyImage,
|
|
34
|
+
captionImage,
|
|
35
|
+
} from '@localmode/core';
|
|
36
|
+
|
|
37
|
+
// Text Classification
|
|
38
|
+
const sentiment = await classify({
|
|
39
|
+
model: transformers.classifier('Xenova/distilbert-base-uncased-finetuned-sst-2-english'),
|
|
40
|
+
text: 'I love this product!',
|
|
41
|
+
});
|
|
42
|
+
console.log(sentiment.label); // 'POSITIVE'
|
|
43
|
+
|
|
44
|
+
// Named Entity Recognition
|
|
45
|
+
const entities = await extractEntities({
|
|
46
|
+
model: transformers.ner('Xenova/bert-base-NER'),
|
|
47
|
+
text: 'John works at Microsoft in Seattle',
|
|
48
|
+
});
|
|
49
|
+
console.log(entities.entities);
|
|
50
|
+
// [{ entity: 'John', type: 'PER', ... }, { entity: 'Microsoft', type: 'ORG', ... }, ...]
|
|
51
|
+
|
|
52
|
+
// Speech-to-Text
|
|
53
|
+
const transcription = await transcribe({
|
|
54
|
+
model: transformers.speechToText('Xenova/whisper-tiny'),
|
|
55
|
+
audio: audioBlob,
|
|
56
|
+
returnTimestamps: true,
|
|
57
|
+
});
|
|
58
|
+
console.log(transcription.text);
|
|
59
|
+
|
|
60
|
+
// Image Classification
|
|
61
|
+
const classification = await classifyImage({
|
|
62
|
+
model: transformers.imageClassifier('Xenova/vit-base-patch16-224'),
|
|
63
|
+
image: imageBlob,
|
|
64
|
+
});
|
|
65
|
+
console.log(classification.predictions);
|
|
66
|
+
|
|
67
|
+
// Image Captioning
|
|
68
|
+
const caption = await captionImage({
|
|
69
|
+
model: transformers.captioner('Xenova/blip-image-captioning-base'),
|
|
70
|
+
image: imageBlob,
|
|
71
|
+
});
|
|
72
|
+
console.log(caption.caption);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Available Model Types
|
|
76
|
+
|
|
77
|
+
### Text/NLP Models (P1)
|
|
78
|
+
|
|
79
|
+
| Method | Interface | Description |
|
|
80
|
+
| ------------------------------------------ | ----------------------------- | ----------------------------- |
|
|
81
|
+
| `transformers.classifier(modelId)` | `ClassificationModel` | Text classification |
|
|
82
|
+
| `transformers.zeroShotClassifier(modelId)` | `ZeroShotClassificationModel` | Zero-shot text classification |
|
|
83
|
+
| `transformers.ner(modelId)` | `NERModel` | Named Entity Recognition |
|
|
84
|
+
| `transformers.reranker(modelId)` | `RerankerModel` | Document reranking |
|
|
85
|
+
| `transformers.embedding(modelId)` | `EmbeddingModel` | Text embeddings |
|
|
86
|
+
|
|
87
|
+
### Text/NLP Models (P2)
|
|
88
|
+
|
|
89
|
+
| Method | Interface | Description |
|
|
90
|
+
| ----------------------------------------- | ------------------------ | ----------------------- |
|
|
91
|
+
| `transformers.translator(modelId)` | `TranslationModel` | Text translation |
|
|
92
|
+
| `transformers.summarizer(modelId)` | `SummarizationModel` | Text summarization |
|
|
93
|
+
| `transformers.fillMask(modelId)` | `FillMaskModel` | Masked token prediction |
|
|
94
|
+
| `transformers.questionAnswering(modelId)` | `QuestionAnsweringModel` | Extractive QA |
|
|
95
|
+
|
|
96
|
+
### Vision Models (P1)
|
|
97
|
+
|
|
98
|
+
| Method | Interface | Description |
|
|
99
|
+
| ----------------------------------------------- | ---------------------------------- | ------------------------------ |
|
|
100
|
+
| `transformers.imageClassifier(modelId)` | `ImageClassificationModel` | Image classification |
|
|
101
|
+
| `transformers.zeroShotImageClassifier(modelId)` | `ZeroShotImageClassificationModel` | Zero-shot image classification |
|
|
102
|
+
| `transformers.captioner(modelId)` | `ImageCaptionModel` | Image captioning |
|
|
103
|
+
|
|
104
|
+
### Vision Models (P2)
|
|
105
|
+
|
|
106
|
+
| Method | Interface | Description |
|
|
107
|
+
| -------------------------------------- | ---------------------- | --------------------------------------- |
|
|
108
|
+
| `transformers.segmenter(modelId)` | `SegmentationModel` | Image segmentation |
|
|
109
|
+
| `transformers.objectDetector(modelId)` | `ObjectDetectionModel` | Object detection |
|
|
110
|
+
| `transformers.imageFeatures(modelId)` | `ImageFeatureModel` | Image feature extraction |
|
|
111
|
+
| `transformers.imageToImage(modelId)` | `ImageToImageModel` | Image transformation / super resolution |
|
|
112
|
+
| `transformers.ocr(modelId)` | `OCRModel` | OCR (TrOCR) |
|
|
113
|
+
| `transformers.documentQA(modelId)` | `DocumentQAModel` | Document/Table question answering |
|
|
114
|
+
|
|
115
|
+
### Audio Models (P1 & P2)
|
|
116
|
+
|
|
117
|
+
| Method | Interface | Description |
|
|
118
|
+
| ------------------------------------ | ------------------- | ----------------------------- |
|
|
119
|
+
| `transformers.speechToText(modelId)` | `SpeechToTextModel` | Speech-to-text transcription |
|
|
120
|
+
| `transformers.textToSpeech(modelId)` | `TextToSpeechModel` | Text-to-speech synthesis (P2) |
|
|
121
|
+
|
|
122
|
+
## Recommended Models
|
|
123
|
+
|
|
124
|
+
### Text Classification
|
|
125
|
+
|
|
126
|
+
- `Xenova/distilbert-base-uncased-finetuned-sst-2-english` - Sentiment analysis
|
|
127
|
+
- `Xenova/twitter-roberta-base-sentiment-latest` - Twitter sentiment
|
|
128
|
+
|
|
129
|
+
### Named Entity Recognition
|
|
130
|
+
|
|
131
|
+
- `Xenova/bert-base-NER` - Standard NER (PER, ORG, LOC, MISC)
|
|
132
|
+
|
|
133
|
+
### Reranking
|
|
134
|
+
|
|
135
|
+
- `Xenova/ms-marco-MiniLM-L-6-v2` - Document reranking for RAG
|
|
136
|
+
|
|
137
|
+
### Translation (P2)
|
|
138
|
+
|
|
139
|
+
- `Xenova/opus-mt-en-de` - English to German
|
|
140
|
+
- `Xenova/opus-mt-en-fr` - English to French
|
|
141
|
+
- `Xenova/nllb-200-distilled-600M` - 200 languages
|
|
142
|
+
|
|
143
|
+
### Summarization (P2)
|
|
144
|
+
|
|
145
|
+
- `Xenova/bart-large-cnn` - News summarization
|
|
146
|
+
- `Xenova/distilbart-cnn-12-6` - Fast summarization
|
|
147
|
+
|
|
148
|
+
### Fill-Mask (P2)
|
|
149
|
+
|
|
150
|
+
- `Xenova/bert-base-uncased` - General purpose
|
|
151
|
+
- `Xenova/roberta-base` - Better for some tasks
|
|
152
|
+
|
|
153
|
+
### Question Answering (P2)
|
|
154
|
+
|
|
155
|
+
- `Xenova/distilbert-base-cased-distilled-squad` - SQuAD trained
|
|
156
|
+
- `Xenova/roberta-base-squad2` - SQuAD 2.0 trained
|
|
157
|
+
|
|
158
|
+
### Speech-to-Text
|
|
159
|
+
|
|
160
|
+
- `Xenova/whisper-tiny` - Fast, smaller size (~70MB)
|
|
161
|
+
- `Xenova/whisper-small` - Better accuracy (~240MB)
|
|
162
|
+
|
|
163
|
+
### Text-to-Speech (P2)
|
|
164
|
+
|
|
165
|
+
- `Xenova/speecht5_tts` - Natural speech synthesis
|
|
166
|
+
|
|
167
|
+
### Image Classification
|
|
168
|
+
|
|
169
|
+
- `Xenova/vit-base-patch16-224` - General image classification
|
|
170
|
+
- `Xenova/clip-vit-base-patch32` - Zero-shot image classification
|
|
171
|
+
|
|
172
|
+
### Image Captioning
|
|
173
|
+
|
|
174
|
+
- `Xenova/blip-image-captioning-base` - High-quality captions
|
|
175
|
+
|
|
176
|
+
### Image Segmentation (P2)
|
|
177
|
+
|
|
178
|
+
- `Xenova/segformer-b0-finetuned-ade-512-512` - Fast segmentation
|
|
179
|
+
- `Xenova/detr-resnet-50-panoptic` - Panoptic segmentation
|
|
180
|
+
|
|
181
|
+
### Object Detection (P2)
|
|
182
|
+
|
|
183
|
+
- `Xenova/detr-resnet-50` - COCO objects
|
|
184
|
+
- `Xenova/yolos-tiny` - Fast detection
|
|
185
|
+
|
|
186
|
+
### Image Features (P2)
|
|
187
|
+
|
|
188
|
+
- `Xenova/clip-vit-base-patch32` - Image embeddings
|
|
189
|
+
- `Xenova/dinov2-small` - Self-supervised features
|
|
190
|
+
|
|
191
|
+
### OCR (P2)
|
|
192
|
+
|
|
193
|
+
- `Xenova/trocr-base-handwritten` - Handwritten text
|
|
194
|
+
- `Xenova/trocr-base-printed` - Printed text
|
|
195
|
+
|
|
196
|
+
### Document QA (P2)
|
|
197
|
+
|
|
198
|
+
- `Xenova/donut-base-finetuned-docvqa` - Document QA
|
|
199
|
+
- `Xenova/tapas-base-finetuned-wtq` - Table QA
|
|
200
|
+
|
|
201
|
+
### Embeddings
|
|
202
|
+
|
|
203
|
+
- `Xenova/all-MiniLM-L6-v2` - Fast, general-purpose (~22MB)
|
|
204
|
+
- `Xenova/paraphrase-multilingual-MiniLM-L12-v2` - 50+ languages
|
|
205
|
+
|
|
206
|
+
## P2 Feature Examples
|
|
207
|
+
|
|
208
|
+
### Translation
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { translate } from '@localmode/core';
|
|
212
|
+
import { transformers } from '@localmode/transformers';
|
|
213
|
+
|
|
214
|
+
const { translatedText } = await translate({
|
|
215
|
+
model: transformers.translator('Xenova/opus-mt-en-de'),
|
|
216
|
+
text: 'Hello world!',
|
|
217
|
+
targetLanguage: 'de',
|
|
218
|
+
});
|
|
219
|
+
console.log(translatedText); // "Hallo Welt!"
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Summarization
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
import { summarize } from '@localmode/core';
|
|
226
|
+
import { transformers } from '@localmode/transformers';
|
|
227
|
+
|
|
228
|
+
const { summary } = await summarize({
|
|
229
|
+
model: transformers.summarizer('Xenova/bart-large-cnn'),
|
|
230
|
+
text: longArticle,
|
|
231
|
+
maxLength: 100,
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Image Segmentation
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
import { segmentImage } from '@localmode/core';
|
|
239
|
+
import { transformers } from '@localmode/transformers';
|
|
240
|
+
|
|
241
|
+
const { masks } = await segmentImage({
|
|
242
|
+
model: transformers.segmenter('Xenova/segformer-b0-finetuned-ade-512-512'),
|
|
243
|
+
image: imageBlob,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
for (const mask of masks) {
|
|
247
|
+
console.log(mask.label, mask.score);
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Object Detection
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
import { detectObjects } from '@localmode/core';
|
|
255
|
+
import { transformers } from '@localmode/transformers';
|
|
256
|
+
|
|
257
|
+
const { objects } = await detectObjects({
|
|
258
|
+
model: transformers.objectDetector('Xenova/detr-resnet-50'),
|
|
259
|
+
image: imageBlob,
|
|
260
|
+
threshold: 0.5,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
for (const obj of objects) {
|
|
264
|
+
console.log(`${obj.label}: ${obj.box.x},${obj.box.y}`);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Text-to-Speech
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
import { synthesizeSpeech } from '@localmode/core';
|
|
272
|
+
import { transformers } from '@localmode/transformers';
|
|
273
|
+
|
|
274
|
+
const { audio, sampleRate } = await synthesizeSpeech({
|
|
275
|
+
model: transformers.textToSpeech('Xenova/speecht5_tts'),
|
|
276
|
+
text: 'Hello, how are you?',
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// Play with Web Audio API
|
|
280
|
+
const ctx = new AudioContext();
|
|
281
|
+
const buffer = ctx.createBuffer(1, audio.length, sampleRate);
|
|
282
|
+
buffer.getChannelData(0).set(audio);
|
|
283
|
+
const source = ctx.createBufferSource();
|
|
284
|
+
source.buffer = buffer;
|
|
285
|
+
source.connect(ctx.destination);
|
|
286
|
+
source.start();
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### OCR
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
import { extractText } from '@localmode/core';
|
|
293
|
+
import { transformers } from '@localmode/transformers';
|
|
294
|
+
|
|
295
|
+
const { text, regions } = await extractText({
|
|
296
|
+
model: transformers.ocr('Xenova/trocr-base-printed'),
|
|
297
|
+
image: documentImage,
|
|
298
|
+
});
|
|
299
|
+
console.log(text);
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Question Answering
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
import { answerQuestion } from '@localmode/core';
|
|
306
|
+
import { transformers } from '@localmode/transformers';
|
|
307
|
+
|
|
308
|
+
const { answers } = await answerQuestion({
|
|
309
|
+
model: transformers.questionAnswering('Xenova/distilbert-base-cased-distilled-squad'),
|
|
310
|
+
question: 'What is the capital of France?',
|
|
311
|
+
context: 'Paris is the capital and largest city of France.',
|
|
312
|
+
});
|
|
313
|
+
console.log(answers[0].answer); // "Paris"
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Advanced Usage
|
|
317
|
+
|
|
318
|
+
### Custom Model Options
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
const model = transformers.classifier('Xenova/distilbert-base-uncased-finetuned-sst-2-english', {
|
|
322
|
+
quantized: true, // Use quantized model (smaller, faster)
|
|
323
|
+
revision: 'main', // Model revision
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Provider Options
|
|
328
|
+
|
|
329
|
+
Pass provider-specific options to core functions:
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
const result = await classify({
|
|
333
|
+
model: transformers.classifier('Xenova/model'),
|
|
334
|
+
text: 'Hello world',
|
|
335
|
+
providerOptions: {
|
|
336
|
+
transformers: {
|
|
337
|
+
// Any Transformers.js specific options
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Preloading Models
|
|
344
|
+
|
|
345
|
+
For better UX, preload models before use:
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
// Models are automatically cached after first load
|
|
349
|
+
const classifier = transformers.classifier(
|
|
350
|
+
'Xenova/distilbert-base-uncased-finetuned-sst-2-english'
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
// First call downloads the model
|
|
354
|
+
const result = await classify({ model: classifier, text: 'Hello' });
|
|
355
|
+
|
|
356
|
+
// Subsequent calls are instant (loaded from cache)
|
|
357
|
+
const result2 = await classify({ model: classifier, text: 'World' });
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Browser Compatibility
|
|
361
|
+
|
|
362
|
+
| Browser | WebGPU | WASM | Notes |
|
|
363
|
+
| ----------- | ------ | ---- | ---------------------------- |
|
|
364
|
+
| Chrome 113+ | ✅ | ✅ | Best performance with WebGPU |
|
|
365
|
+
| Edge 113+ | ✅ | ✅ | Same as Chrome |
|
|
366
|
+
| Firefox | ❌ | ✅ | WASM only |
|
|
367
|
+
| Safari 18+ | ✅ | ✅ | WebGPU available |
|
|
368
|
+
| iOS Safari | ❌ | ✅ | WASM only |
|
|
369
|
+
|
|
370
|
+
## Performance Tips
|
|
371
|
+
|
|
372
|
+
1. **Use quantized models** - Smaller and faster with minimal quality loss
|
|
373
|
+
2. **Preload models** - Load during app init for instant inference
|
|
374
|
+
3. **Use WebGPU when available** - 3-5x faster than WASM
|
|
375
|
+
4. **Batch operations** - Process multiple inputs together
|
|
376
|
+
|
|
377
|
+
## License
|
|
378
|
+
|
|
379
|
+
[MIT](../../LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var b=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`,this.dimensions=this.getDimensionsFromModelId(e);}modelId;provider="transformers";dimensions;maxEmbeddingsPerCall=128;supportsParallelCalls=false;pipeline=null;loadPromise=null;getDimensionsFromModelId(e){let t=e.toLowerCase();return t.includes("minilm-l6")||t.includes("bge-small")?384:t.includes("mpnet")||t.includes("bge-base")?768:t.includes("e5-large")||t.includes("bge-large")?1024:384}async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e,env:t}=await import('@huggingface/transformers');t.backends.onnx.logSeverityLevel=3;let r=await e("feature-extraction",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=r,r})(),this.loadPromise)}async doEmbed(e){let{values:t,abortSignal:r}=e;r?.throwIfAborted();let i=await this.loadPipeline();r?.throwIfAborted();let l=[],n=0;for(let d of t){r?.throwIfAborted();let a=(await i(d,{pooling:"mean",normalize:true})).data,o=new Float32Array(a);l.push(o),n+=Math.ceil(d.split(/\s+/).length*1.3);}return {embeddings:l,usage:{tokens:n},response:{modelId:this.modelId,timestamp:new Date}}}};function B(s,e){return new b(s,e)}var y=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";labels=[];pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("text-classification",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doClassify(e){let{texts:t,abortSignal:r}=e,i=Date.now();r?.throwIfAborted();let l=await this.loadPipeline();r?.throwIfAborted();let n=[],d=0;for(let p of t){r?.throwIfAborted();let a=await l(p,{top_k:5}),o=Array.isArray(a)?a:[a],c=o[0],g={};for(let m of o)g[m.label]=m.score;n.push({label:c.label,score:c.score,allScores:g}),d+=Math.ceil(p.split(/\s+/).length*1.3);}return {results:n,usage:{inputTokens:d,durationMs:Date.now()-i}}}};function z(s,e){return new y(s,e)}var P=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("zero-shot-classification",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doClassifyZeroShot(e){let{texts:t,candidateLabels:r,multiLabel:i,abortSignal:l}=e,n=Date.now();l?.throwIfAborted();let d=await this.loadPipeline();l?.throwIfAborted();let p=[],a=0;for(let o of t){l?.throwIfAborted();let g=await d(o,r,{multi_label:i??false});p.push({labels:g.labels,scores:g.scores}),a+=Math.ceil(o.split(/\s+/).length*1.3);}return {results:p,usage:{inputTokens:a,durationMs:Date.now()-n}}}};function j(s,e){return new P(s,e)}var w=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";entityTypes=["PERSON","ORG","LOC","MISC"];pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("token-classification",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doExtract(e){let{texts:t,abortSignal:r}=e,i=Date.now();r?.throwIfAborted();let l=await this.loadPipeline();r?.throwIfAborted();let n=[],d=0;for(let p of t){r?.throwIfAborted();let a=await l(p,{aggregation_strategy:"simple"}),o=[],c=Array.isArray(a)?a:[a];for(let g of c){let u=(g.entity_group??g.entity).replace(/^[BI]-/,"");o.push({text:g.word,type:u,start:g.start,end:g.end,score:g.score});}n.push({entities:o}),d+=Math.ceil(p.split(/\s+/).length*1.3);}return {results:n,usage:{inputTokens:d,durationMs:Date.now()-i}}}};function F(s,e){return new w(s,e)}var M=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("text-classification",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doRerank(e){let{query:t,documents:r,topK:i,abortSignal:l}=e,n=Date.now();l?.throwIfAborted();let d=await this.loadPipeline();l?.throwIfAborted();let p=[],a=0;for(let c=0;c<r.length;c++){l?.throwIfAborted();let g=r[c],m=await d([t,g]),f=Array.isArray(m)?m[0]:m,h=f.score;(f.label.toLowerCase().includes("not")||f.label==="LABEL_0"||f.label==="0")&&(h=1-h),p.push({index:c,score:h,text:g}),a+=Math.ceil((t.split(/\s+/).length+g.split(/\s+/).length)*1.3);}return p.sort((c,g)=>g.score-c.score),{results:i?p.slice(0,i):p,usage:{inputTokens:a,durationMs:Date.now()-n}}}};function N(s,e){return new M(s,e)}var v=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";languages=["en","zh","de","es","ru","ko","fr","ja","pt","tr","pl","ca","nl","ar","sv","it","id","hi","fi","vi","he","uk","el","ms","cs","ro","da","hu","ta","no","th","ur","hr","bg","lt","la","mi","ml","cy","sk","te","fa","lv","bn","sr","az","sl","kn","et","mk","br","eu","is","hy","ne","mn","bs","kk","sq","sw","gl","mr","pa","si","km","sn","yo","so","af","oc","ka","be","tg","sd","gu","am","yi","lo","uz","fo","ht","ps","tk","nn","mt","sa","lb","my","bo","tl","mg","as","tt","haw","ln","ha","ba","jw","su"];pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("automatic-speech-recognition",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}objectUrls=[];createWavUrl(e,t=16e3){let r=new ArrayBuffer(44+e.length*2),i=new DataView(r),l=(a,o)=>{for(let c=0;c<o.length;c++)i.setUint8(a+c,o.charCodeAt(c));};l(0,"RIFF"),i.setUint32(4,36+e.length*2,true),l(8,"WAVE"),l(12,"fmt "),i.setUint32(16,16,true),i.setUint16(20,1,true),i.setUint16(22,1,true),i.setUint32(24,t,true),i.setUint32(28,t*2,true),i.setUint16(32,2,true),i.setUint16(34,16,true),l(36,"data"),i.setUint32(40,e.length*2,true);let n=44;for(let a=0;a<e.length;a++){let o=Math.max(-1,Math.min(1,e[a]));i.setInt16(n,o<0?o*32768:o*32767,true),n+=2;}let d=new Blob([r],{type:"audio/wav"}),p=URL.createObjectURL(d);return this.objectUrls.push(p),p}async prepareAudio(e){if(e instanceof Float32Array){if(typeof console<"u"){let r=0,i=0;for(let n=0;n<e.length;n++){r+=e[n]*e[n];let d=Math.abs(e[n]);d>i&&(i=d);}let l=Math.sqrt(r/e.length);console.log("[STT] Audio stats - samples:",e.length,"RMS:",l.toFixed(4),"Max:",i.toFixed(4),"Duration:",(e.length/16e3).toFixed(2)+"s"),l<.001&&console.warn("[STT] Warning: Audio appears to be nearly silent (RMS < 0.001)");}let t=this.createWavUrl(e,16e3);return console.log("[STT] Created WAV URL for pipeline"),t}if(e instanceof Blob){let t=URL.createObjectURL(e);return this.objectUrls.push(t),t}if(e instanceof ArrayBuffer){let t=new Blob([e],{type:"audio/wav"}),r=URL.createObjectURL(t);return this.objectUrls.push(r),r}return e}cleanupObjectUrls(){for(let e of this.objectUrls)URL.revokeObjectURL(e);this.objectUrls=[];}async doTranscribe(e){let{audio:t,language:r,task:i,returnTimestamps:l,abortSignal:n}=e,d=Date.now();n?.throwIfAborted();let p=await this.loadPipeline();n?.throwIfAborted();let a=await this.prepareAudio(t);n?.throwIfAborted();try{let o={language:r,task:i??"transcribe",return_timestamps:l??!1,chunk_length_s:30,stride_length_s:5};l==="word"&&(o.return_timestamps="word"),console.log("[STT] Pipeline options:",JSON.stringify(o));let c=await p(a,o);console.log("[STT] Raw output:",JSON.stringify(c));let g=c,m;g.chunks&&g.chunks.length>0&&(m=g.chunks.map(f=>({text:f.text,start:f.timestamp[0],end:f.timestamp[1]})));let u=0;return m&&m.length>0?u=m[m.length-1].end:t instanceof Float32Array&&(u=t.length/16e3),{text:g.text.trim(),segments:m,language:r,usage:{audioDurationSec:u,durationMs:Date.now()-d}}}finally{this.cleanupObjectUrls();}}};function Z(s,e){return new v(s,e)}var I=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("image-classification",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doClassify(e){let{images:t,topK:r=5,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[];for(let p of t){i?.throwIfAborted();let a=this.prepareImage(p),o=await n(a,{top_k:r}),c=Array.isArray(o)?o:[o];d.push(c.map(g=>({label:g.label,score:g.score})));}return {results:d,usage:{durationMs:Date.now()-l}}}};function Q(s,e){return new I(s,e)}var T=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("zero-shot-image-classification",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doClassifyZeroShot(e){let{images:t,candidateLabels:r,hypothesisTemplate:i,abortSignal:l}=e,n=Date.now();l?.throwIfAborted();let d=await this.loadPipeline();l?.throwIfAborted();let p=[];for(let a of t){l?.throwIfAborted();let o=this.prepareImage(a),c={};i&&(c.hypothesis_template=i);let g=await d(o,r,c),u=[...Array.isArray(g)?g:[g]].sort((f,h)=>h.score-f.score);p.push({labels:u.map(f=>f.label),scores:u.map(f=>f.score)});}return {results:p,usage:{durationMs:Date.now()-n}}}};function X(s,e){return new T(s,e)}var S=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("image-to-text",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doCaption(e){let{images:t,maxLength:r,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[];for(let p of t){i?.throwIfAborted();let a=this.prepareImage(p),o={};r&&(o.max_length=r);let c=await n(a,o),m=(Array.isArray(c)?c:[c])[0].generated_text;d.push(m.trim());}return {captions:d,usage:{durationMs:Date.now()-l}}}};function V(s,e){return new S(s,e)}var x=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`,t.segmentationType&&(this.segmentationType=t.segmentationType);}modelId;provider="transformers";segmentationType="semantic";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("image-segmentation",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doSegment(e){let{images:t,threshold:r=.5,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[];for(let p of t){i?.throwIfAborted();let a=this.prepareImage(p),o=await n(a,{threshold:r}),g=(Array.isArray(o)?o:[o]).map(m=>{let u=m.mask?.data??new Uint8Array(0);return {label:String(m.label??"unknown"),score:Number(m.score??0),mask:u}});d.push({masks:g});}return {results:d,usage:{durationMs:Date.now()-l}}}};function G(s,e){return new x(s,e)}var R=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("object-detection",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doDetect(e){let{images:t,threshold:r=.5,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[];for(let p of t){i?.throwIfAborted();let a=this.prepareImage(p),o=await n(a,{threshold:r}),g=(Array.isArray(o)?o:[o]).map(m=>{let u=m.box;return {label:String(m.label??"unknown"),score:Number(m.score??0),box:u?{x:u.xmin,y:u.ymin,width:u.xmax-u.xmin,height:u.ymax-u.ymin}:{x:0,y:0,width:0,height:0}}});d.push({objects:g});}return {results:d,usage:{durationMs:Date.now()-l}}}};function $(s,e){return new R(s,e)}var k=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`,this.dimensions=t.dimensions??512;}modelId;provider="transformers";dimensions;pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("image-feature-extraction",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doExtract(e){let{images:t,abortSignal:r}=e,i=Date.now();r?.throwIfAborted();let l=await this.loadPipeline();r?.throwIfAborted();let n=[];for(let d of t){r?.throwIfAborted();let p=this.prepareImage(d),a=await l(p,{pool:true}),o;if(a&&typeof a=="object"&&"tolist"in a){let c=a.tolist();o=Array.isArray(c[0])?c[0]:c;}else Array.isArray(a)?o=a.flat():o=[];n.push(new Float32Array(o));}return {features:n,usage:{durationMs:Date.now()-i}}}};function H(s,e){return new k(s,e)}var D=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`,this.taskType=t.taskType??"super-resolution";}modelId;provider="transformers";taskType;pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("image-to-image",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doTransform(e){let{images:t,abortSignal:r}=e,i=Date.now();r?.throwIfAborted();let l=await this.loadPipeline();r?.throwIfAborted();let n=[];for(let d of t){r?.throwIfAborted();let p=this.prepareImage(d),a=await l(p),o;a&&typeof a=="object"?"toBlob"in a&&typeof a.toBlob=="function"?o=await a.toBlob():a instanceof Blob?o=a:o=new Blob(["transformed image"],{type:"image/png"}):o=new Blob(["transformed image"],{type:"image/png"}),n.push(o);}return {images:n,usage:{durationMs:Date.now()-i}}}};function W(s,e){return new D(s,e)}var L=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`,t.sampleRate&&(this.sampleRate=t.sampleRate);}modelId;provider="transformers";sampleRate=16e3;voices=[];pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("text-to-speech",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doSynthesize(e){let{text:t,abortSignal:r}=e,i=Date.now();r?.throwIfAborted();let l=await this.loadPipeline();r?.throwIfAborted();let n=await l(t,{}),d=n.audio,p=n.sampling_rate||this.sampleRate;return {audio:this.floatToWav(d,p),sampleRate:p,usage:{characterCount:t.length,durationMs:Date.now()-i}}}floatToWav(e,t){let d=t*2,p=e.length*2,o=44+p,c=new ArrayBuffer(o),g=new DataView(c),m=(f,h)=>{for(let A=0;A<h.length;A++)g.setUint8(f+A,h.charCodeAt(A));};m(0,"RIFF"),g.setUint32(4,o-8,true),m(8,"WAVE"),m(12,"fmt "),g.setUint32(16,16,true),g.setUint16(20,1,true),g.setUint16(22,1,true),g.setUint32(24,t,true),g.setUint32(28,d,true),g.setUint16(32,2,true),g.setUint16(34,16,true),m(36,"data"),g.setUint32(40,p,true);let u=44;for(let f=0;f<e.length;f++){let h=Math.max(-1,Math.min(1,e[f]));g.setInt16(u+f*2,h*32767,true);}return new Blob([c],{type:"audio/wav"})}};function K(s,e){return new L(s,e)}var C=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("translation",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doTranslate(e){let{texts:t,abortSignal:r}=e,i=Date.now();r?.throwIfAborted();let l=await this.loadPipeline();r?.throwIfAborted();let n=[],d=0,p=0;for(let a of t){r?.throwIfAborted();let o=await l(a),c;Array.isArray(o)?c=o[0].translation_text:c=o.translation_text,n.push(c),d+=a.split(/\s+/).length,p+=c.split(/\s+/).length;}return {translations:n,usage:{inputTokens:d,outputTokens:p,durationMs:Date.now()-i}}}};function J(s,e){return new C(s,e)}var _=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("summarization",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doSummarize(e){let{texts:t,maxLength:r,minLength:i,abortSignal:l}=e,n=Date.now();l?.throwIfAborted();let d=await this.loadPipeline();l?.throwIfAborted();let p={};r!==void 0&&(p.max_length=r),i!==void 0&&(p.min_length=i);let a=[],o=0,c=0;for(let g of t){l?.throwIfAborted();let m=await d(g,p),u;Array.isArray(m)?u=m[0].summary_text:u=m.summary_text,a.push(u),o+=g.split(/\s+/).length,c+=u.split(/\s+/).length;}return {summaries:a,usage:{inputTokens:o,outputTokens:c,durationMs:Date.now()-n}}}};function Y(s,e){return new _(s,e)}var E=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`,t.maskToken&&(this.maskToken=t.maskToken);}modelId;provider="transformers";maskToken="[MASK]";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("fill-mask",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doFillMask(e){let{texts:t,topK:r=5,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[],p=0;for(let a of t){i?.throwIfAborted();let o=await n(a,{top_k:r}),g=(Array.isArray(o)?o:[o]).map(m=>({token:String(m.token_str??""),score:Number(m.score??0),sequence:String(m.sequence??"")}));d.push(g),p+=a.split(/\s+/).length;}return {results:d,usage:{inputTokens:p,durationMs:Date.now()-l}}}};function ee(s,e){return new E(s,e)}var O=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("question-answering",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async doAnswer(e){let{questions:t,topK:r=1,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[],p=0;for(let{question:a,context:o}of t){i?.throwIfAborted();let c=await n(a,o,{top_k:r}),m=(Array.isArray(c)?c:[c]).map(u=>({answer:String(u.answer??""),score:Number(u.score??0),start:Number(u.start??0),end:Number(u.end??0)}));d.push(m),p+=a.split(/\s+/).length+o.split(/\s+/).length;}return {results:d,usage:{inputTokens:p,durationMs:Date.now()-l}}}};function te(s,e){return new O(s,e)}var q=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("image-to-text",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}prepareImage(e){return typeof e=="string"||e instanceof Blob||e instanceof ImageData?e:e instanceof ArrayBuffer?new Blob([e],{type:"image/png"}):e}async doOCR(e){let{images:t,detectRegions:r,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=[],p=[];for(let a of t){i?.throwIfAborted();let o=this.prepareImage(a),c=await n(o),m=(Array.isArray(c)?c:[c]).map(u=>u.generated_text??"").join(" ").trim();d.push(m),r&&p.push([{text:m,bbox:{x:0,y:0,width:0,height:0},confidence:.95}]);}return {texts:d,regions:r?p:void 0,usage:{durationMs:Date.now()-l}}}};function re(s,e){return new q(s,e)}var U=class{constructor(e,t={}){this.baseModelId=e;this.settings=t;this.modelId=`transformers:${e}`;}modelId;provider="transformers";pipeline=null;loadPromise=null;async loadPipeline(){return this.pipeline?this.pipeline:this.loadPromise?this.loadPromise:(this.loadPromise=(async()=>{let{pipeline:e}=await import('@huggingface/transformers'),t=await e("document-question-answering",this.baseModelId,{device:this.settings.device??"auto",dtype:this.settings.quantized!==false?"q8":"fp32",progress_callback:this.settings.onProgress});return this.pipeline=t,t})(),this.loadPromise)}async prepareDocument(e){return typeof e=="string"||e instanceof Blob?e:e instanceof ArrayBuffer?new Blob([e],{type:"application/pdf"}):e}async doAskDocument(e){let{document:t,questions:r,abortSignal:i}=e,l=Date.now();i?.throwIfAborted();let n=await this.loadPipeline();i?.throwIfAborted();let d=await this.prepareDocument(t),p=[];for(let a of r){i?.throwIfAborted();let o=await n(d,a),c=Array.isArray(o)?o[0]:o,g={answer:String(c.answer??""),score:Number(c.score??0)};p.push(g);}return {answers:p,usage:{durationMs:Date.now()-l}}}async doAskTable(e){let{table:t,questions:r,abortSignal:i}=e,l=Date.now();return i?.throwIfAborted(),await this.loadPipeline(),i?.throwIfAborted(),{answers:r.map(()=>({answer:"Table QA requires TAPAS model. Please use a dedicated table QA model.",score:.5})),usage:{durationMs:Date.now()-l}}}};function se(s,e){return new U(s,e)}function ie(s){let e={device:s?.device,quantized:s?.quantized??true,onProgress:s?.onProgress};return {embedding(t,r){return B(t,{...e,...r})},classifier(t,r){return z(t,{...e,...r})},zeroShot(t,r){return j(t,{...e,...r})},ner(t,r){return F(t,{...e,...r})},reranker(t,r){return N(t,{...e,...r})},speechToText(t,r){return Z(t,{...e,...r})},imageClassifier(t,r){return Q(t,{...e,...r})},zeroShotImageClassifier(t,r){return X(t,{...e,...r})},captioner(t,r){return V(t,{...e,...r})},segmenter(t,r){return G(t,{...e,...r})},objectDetector(t,r){return $(t,{...e,...r})},imageFeatures(t,r){return H(t,{...e,...r})},imageToImage(t,r){return W(t,{...e,...r})},textToSpeech(t,r){return K(t,{...e,...r})},translator(t,r){return J(t,{...e,...r})},summarizer(t,r){return Y(t,{...e,...r})},fillMask(t,r){return ee(t,{...e,...r})},questionAnswering(t,r){return te(t,{...e,...r})},ocr(t,r){return re(t,{...e,...r})},documentQA(t,r){return se(t,{...e,...r})}}}var fe=ie();async function oe(){if(typeof navigator>"u"||!("gpu"in navigator))return false;try{let s=navigator.gpu;return s?await s.requestAdapter()!==null:!1}catch{return false}}async function he(){return await oe()?"webgpu":"wasm"}async function be(s){if(typeof caches>"u")return false;try{return (await(await caches.open("transformers-cache")).keys()).some(r=>r.url.includes(s.replace("/","%2F")))}catch{return false}}async function ye(s,e){let{pipeline:t}=await import('@huggingface/transformers'),r="feature-extraction";s.toLowerCase().includes("whisper")?r="automatic-speech-recognition":s.toLowerCase().includes("blip")||s.toLowerCase().includes("caption")?r="image-to-text":s.toLowerCase().includes("clip")?r="zero-shot-image-classification":s.toLowerCase().includes("vit")||s.toLowerCase().includes("resnet")?r="image-classification":s.toLowerCase().includes("ner")||s.toLowerCase().includes("token")?r="token-classification":s.toLowerCase().includes("mnli")||s.toLowerCase().includes("nli")||s.toLowerCase().includes("bart")?r="zero-shot-classification":(s.toLowerCase().includes("sst")||s.toLowerCase().includes("sentiment")||s.toLowerCase().includes("distilbert")||s.toLowerCase().includes("rerank")||s.toLowerCase().includes("marco"))&&(r="text-classification");let l={progress_callback:e?.onProgress?n=>{e.onProgress?.({status:n.status,name:n.name,file:n.file,progress:n.progress,loaded:n.loaded,total:n.total});}:void 0};e?.quantized!==void 0&&(l.dtype=e.quantized?"q8":"fp32"),await t(r,s,l);}async function Pe(){if(typeof caches>"u")return false;try{return await caches.delete("transformers-cache")}catch{return false}}async function we(){if(typeof navigator>"u"||!("storage"in navigator))return 0;try{return (await navigator.storage.estimate()).usage??0}catch{return 0}}var ne={ALL_MINILM_L6_V2:"Xenova/all-MiniLM-L6-v2",PARAPHRASE_MULTILINGUAL_MINILM:"Xenova/paraphrase-multilingual-MiniLM-L12-v2",ALL_MPNET_BASE_V2:"Xenova/all-mpnet-base-v2",BGE_SMALL_EN:"Xenova/bge-small-en-v1.5",BGE_BASE_EN:"Xenova/bge-base-en-v1.5"},ae={DISTILBERT_SST2:"Xenova/distilbert-base-uncased-finetuned-sst-2-english",TWITTER_ROBERTA_SENTIMENT:"Xenova/twitter-roberta-base-sentiment-latest",DISTILROBERTA_EMOTION:"Xenova/distilroberta-base-emotion"},le={BART_LARGE_MNLI:"Xenova/bart-large-mnli",DISTILBART_MNLI:"Xenova/distilbart-mnli-12-3"},pe={BERT_BASE_NER:"Xenova/bert-base-NER",XLM_ROBERTA_NER:"Xenova/xlm-roberta-large-finetuned-conll03-english"},ce={MS_MARCO_MINILM_L6:"Xenova/ms-marco-MiniLM-L-6-v2",BGE_RERANKER_BASE:"Xenova/bge-reranker-base"},de={WHISPER_TINY:"Xenova/whisper-tiny",WHISPER_SMALL:"Xenova/whisper-small",WHISPER_BASE:"Xenova/whisper-base",DISTIL_WHISPER_SMALL:"Xenova/distil-whisper-small.en"},ge={VIT_BASE_PATCH16:"Xenova/vit-base-patch16-224",VIT_SMALL_PATCH16:"Xenova/vit-small-patch16-224",RESNET_50:"Xenova/resnet-50"},me={CLIP_VIT_BASE_PATCH32:"Xenova/clip-vit-base-patch32",CLIP_VIT_LARGE_PATCH14:"Xenova/clip-vit-large-patch14",SIGLIP_BASE_PATCH16:"Xenova/siglip-base-patch16-224"},ue={BLIP_BASE:"Xenova/blip-image-captioning-base",BLIP_LARGE:"Xenova/blip-image-captioning-large",GIT_BASE:"Xenova/git-base-coco"},Me={embedding:ne,classification:ae,zeroShot:le,ner:pe,reranker:ce,speechToText:de,imageClassification:ge,zeroShotImage:me,imageCaption:ue};exports.CLASSIFICATION_MODELS=ae;exports.EMBEDDING_MODELS=ne;exports.IMAGE_CAPTION_MODELS=ue;exports.IMAGE_CLASSIFICATION_MODELS=ge;exports.MODELS=Me;exports.NER_MODELS=pe;exports.RERANKER_MODELS=ce;exports.SPEECH_TO_TEXT_MODELS=de;exports.TransformersCaptionModel=S;exports.TransformersClassificationModel=y;exports.TransformersEmbeddingModel=b;exports.TransformersImageClassificationModel=I;exports.TransformersNERModel=w;exports.TransformersRerankerModel=M;exports.TransformersSpeechToTextModel=v;exports.TransformersZeroShotImageModel=T;exports.TransformersZeroShotModel=P;exports.ZERO_SHOT_IMAGE_MODELS=me;exports.ZERO_SHOT_MODELS=le;exports.clearModelCache=Pe;exports.createTransformers=ie;exports.getModelStorageUsage=we;exports.getOptimalDevice=he;exports.isModelCached=be;exports.isWebGPUAvailable=oe;exports.preloadModel=ye;exports.transformers=fe;//# sourceMappingURL=index.cjs.map
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|