edgeflowjs 0.1.0 → 0.3.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 +200 -66
- package/dist/backends/index.d.ts +9 -2
- package/dist/backends/index.d.ts.map +1 -1
- package/dist/backends/index.js +13 -13
- package/dist/backends/index.js.map +1 -1
- package/dist/backends/onnx.d.ts +49 -4
- package/dist/backends/onnx.d.ts.map +1 -1
- package/dist/backends/onnx.js +165 -76
- package/dist/backends/onnx.js.map +1 -1
- package/dist/backends/transformers-adapter.d.ts +99 -0
- package/dist/backends/transformers-adapter.d.ts.map +1 -0
- package/dist/backends/transformers-adapter.js +171 -0
- package/dist/backends/transformers-adapter.js.map +1 -0
- package/dist/backends/webgpu.d.ts +7 -5
- package/dist/backends/webgpu.d.ts.map +1 -1
- package/dist/backends/webgpu.js +7 -5
- package/dist/backends/webgpu.js.map +1 -1
- package/dist/backends/webnn.d.ts +6 -5
- package/dist/backends/webnn.d.ts.map +1 -1
- package/dist/backends/webnn.js +6 -5
- package/dist/backends/webnn.js.map +1 -1
- package/dist/core/composer.d.ts +118 -0
- package/dist/core/composer.d.ts.map +1 -0
- package/dist/core/composer.js +163 -0
- package/dist/core/composer.js.map +1 -0
- package/dist/core/device-profiler.d.ts +75 -0
- package/dist/core/device-profiler.d.ts.map +1 -0
- package/dist/core/device-profiler.js +131 -0
- package/dist/core/device-profiler.js.map +1 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +8 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/memory.d.ts +22 -2
- package/dist/core/memory.d.ts.map +1 -1
- package/dist/core/memory.js +49 -13
- package/dist/core/memory.js.map +1 -1
- package/dist/core/plugin.d.ts +100 -0
- package/dist/core/plugin.d.ts.map +1 -0
- package/dist/core/plugin.js +106 -0
- package/dist/core/plugin.js.map +1 -0
- package/dist/core/runtime.d.ts +4 -0
- package/dist/core/runtime.d.ts.map +1 -1
- package/dist/core/runtime.js +18 -0
- package/dist/core/runtime.js.map +1 -1
- package/dist/core/scheduler.d.ts +17 -0
- package/dist/core/scheduler.d.ts.map +1 -1
- package/dist/core/scheduler.js +101 -3
- package/dist/core/scheduler.js.map +1 -1
- package/dist/core/types.d.ts +14 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/worker.d.ts +202 -0
- package/dist/core/worker.d.ts.map +1 -0
- package/dist/core/worker.js +477 -0
- package/dist/core/worker.js.map +1 -0
- package/dist/edgeflow.browser.js +9789 -4379
- package/dist/edgeflow.browser.js.map +4 -4
- package/dist/edgeflow.browser.min.js +435 -5
- package/dist/edgeflow.browser.min.js.map +4 -4
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -12
- package/dist/index.js.map +1 -1
- package/dist/pipelines/automatic-speech-recognition.d.ts +63 -0
- package/dist/pipelines/automatic-speech-recognition.d.ts.map +1 -0
- package/dist/pipelines/automatic-speech-recognition.js +269 -0
- package/dist/pipelines/automatic-speech-recognition.js.map +1 -0
- package/dist/pipelines/base.d.ts +6 -1
- package/dist/pipelines/base.d.ts.map +1 -1
- package/dist/pipelines/base.js +12 -2
- package/dist/pipelines/base.js.map +1 -1
- package/dist/pipelines/feature-extraction.d.ts +5 -40
- package/dist/pipelines/feature-extraction.d.ts.map +1 -1
- package/dist/pipelines/feature-extraction.js +44 -63
- package/dist/pipelines/feature-extraction.js.map +1 -1
- package/dist/pipelines/image-classification.d.ts +4 -36
- package/dist/pipelines/image-classification.d.ts.map +1 -1
- package/dist/pipelines/image-classification.js +22 -60
- package/dist/pipelines/image-classification.js.map +1 -1
- package/dist/pipelines/image-segmentation.d.ts +221 -0
- package/dist/pipelines/image-segmentation.d.ts.map +1 -0
- package/dist/pipelines/image-segmentation.js +535 -0
- package/dist/pipelines/image-segmentation.js.map +1 -0
- package/dist/pipelines/index.d.ts +18 -0
- package/dist/pipelines/index.d.ts.map +1 -1
- package/dist/pipelines/index.js +51 -2
- package/dist/pipelines/index.js.map +1 -1
- package/dist/pipelines/object-detection.d.ts +44 -0
- package/dist/pipelines/object-detection.d.ts.map +1 -0
- package/dist/pipelines/object-detection.js +218 -0
- package/dist/pipelines/object-detection.js.map +1 -0
- package/dist/pipelines/question-answering.d.ts +41 -0
- package/dist/pipelines/question-answering.d.ts.map +1 -0
- package/dist/pipelines/question-answering.js +164 -0
- package/dist/pipelines/question-answering.js.map +1 -0
- package/dist/pipelines/text-classification.d.ts +3 -39
- package/dist/pipelines/text-classification.d.ts.map +1 -1
- package/dist/pipelines/text-classification.js +29 -67
- package/dist/pipelines/text-classification.js.map +1 -1
- package/dist/pipelines/text-generation.d.ts +281 -0
- package/dist/pipelines/text-generation.d.ts.map +1 -0
- package/dist/pipelines/text-generation.js +766 -0
- package/dist/pipelines/text-generation.js.map +1 -0
- package/dist/pipelines/zero-shot-classification.d.ts +45 -0
- package/dist/pipelines/zero-shot-classification.d.ts.map +1 -0
- package/dist/pipelines/zero-shot-classification.js +140 -0
- package/dist/pipelines/zero-shot-classification.js.map +1 -0
- package/dist/tools/benchmark.d.ts +92 -0
- package/dist/tools/benchmark.d.ts.map +1 -0
- package/dist/tools/benchmark.js +213 -0
- package/dist/tools/benchmark.js.map +1 -0
- package/dist/tools/debugger.d.ts +258 -0
- package/dist/tools/debugger.d.ts.map +1 -0
- package/dist/tools/debugger.js +624 -0
- package/dist/tools/debugger.js.map +1 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +16 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/monitor.d.ts +284 -0
- package/dist/tools/monitor.d.ts.map +1 -0
- package/dist/tools/monitor.js +921 -0
- package/dist/tools/monitor.js.map +1 -0
- package/dist/tools/quantization.d.ts +235 -0
- package/dist/tools/quantization.d.ts.map +1 -0
- package/dist/tools/quantization.js +830 -0
- package/dist/tools/quantization.js.map +1 -0
- package/dist/utils/hub.d.ts +162 -0
- package/dist/utils/hub.d.ts.map +1 -0
- package/dist/utils/hub.js +311 -0
- package/dist/utils/hub.js.map +1 -0
- package/dist/utils/index.d.ts +3 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +5 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/model-loader.d.ts.map +1 -1
- package/dist/utils/model-loader.js +106 -30
- package/dist/utils/model-loader.js.map +1 -1
- package/dist/utils/offline.d.ts +147 -0
- package/dist/utils/offline.d.ts.map +1 -0
- package/dist/utils/offline.js +405 -0
- package/dist/utils/offline.js.map +1 -0
- package/dist/utils/preprocessor.d.ts +82 -6
- package/dist/utils/preprocessor.d.ts.map +1 -1
- package/dist/utils/preprocessor.js +278 -21
- package/dist/utils/preprocessor.js.map +1 -1
- package/dist/utils/tokenizer.d.ts +197 -72
- package/dist/utils/tokenizer.d.ts.map +1 -1
- package/dist/utils/tokenizer.js +558 -274
- package/dist/utils/tokenizer.js.map +1 -1
- package/package.json +26 -11
|
@@ -7,65 +7,29 @@
|
|
|
7
7
|
import { PipelineConfig, PipelineOptions } from '../core/types.js';
|
|
8
8
|
import { EdgeFlowTensor } from '../core/tensor.js';
|
|
9
9
|
import { BasePipeline, TextClassificationResult } from './base.js';
|
|
10
|
-
/**
|
|
11
|
-
* Text classification options
|
|
12
|
-
*/
|
|
13
10
|
export interface TextClassificationOptions extends PipelineOptions {
|
|
14
|
-
/** Return all labels with scores */
|
|
15
11
|
returnAllScores?: boolean;
|
|
16
|
-
/** Custom labels */
|
|
17
12
|
labels?: string[];
|
|
18
|
-
/** Number of labels to return */
|
|
19
13
|
topK?: number;
|
|
20
14
|
}
|
|
21
|
-
/**
|
|
22
|
-
* TextClassificationPipeline - Classify text into categories
|
|
23
|
-
*/
|
|
24
15
|
export declare class TextClassificationPipeline extends BasePipeline<string | string[], TextClassificationResult | TextClassificationResult[]> {
|
|
25
16
|
private tokenizer;
|
|
17
|
+
private onnxModel;
|
|
26
18
|
private labels;
|
|
19
|
+
private modelUrl;
|
|
20
|
+
private tokenizerUrl;
|
|
27
21
|
constructor(config: PipelineConfig, labels?: string[]);
|
|
28
|
-
/**
|
|
29
|
-
* Initialize pipeline
|
|
30
|
-
*/
|
|
31
22
|
initialize(): Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
* Set custom labels
|
|
34
|
-
*/
|
|
35
23
|
setLabels(labels: string[]): void;
|
|
36
|
-
/**
|
|
37
|
-
* Run classification
|
|
38
|
-
*/
|
|
39
24
|
run(input: string | string[], options?: TextClassificationOptions): Promise<TextClassificationResult | TextClassificationResult[]>;
|
|
40
|
-
/**
|
|
41
|
-
* Preprocess text input
|
|
42
|
-
*/
|
|
43
25
|
protected preprocess(input: string | string[]): Promise<EdgeFlowTensor[]>;
|
|
44
|
-
/**
|
|
45
|
-
* Run model inference
|
|
46
|
-
*/
|
|
47
26
|
private runInference;
|
|
48
|
-
/**
|
|
49
|
-
* Postprocess model outputs
|
|
50
|
-
*/
|
|
51
27
|
protected postprocess(outputs: EdgeFlowTensor[], options?: TextClassificationOptions): Promise<TextClassificationResult>;
|
|
52
28
|
}
|
|
53
|
-
/**
|
|
54
|
-
* SentimentAnalysisPipeline - Specialized for sentiment analysis
|
|
55
|
-
*/
|
|
56
29
|
export declare class SentimentAnalysisPipeline extends TextClassificationPipeline {
|
|
57
30
|
constructor(config: PipelineConfig);
|
|
58
|
-
/**
|
|
59
|
-
* Analyze sentiment
|
|
60
|
-
*/
|
|
61
31
|
analyze(text: string | string[], options?: TextClassificationOptions): Promise<TextClassificationResult | TextClassificationResult[]>;
|
|
62
32
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Create text classification pipeline
|
|
65
|
-
*/
|
|
66
33
|
export declare function createTextClassificationPipeline(config?: Partial<PipelineConfig>): TextClassificationPipeline;
|
|
67
|
-
/**
|
|
68
|
-
* Create sentiment analysis pipeline
|
|
69
|
-
*/
|
|
70
34
|
export declare function createSentimentAnalysisPipeline(config?: Partial<PipelineConfig>): SentimentAnalysisPipeline;
|
|
71
35
|
//# sourceMappingURL=text-classification.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-classification.d.ts","sourceRoot":"","sources":["../../src/pipelines/text-classification.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,cAAc,EACd,eAAe,
|
|
1
|
+
{"version":3,"file":"text-classification.d.ts","sourceRoot":"","sources":["../../src/pipelines/text-classification.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,cAAc,EACd,eAAe,EAEhB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAW,MAAM,mBAAmB,CAAC;AAI5D,OAAO,EACL,YAAY,EACZ,wBAAwB,EAGzB,MAAM,WAAW,CAAC;AAiBnB,MAAM,WAAW,yBAA0B,SAAQ,eAAe;IAChE,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,0BAA2B,SAAQ,YAAY,CAC1D,MAAM,GAAG,MAAM,EAAE,EACjB,wBAAwB,GAAG,wBAAwB,EAAE,CACtD;IACC,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,SAAS,CAA4B;IAC7C,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,YAAY,CAAS;gBAEjB,MAAM,EAAE,cAAc,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE;IAOtC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAa1C,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI;IAIlB,GAAG,CAChB,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,EACxB,OAAO,CAAC,EAAE,yBAAyB,GAClC,OAAO,CAAC,wBAAwB,GAAG,wBAAwB,EAAE,CAAC;cAwBxC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;YAwB1E,YAAY;cASD,WAAW,CAClC,OAAO,EAAE,cAAc,EAAE,EACzB,OAAO,CAAC,EAAE,yBAAyB,GAClC,OAAO,CAAC,wBAAwB,CAAC;CA0BrC;AAMD,qBAAa,yBAA0B,SAAQ,0BAA0B;gBAC3D,MAAM,EAAE,cAAc;IAI5B,OAAO,CACX,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,EACvB,OAAO,CAAC,EAAE,yBAAyB,GAClC,OAAO,CAAC,wBAAwB,GAAG,wBAAwB,EAAE,CAAC;CAGlE;AAMD,wBAAgB,gCAAgC,CAC9C,MAAM,GAAE,OAAO,CAAC,cAAc,CAAM,GACnC,0BAA0B,CAQ5B;AAED,wBAAgB,+BAA+B,CAC7C,MAAM,GAAE,OAAO,CAAC,cAAc,CAAM,GACnC,yBAAyB,CAQ3B"}
|
|
@@ -5,37 +5,43 @@
|
|
|
5
5
|
* sentiment analysis, topic classification, etc.
|
|
6
6
|
*/
|
|
7
7
|
import { EdgeFlowTensor, softmax } from '../core/tensor.js';
|
|
8
|
-
import {
|
|
8
|
+
import { Tokenizer } from '../utils/tokenizer.js';
|
|
9
|
+
import { loadModelData } from '../utils/model-loader.js';
|
|
10
|
+
import { loadModelFromBuffer, runInferenceNamed } from '../core/runtime.js';
|
|
9
11
|
import { BasePipeline, registerPipeline, SENTIMENT_LABELS, } from './base.js';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// Default Model (DistilBERT fine-tuned on SST-2)
|
|
14
|
+
// ============================================================================
|
|
15
|
+
const DEFAULT_MODELS = {
|
|
16
|
+
model: 'https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/onnx/model_quantized.onnx',
|
|
17
|
+
tokenizer: 'https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/tokenizer.json',
|
|
18
|
+
};
|
|
19
|
+
const DEFAULT_SST2_LABELS = ['NEGATIVE', 'POSITIVE'];
|
|
13
20
|
export class TextClassificationPipeline extends BasePipeline {
|
|
14
21
|
tokenizer = null;
|
|
22
|
+
onnxModel = null;
|
|
15
23
|
labels;
|
|
24
|
+
modelUrl;
|
|
25
|
+
tokenizerUrl;
|
|
16
26
|
constructor(config, labels) {
|
|
17
27
|
super(config);
|
|
18
|
-
this.labels = labels ??
|
|
28
|
+
this.labels = labels ?? DEFAULT_SST2_LABELS;
|
|
29
|
+
this.modelUrl = config.model !== 'default' ? config.model : DEFAULT_MODELS.model;
|
|
30
|
+
this.tokenizerUrl = DEFAULT_MODELS.tokenizer;
|
|
19
31
|
}
|
|
20
|
-
/**
|
|
21
|
-
* Initialize pipeline
|
|
22
|
-
*/
|
|
23
32
|
async initialize() {
|
|
24
33
|
await super.initialize();
|
|
25
|
-
// Initialize tokenizer
|
|
26
34
|
if (!this.tokenizer) {
|
|
27
|
-
this.tokenizer =
|
|
35
|
+
this.tokenizer = await Tokenizer.fromUrl(this.tokenizerUrl);
|
|
36
|
+
}
|
|
37
|
+
if (!this.onnxModel) {
|
|
38
|
+
const modelData = await loadModelData(this.modelUrl, { cache: this.config.cache ?? true });
|
|
39
|
+
this.onnxModel = await loadModelFromBuffer(modelData);
|
|
28
40
|
}
|
|
29
41
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Set custom labels
|
|
32
|
-
*/
|
|
33
42
|
setLabels(labels) {
|
|
34
43
|
this.labels = labels;
|
|
35
44
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Run classification
|
|
38
|
-
*/
|
|
39
45
|
async run(input, options) {
|
|
40
46
|
const isBatch = Array.isArray(input);
|
|
41
47
|
const inputs = isBatch ? input : [input];
|
|
@@ -43,73 +49,42 @@ export class TextClassificationPipeline extends BasePipeline {
|
|
|
43
49
|
const startTime = performance.now();
|
|
44
50
|
const results = [];
|
|
45
51
|
for (const text of inputs) {
|
|
46
|
-
// Preprocess
|
|
47
52
|
const tensorInputs = await this.preprocess(text);
|
|
48
|
-
// Run inference
|
|
49
53
|
const outputs = await this.runInference(tensorInputs);
|
|
50
|
-
// Postprocess
|
|
51
54
|
const result = await this.postprocess(outputs, options);
|
|
52
55
|
results.push(result);
|
|
53
56
|
}
|
|
54
57
|
const processingTime = performance.now() - startTime;
|
|
55
|
-
// Add processing time to results
|
|
56
58
|
for (const result of results) {
|
|
57
59
|
result.processingTime = processingTime / results.length;
|
|
58
60
|
}
|
|
59
61
|
return isBatch ? results : results[0];
|
|
60
62
|
}
|
|
61
|
-
/**
|
|
62
|
-
* Preprocess text input
|
|
63
|
-
*/
|
|
64
63
|
async preprocess(input) {
|
|
65
64
|
const text = Array.isArray(input) ? input[0] : input;
|
|
66
|
-
// Tokenize
|
|
67
65
|
const encoded = this.tokenizer.encode(text, {
|
|
68
66
|
maxLength: 128,
|
|
69
67
|
padding: 'max_length',
|
|
70
68
|
truncation: true,
|
|
71
69
|
});
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
const attentionMask = new EdgeFlowTensor(new Float32Array(encoded.attentionMask), [1, encoded.attentionMask.length], 'float32');
|
|
70
|
+
const inputIds = new EdgeFlowTensor(BigInt64Array.from(encoded.inputIds.map(id => BigInt(id))), [1, encoded.inputIds.length], 'int64');
|
|
71
|
+
const attentionMask = new EdgeFlowTensor(BigInt64Array.from(encoded.attentionMask.map(m => BigInt(m))), [1, encoded.attentionMask.length], 'int64');
|
|
75
72
|
return [inputIds, attentionMask];
|
|
76
73
|
}
|
|
77
|
-
/**
|
|
78
|
-
* Run model inference
|
|
79
|
-
*/
|
|
80
74
|
async runInference(inputs) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
const inputData = inputs[0]?.toFloat32Array() ?? new Float32Array(0);
|
|
87
|
-
const sum = inputData.reduce((a, b) => a + b, 0);
|
|
88
|
-
// Generate pseudo-random but deterministic scores
|
|
89
|
-
for (let i = 0; i < numClasses; i++) {
|
|
90
|
-
logits[i] = Math.sin(sum * (i + 1)) * 2;
|
|
91
|
-
}
|
|
92
|
-
return [new EdgeFlowTensor(logits, [1, numClasses], 'float32')];
|
|
75
|
+
const namedInputs = new Map();
|
|
76
|
+
namedInputs.set('input_ids', inputs[0]);
|
|
77
|
+
namedInputs.set('attention_mask', inputs[1]);
|
|
78
|
+
const outputs = await runInferenceNamed(this.onnxModel, namedInputs);
|
|
79
|
+
return outputs;
|
|
93
80
|
}
|
|
94
|
-
/**
|
|
95
|
-
* Postprocess model outputs
|
|
96
|
-
*/
|
|
97
81
|
async postprocess(outputs, options) {
|
|
98
82
|
const logits = outputs[0];
|
|
99
83
|
if (!logits) {
|
|
100
84
|
return { label: 'unknown', score: 0 };
|
|
101
85
|
}
|
|
102
|
-
// Apply softmax
|
|
103
86
|
const probs = softmax(logits, -1);
|
|
104
87
|
const probsArray = probs.toFloat32Array();
|
|
105
|
-
// Get predictions
|
|
106
|
-
const topK = options?.topK ?? 1;
|
|
107
|
-
const returnAllScores = options?.returnAllScores ?? false;
|
|
108
|
-
if (returnAllScores || topK > 1) {
|
|
109
|
-
// Return multiple results - for simplicity, return top-1 here
|
|
110
|
-
// Full implementation would return sorted array
|
|
111
|
-
}
|
|
112
|
-
// Find argmax
|
|
113
88
|
let maxIdx = 0;
|
|
114
89
|
let maxScore = probsArray[0] ?? 0;
|
|
115
90
|
for (let i = 1; i < probsArray.length; i++) {
|
|
@@ -128,16 +103,10 @@ export class TextClassificationPipeline extends BasePipeline {
|
|
|
128
103
|
// ============================================================================
|
|
129
104
|
// Sentiment Analysis Pipeline
|
|
130
105
|
// ============================================================================
|
|
131
|
-
/**
|
|
132
|
-
* SentimentAnalysisPipeline - Specialized for sentiment analysis
|
|
133
|
-
*/
|
|
134
106
|
export class SentimentAnalysisPipeline extends TextClassificationPipeline {
|
|
135
107
|
constructor(config) {
|
|
136
108
|
super(config, SENTIMENT_LABELS);
|
|
137
109
|
}
|
|
138
|
-
/**
|
|
139
|
-
* Analyze sentiment
|
|
140
|
-
*/
|
|
141
110
|
async analyze(text, options) {
|
|
142
111
|
return this.run(text, options);
|
|
143
112
|
}
|
|
@@ -145,9 +114,6 @@ export class SentimentAnalysisPipeline extends TextClassificationPipeline {
|
|
|
145
114
|
// ============================================================================
|
|
146
115
|
// Factory Functions
|
|
147
116
|
// ============================================================================
|
|
148
|
-
/**
|
|
149
|
-
* Create text classification pipeline
|
|
150
|
-
*/
|
|
151
117
|
export function createTextClassificationPipeline(config = {}) {
|
|
152
118
|
return new TextClassificationPipeline({
|
|
153
119
|
task: 'text-classification',
|
|
@@ -157,9 +123,6 @@ export function createTextClassificationPipeline(config = {}) {
|
|
|
157
123
|
quantization: config.quantization,
|
|
158
124
|
});
|
|
159
125
|
}
|
|
160
|
-
/**
|
|
161
|
-
* Create sentiment analysis pipeline
|
|
162
|
-
*/
|
|
163
126
|
export function createSentimentAnalysisPipeline(config = {}) {
|
|
164
127
|
return new SentimentAnalysisPipeline({
|
|
165
128
|
task: 'sentiment-analysis',
|
|
@@ -169,7 +132,6 @@ export function createSentimentAnalysisPipeline(config = {}) {
|
|
|
169
132
|
quantization: config.quantization,
|
|
170
133
|
});
|
|
171
134
|
}
|
|
172
|
-
// Register pipelines
|
|
173
135
|
registerPipeline('text-classification', (config) => new TextClassificationPipeline(config));
|
|
174
136
|
registerPipeline('sentiment-analysis', (config) => new SentimentAnalysisPipeline(config));
|
|
175
137
|
//# sourceMappingURL=text-classification.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-classification.js","sourceRoot":"","sources":["../../src/pipelines/text-classification.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"text-classification.js","sourceRoot":"","sources":["../../src/pipelines/text-classification.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE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C;IAClC,CAAC;IAED,KAAK,CAAC,OAAO,CACX,IAAuB,EACvB,OAAmC;QAEnC,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjC,CAAC;CACF;AAED,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E,MAAM,UAAU,gCAAgC,CAC9C,SAAkC,EAAE;IAEpC,OAAO,IAAI,0BAA0B,CAAC;QACpC,IAAI,EAAE,qBAAqB;QAC3B,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,SAAS;QAChC,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI;QAC3B,YAAY,EAAE,MAAM,CAAC,YAAY;KAClC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,+BAA+B,CAC7C,SAAkC,EAAE;IAEpC,OAAO,IAAI,yBAAyB,CAAC;QACnC,IAAI,EAAE,oBAAoB;QAC1B,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,SAAS;QAChC,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI;QAC3B,YAAY,EAAE,MAAM,CAAC,YAAY;KAClC,CAAC,CAAC;AACL,CAAC;AAED,gBAAgB,CAAC,qBAAqB,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,0BAA0B,CAAC,MAAM,CAAC,CAAC,CAAC;AAC5F,gBAAgB,CAAC,oBAAoB,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,yBAAyB,CAAC,MAAM,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* edgeFlow.js - Text Generation Pipeline
|
|
3
|
+
*
|
|
4
|
+
* Autoregressive text generation with streaming support.
|
|
5
|
+
* Supports GPT-2, LLaMA, Mistral, and other causal LM models.
|
|
6
|
+
* Includes chat/conversation support with message history.
|
|
7
|
+
*/
|
|
8
|
+
import { BasePipeline, PipelineResult } from './base.js';
|
|
9
|
+
import { Tokenizer } from '../utils/tokenizer.js';
|
|
10
|
+
import { EdgeFlowTensor } from '../core/tensor.js';
|
|
11
|
+
import { PipelineConfig, PipelineOptions } from '../core/types.js';
|
|
12
|
+
/**
|
|
13
|
+
* LLM model loading progress callback
|
|
14
|
+
*/
|
|
15
|
+
export interface LLMLoadProgress {
|
|
16
|
+
/** Stage: 'tokenizer' or 'model' */
|
|
17
|
+
stage: 'tokenizer' | 'model';
|
|
18
|
+
/** Bytes loaded */
|
|
19
|
+
loaded: number;
|
|
20
|
+
/** Total bytes */
|
|
21
|
+
total: number;
|
|
22
|
+
/** Progress percentage (0-100) */
|
|
23
|
+
progress: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Chat message
|
|
27
|
+
*/
|
|
28
|
+
export interface ChatMessage {
|
|
29
|
+
/** Role: 'system', 'user', or 'assistant' */
|
|
30
|
+
role: 'system' | 'user' | 'assistant';
|
|
31
|
+
/** Message content */
|
|
32
|
+
content: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Chat template type
|
|
36
|
+
*/
|
|
37
|
+
export type ChatTemplateType = 'chatml' | 'llama2' | 'llama3' | 'mistral' | 'phi3' | 'alpaca' | 'vicuna' | 'custom';
|
|
38
|
+
/**
|
|
39
|
+
* Text generation options
|
|
40
|
+
*/
|
|
41
|
+
export interface TextGenerationOptions {
|
|
42
|
+
/** Maximum number of new tokens to generate */
|
|
43
|
+
maxNewTokens?: number;
|
|
44
|
+
/** Maximum total length (prompt + generated) */
|
|
45
|
+
maxLength?: number;
|
|
46
|
+
/** Minimum number of new tokens to generate */
|
|
47
|
+
minNewTokens?: number;
|
|
48
|
+
/** Sampling temperature (higher = more random) */
|
|
49
|
+
temperature?: number;
|
|
50
|
+
/** Top-k sampling (0 = disabled) */
|
|
51
|
+
topK?: number;
|
|
52
|
+
/** Top-p (nucleus) sampling (1.0 = disabled) */
|
|
53
|
+
topP?: number;
|
|
54
|
+
/** Repetition penalty (1.0 = disabled) */
|
|
55
|
+
repetitionPenalty?: number;
|
|
56
|
+
/** Stop sequences */
|
|
57
|
+
stopSequences?: string[];
|
|
58
|
+
/** Whether to do sampling (false = greedy) */
|
|
59
|
+
doSample?: boolean;
|
|
60
|
+
/** Number of sequences to return */
|
|
61
|
+
numReturnSequences?: number;
|
|
62
|
+
/** Return full text (including prompt) */
|
|
63
|
+
returnFullText?: boolean;
|
|
64
|
+
/** Callback for each generated token */
|
|
65
|
+
onToken?: (token: string, tokenId: number) => void;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Chat generation options
|
|
69
|
+
*/
|
|
70
|
+
export interface ChatOptions extends TextGenerationOptions {
|
|
71
|
+
/** System prompt */
|
|
72
|
+
systemPrompt?: string;
|
|
73
|
+
/** Chat template type */
|
|
74
|
+
templateType?: ChatTemplateType;
|
|
75
|
+
/** Custom template (if templateType is 'custom') */
|
|
76
|
+
customTemplate?: {
|
|
77
|
+
systemPrefix?: string;
|
|
78
|
+
systemSuffix?: string;
|
|
79
|
+
userPrefix?: string;
|
|
80
|
+
userSuffix?: string;
|
|
81
|
+
assistantPrefix?: string;
|
|
82
|
+
assistantSuffix?: string;
|
|
83
|
+
separator?: string;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Text generation result
|
|
88
|
+
*/
|
|
89
|
+
export interface TextGenerationResult extends PipelineResult {
|
|
90
|
+
/** Generated text */
|
|
91
|
+
generatedText: string;
|
|
92
|
+
/** Full text (prompt + generated) if returnFullText is true */
|
|
93
|
+
fullText?: string;
|
|
94
|
+
/** Generated token IDs */
|
|
95
|
+
tokenIds: number[];
|
|
96
|
+
/** Number of tokens generated */
|
|
97
|
+
numTokens: number;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Streaming generation event
|
|
101
|
+
*/
|
|
102
|
+
export interface GenerationStreamEvent {
|
|
103
|
+
/** Current token */
|
|
104
|
+
token: string;
|
|
105
|
+
/** Token ID */
|
|
106
|
+
tokenId: number;
|
|
107
|
+
/** Generated text so far */
|
|
108
|
+
generatedText: string;
|
|
109
|
+
/** Whether generation is complete */
|
|
110
|
+
done: boolean;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* TextGenerationPipeline - Autoregressive text generation
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const generator = await pipeline('text-generation', 'Xenova/gpt2');
|
|
118
|
+
*
|
|
119
|
+
* // Simple generation
|
|
120
|
+
* const result = await generator.run('Once upon a time');
|
|
121
|
+
* console.log(result.generatedText);
|
|
122
|
+
*
|
|
123
|
+
* // Streaming generation
|
|
124
|
+
* for await (const event of generator.stream('Hello, ')) {
|
|
125
|
+
* process.stdout.write(event.token);
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export declare class TextGenerationPipeline extends BasePipeline<string | string[], TextGenerationResult | TextGenerationResult[]> {
|
|
130
|
+
private tokenizer;
|
|
131
|
+
private eosTokenId;
|
|
132
|
+
private llmModel;
|
|
133
|
+
private modelsLoaded;
|
|
134
|
+
private modelUrl;
|
|
135
|
+
private tokenizerUrl;
|
|
136
|
+
constructor(config?: PipelineConfig);
|
|
137
|
+
/**
|
|
138
|
+
* Check if model is loaded
|
|
139
|
+
*/
|
|
140
|
+
get isModelLoaded(): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Set custom model URLs
|
|
143
|
+
*/
|
|
144
|
+
setModelUrls(model: string, tokenizer: string): void;
|
|
145
|
+
/**
|
|
146
|
+
* Load model and tokenizer with progress callback
|
|
147
|
+
*/
|
|
148
|
+
loadModel(onProgress?: (progress: LLMLoadProgress) => void): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Fetch model with progress tracking
|
|
151
|
+
*/
|
|
152
|
+
private fetchModelWithProgress;
|
|
153
|
+
/**
|
|
154
|
+
* Initialize pipeline (override to skip default model loading)
|
|
155
|
+
*/
|
|
156
|
+
initialize(): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Set tokenizer
|
|
159
|
+
*/
|
|
160
|
+
setTokenizer(tokenizer: Tokenizer): void;
|
|
161
|
+
/**
|
|
162
|
+
* Preprocess - not used for text generation (handled in generateSingle)
|
|
163
|
+
*/
|
|
164
|
+
protected preprocess(input: string | string[]): Promise<EdgeFlowTensor[]>;
|
|
165
|
+
/**
|
|
166
|
+
* Postprocess - not used for text generation (handled in generateSingle)
|
|
167
|
+
*/
|
|
168
|
+
protected postprocess(_outputs: EdgeFlowTensor[], _options?: PipelineOptions): Promise<TextGenerationResult | TextGenerationResult[]>;
|
|
169
|
+
/**
|
|
170
|
+
* Generate text (non-streaming)
|
|
171
|
+
*/
|
|
172
|
+
run(prompt: string | string[], options?: PipelineOptions & TextGenerationOptions): Promise<TextGenerationResult | TextGenerationResult[]>;
|
|
173
|
+
/**
|
|
174
|
+
* Generate text with streaming (async generator)
|
|
175
|
+
*/
|
|
176
|
+
stream(prompt: string, options?: TextGenerationOptions): AsyncGenerator<GenerationStreamEvent>;
|
|
177
|
+
/**
|
|
178
|
+
* Generate a single sequence (non-streaming)
|
|
179
|
+
*/
|
|
180
|
+
private generateSingle;
|
|
181
|
+
/**
|
|
182
|
+
* Generate next token using the model
|
|
183
|
+
*/
|
|
184
|
+
private generateNextToken;
|
|
185
|
+
/**
|
|
186
|
+
* Greedy decoding (argmax)
|
|
187
|
+
*/
|
|
188
|
+
private greedy;
|
|
189
|
+
/**
|
|
190
|
+
* Sample from probability distribution with top-k/top-p filtering
|
|
191
|
+
*/
|
|
192
|
+
private sample;
|
|
193
|
+
private conversationHistory;
|
|
194
|
+
private chatTemplateType;
|
|
195
|
+
/**
|
|
196
|
+
* Set the chat template type
|
|
197
|
+
*/
|
|
198
|
+
setChatTemplate(templateType: ChatTemplateType): void;
|
|
199
|
+
/**
|
|
200
|
+
* Apply chat template to messages
|
|
201
|
+
*/
|
|
202
|
+
applyChatTemplate(messages: ChatMessage[], options?: ChatOptions): string;
|
|
203
|
+
/**
|
|
204
|
+
* ChatML template (used by many models including Qwen, Yi)
|
|
205
|
+
*/
|
|
206
|
+
private applyChatMLTemplate;
|
|
207
|
+
/**
|
|
208
|
+
* Llama 2 template
|
|
209
|
+
*/
|
|
210
|
+
private applyLlama2Template;
|
|
211
|
+
/**
|
|
212
|
+
* Llama 3 template
|
|
213
|
+
*/
|
|
214
|
+
private applyLlama3Template;
|
|
215
|
+
/**
|
|
216
|
+
* Mistral template
|
|
217
|
+
*/
|
|
218
|
+
private applyMistralTemplate;
|
|
219
|
+
/**
|
|
220
|
+
* Phi-3 template
|
|
221
|
+
*/
|
|
222
|
+
private applyPhi3Template;
|
|
223
|
+
/**
|
|
224
|
+
* Alpaca template
|
|
225
|
+
*/
|
|
226
|
+
private applyAlpacaTemplate;
|
|
227
|
+
/**
|
|
228
|
+
* Vicuna template
|
|
229
|
+
*/
|
|
230
|
+
private applyVicunaTemplate;
|
|
231
|
+
/**
|
|
232
|
+
* Custom template
|
|
233
|
+
*/
|
|
234
|
+
private applyCustomTemplate;
|
|
235
|
+
/**
|
|
236
|
+
* Chat with the model
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const generator = await pipeline('text-generation', 'model');
|
|
241
|
+
*
|
|
242
|
+
* // Single turn
|
|
243
|
+
* const response = await generator.chat('Hello, how are you?');
|
|
244
|
+
*
|
|
245
|
+
* // Multi-turn with history
|
|
246
|
+
* const response1 = await generator.chat('What is AI?');
|
|
247
|
+
* const response2 = await generator.chat('Can you give an example?');
|
|
248
|
+
*
|
|
249
|
+
* // With system prompt
|
|
250
|
+
* const response = await generator.chat('Hello', {
|
|
251
|
+
* systemPrompt: 'You are a helpful assistant.',
|
|
252
|
+
* });
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
chat(userMessage: string, options?: ChatOptions): Promise<TextGenerationResult>;
|
|
256
|
+
/**
|
|
257
|
+
* Stream chat response
|
|
258
|
+
*/
|
|
259
|
+
chatStream(userMessage: string, options?: ChatOptions): AsyncGenerator<GenerationStreamEvent>;
|
|
260
|
+
/**
|
|
261
|
+
* Get conversation history
|
|
262
|
+
*/
|
|
263
|
+
getConversationHistory(): ChatMessage[];
|
|
264
|
+
/**
|
|
265
|
+
* Set conversation history
|
|
266
|
+
*/
|
|
267
|
+
setConversationHistory(messages: ChatMessage[]): void;
|
|
268
|
+
/**
|
|
269
|
+
* Clear conversation history
|
|
270
|
+
*/
|
|
271
|
+
clearConversation(): void;
|
|
272
|
+
/**
|
|
273
|
+
* Remove last exchange (user message + assistant response)
|
|
274
|
+
*/
|
|
275
|
+
undoLastExchange(): void;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Create text generation pipeline
|
|
279
|
+
*/
|
|
280
|
+
export declare function createTextGenerationPipeline(config?: PipelineConfig): TextGenerationPipeline;
|
|
281
|
+
//# sourceMappingURL=text-generation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text-generation.d.ts","sourceRoot":"","sources":["../../src/pipelines/text-generation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAW,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,eAAe,EAAe,MAAM,kBAAkB,CAAC;AAgBhF;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,oCAAoC;IACpC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC;IAC7B,mBAAmB;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,kBAAkB;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,QAAQ,EAAE,MAAM,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