@fgv/ts-extras-transformers 5.1.0-29
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/.rush/temp/cad001c0dd0af20627d8849ed0e5fa4904160f67.tar.log +30 -0
- package/.rush/temp/chunked-rush-logs/ts-extras-transformers.build.chunks.jsonl +9 -0
- package/.rush/temp/chunked-rush-logs/ts-extras-transformers.test.chunks.jsonl +28 -0
- package/.rush/temp/operation/build/all.log +9 -0
- package/.rush/temp/operation/build/log-chunks.jsonl +9 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/operation/test/all.log +28 -0
- package/.rush/temp/operation/test/log-chunks.jsonl +28 -0
- package/.rush/temp/operation/test/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +742 -0
- package/CHANGELOG.json +4 -0
- package/README.md +121 -0
- package/config/api-extractor.json +38 -0
- package/config/jest.config.json +13 -0
- package/config/rig.json +6 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/dist/test/unit/transformers.test.js +197 -0
- package/dist/test/unit/transformers.test.js.map +1 -0
- package/dist/ts-extras-transformers.d.ts +137 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/eslint.config.js +15 -0
- package/etc/ts-extras-transformers.api.md +43 -0
- package/lib/index.d.ts +112 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +143 -0
- package/lib/index.js.map +1 -0
- package/lib/test/unit/transformers.test.d.ts +2 -0
- package/lib/test/unit/transformers.test.d.ts.map +1 -0
- package/lib/test/unit/transformers.test.js +232 -0
- package/lib/test/unit/transformers.test.js.map +1 -0
- package/package.json +80 -0
- package/rush-logs/ts-extras-transformers.build.cache.log +3 -0
- package/rush-logs/ts-extras-transformers.build.log +9 -0
- package/rush-logs/ts-extras-transformers.test.cache.log +1 -0
- package/rush-logs/ts-extras-transformers.test.log +28 -0
- package/src/index.ts +179 -0
- package/src/test/unit/transformers.test.ts +267 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +14 -0
- package/temp/build/typescript/ts_8nwakTlr.json +1 -0
- package/temp/coverage/base.css +224 -0
- package/temp/coverage/block-navigation.js +87 -0
- package/temp/coverage/favicon.png +0 -0
- package/temp/coverage/index.html +116 -0
- package/temp/coverage/index.ts.html +622 -0
- package/temp/coverage/lcov-report/base.css +224 -0
- package/temp/coverage/lcov-report/block-navigation.js +87 -0
- package/temp/coverage/lcov-report/favicon.png +0 -0
- package/temp/coverage/lcov-report/index.html +116 -0
- package/temp/coverage/lcov-report/index.ts.html +622 -0
- package/temp/coverage/lcov-report/prettify.css +1 -0
- package/temp/coverage/lcov-report/prettify.js +2 -0
- package/temp/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/temp/coverage/lcov-report/sorter.js +210 -0
- package/temp/coverage/lcov.info +209 -0
- package/temp/coverage/prettify.css +1 -0
- package/temp/coverage/prettify.js +2 -0
- package/temp/coverage/sort-arrow-sprite.png +0 -0
- package/temp/coverage/sorter.js +210 -0
- package/temp/test/jest/haste-map-502913d9c10083bec2207119c45d7122-cd1aa4a0322f87b67f395127aa5e94ac-091f69b131db2c080ae4b15b7b2d4a16 +0 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/8d/package_8dec8c7ec8d6c787063a4a014e769a65 +53 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/d5/package_d56b0a8a49dbfccad646fc2e21950522 +203 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/fc/versions_fc561f31b0dfb504443c5b8531747bfb +31 -0
- package/temp/test/jest/perf-cache-502913d9c10083bec2207119c45d7122-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
- package/temp/ts-extras-transformers.api.json +675 -0
- package/temp/ts-extras-transformers.api.md +43 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
jest.mock('@huggingface/transformers');
|
|
2
|
+
|
|
3
|
+
import '@fgv/ts-utils-jest';
|
|
4
|
+
import * as upstream from '@huggingface/transformers';
|
|
5
|
+
import {
|
|
6
|
+
loadPipeline,
|
|
7
|
+
classify,
|
|
8
|
+
classifyAll,
|
|
9
|
+
embed,
|
|
10
|
+
type TextClassificationPipeline,
|
|
11
|
+
type TextClassificationOutput,
|
|
12
|
+
type FeatureExtractionPipeline,
|
|
13
|
+
type Tensor,
|
|
14
|
+
type AllTasks
|
|
15
|
+
} from '../../index';
|
|
16
|
+
|
|
17
|
+
// ─── loadPipeline ──────────────────────────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
describe('loadPipeline', () => {
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
jest.resetAllMocks();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('returns Success wrapping the upstream pipeline on success', async () => {
|
|
25
|
+
const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];
|
|
26
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
27
|
+
|
|
28
|
+
const result = await loadPipeline(
|
|
29
|
+
'text-classification',
|
|
30
|
+
'Xenova/distilbert-base-uncased-finetuned-sst-2-english'
|
|
31
|
+
);
|
|
32
|
+
expect(result).toSucceedWith(mockPipeline);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('passes task and model to the upstream pipeline factory', async () => {
|
|
36
|
+
const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];
|
|
37
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
38
|
+
|
|
39
|
+
await loadPipeline('text-classification', 'some-model-id');
|
|
40
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', undefined);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('passes options through to the upstream pipeline factory', async () => {
|
|
44
|
+
const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];
|
|
45
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
46
|
+
|
|
47
|
+
const opts = { device: 'cpu' } as Parameters<typeof upstream.pipeline>[2];
|
|
48
|
+
await loadPipeline('text-classification', 'some-model-id', opts);
|
|
49
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', opts);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('returns Success when model is omitted', async () => {
|
|
53
|
+
const mockPipeline = jest.fn() as unknown as AllTasks['feature-extraction'];
|
|
54
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
55
|
+
|
|
56
|
+
const result = await loadPipeline('feature-extraction');
|
|
57
|
+
expect(result).toSucceed();
|
|
58
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('feature-extraction', undefined, undefined);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('returns Failure capturing upstream error message on network failure', async () => {
|
|
62
|
+
jest
|
|
63
|
+
.mocked(upstream.pipeline)
|
|
64
|
+
.mockRejectedValueOnce(new Error('Could not locate the model: model-not-found'));
|
|
65
|
+
|
|
66
|
+
const result = await loadPipeline('text-classification', 'model-not-found');
|
|
67
|
+
expect(result).toFailWith(/could not locate the model/i);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('returns Failure capturing upstream error on ONNX init failure', async () => {
|
|
71
|
+
jest.mocked(upstream.pipeline).mockRejectedValueOnce(new Error('Failed to initialize ONNX runtime'));
|
|
72
|
+
|
|
73
|
+
const result = await loadPipeline('text-classification', 'some-model');
|
|
74
|
+
expect(result).toFailWith(/failed to initialize onnx runtime/i);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// ─── classify ─────────────────────────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
describe('classify', () => {
|
|
81
|
+
let mockClassifier: jest.MockedFunction<TextClassificationPipeline>;
|
|
82
|
+
|
|
83
|
+
beforeEach(() => {
|
|
84
|
+
jest.resetAllMocks();
|
|
85
|
+
mockClassifier = jest.fn() as unknown as jest.MockedFunction<TextClassificationPipeline>;
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('returns Success wrapping TextClassificationOutput on success', async () => {
|
|
89
|
+
const mockOutput: TextClassificationOutput = [{ label: 'POSITIVE', score: 0.9998 }];
|
|
90
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
91
|
+
|
|
92
|
+
const result = await classify(mockClassifier, 'I love transformers!');
|
|
93
|
+
expect(result).toSucceedWith(mockOutput);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('passes text to the upstream classifier', async () => {
|
|
97
|
+
const mockOutput: TextClassificationOutput = [{ label: 'NEGATIVE', score: 0.9997 }];
|
|
98
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
99
|
+
|
|
100
|
+
await classify(mockClassifier, 'I hate bugs');
|
|
101
|
+
expect(mockClassifier).toHaveBeenCalledWith('I hate bugs', undefined);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('passes options through to the upstream classifier', async () => {
|
|
105
|
+
const mockOutput: TextClassificationOutput = [
|
|
106
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
107
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
108
|
+
];
|
|
109
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
110
|
+
|
|
111
|
+
const opts = { top_k: null };
|
|
112
|
+
await classify(mockClassifier, 'Hello world', opts);
|
|
113
|
+
expect(mockClassifier).toHaveBeenCalledWith('Hello world', opts);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('normalises flat array result correctly', async () => {
|
|
117
|
+
const mockOutput: TextClassificationOutput = [
|
|
118
|
+
{ label: 'SAFE', score: 0.95 },
|
|
119
|
+
{ label: 'UNSAFE', score: 0.05 }
|
|
120
|
+
];
|
|
121
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
122
|
+
|
|
123
|
+
expect(await classify(mockClassifier, 'hello')).toSucceedWith(mockOutput);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('normalises nested array result by flattening one level', async () => {
|
|
127
|
+
// When upstream returns array-of-arrays (e.g. batch input path leaked through),
|
|
128
|
+
// we flatten one level so consumers always receive a flat TextClassificationOutput.
|
|
129
|
+
const inner: TextClassificationOutput = [{ label: 'POSITIVE', score: 0.9 }];
|
|
130
|
+
mockClassifier.mockResolvedValueOnce([inner] as unknown as TextClassificationOutput);
|
|
131
|
+
|
|
132
|
+
expect(await classify(mockClassifier, 'hello')).toSucceedAndSatisfy((output) => {
|
|
133
|
+
expect(output).toEqual([{ label: 'POSITIVE', score: 0.9 }]);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('returns Failure capturing upstream error on inference failure', async () => {
|
|
138
|
+
mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
139
|
+
|
|
140
|
+
const result = await classify(mockClassifier, 'some text');
|
|
141
|
+
expect(result).toFailWith(/inference session error/i);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('returns Failure capturing upstream tokenisation error', async () => {
|
|
145
|
+
mockClassifier.mockRejectedValueOnce(new Error('Tokenisation failed: unexpected token'));
|
|
146
|
+
|
|
147
|
+
const result = await classify(mockClassifier, 'bad input');
|
|
148
|
+
expect(result).toFailWith(/tokenisation failed/i);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ─── classifyAll ──────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
describe('classifyAll', () => {
|
|
155
|
+
let mockClassifier: jest.MockedFunction<TextClassificationPipeline>;
|
|
156
|
+
|
|
157
|
+
beforeEach(() => {
|
|
158
|
+
jest.resetAllMocks();
|
|
159
|
+
mockClassifier = jest.fn() as unknown as jest.MockedFunction<TextClassificationPipeline>;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('forces top_k: null on the underlying classify call', async () => {
|
|
163
|
+
const mockOutput: TextClassificationOutput = [
|
|
164
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
165
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
166
|
+
];
|
|
167
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
168
|
+
|
|
169
|
+
await classifyAll(mockClassifier, 'hello');
|
|
170
|
+
expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('overrides caller-supplied top_k with null', async () => {
|
|
174
|
+
const mockOutput: TextClassificationOutput = [
|
|
175
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
176
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
177
|
+
];
|
|
178
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
179
|
+
|
|
180
|
+
await classifyAll(mockClassifier, 'hello', { top_k: 1 });
|
|
181
|
+
expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('returns all labels on success', async () => {
|
|
185
|
+
const mockOutput: TextClassificationOutput = [
|
|
186
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
187
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
188
|
+
];
|
|
189
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
190
|
+
|
|
191
|
+
const result = await classifyAll(mockClassifier, 'I love transformers!');
|
|
192
|
+
expect(result).toSucceedWith(mockOutput);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('propagates upstream inference failure as Failure', async () => {
|
|
196
|
+
mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
197
|
+
|
|
198
|
+
const result = await classifyAll(mockClassifier, 'some text');
|
|
199
|
+
expect(result).toFailWith(/inference session error/i);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// ─── embed ────────────────────────────────────────────────────────────────────
|
|
204
|
+
|
|
205
|
+
describe('embed', () => {
|
|
206
|
+
let mockExtractor: jest.MockedFunction<FeatureExtractionPipeline>;
|
|
207
|
+
|
|
208
|
+
beforeEach(() => {
|
|
209
|
+
jest.resetAllMocks();
|
|
210
|
+
mockExtractor = jest.fn() as unknown as jest.MockedFunction<FeatureExtractionPipeline>;
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('returns Success wrapping the upstream Tensor on success', async () => {
|
|
214
|
+
const mockTensor = {
|
|
215
|
+
type: 'float32',
|
|
216
|
+
data: new Float32Array([0.1, 0.2, 0.3]),
|
|
217
|
+
dims: [1, 3]
|
|
218
|
+
} as unknown as Tensor;
|
|
219
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
220
|
+
|
|
221
|
+
const result = await embed(mockExtractor, 'This is a test.');
|
|
222
|
+
expect(result).toSucceedWith(mockTensor);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test('passes text to the upstream extractor', async () => {
|
|
226
|
+
const mockTensor = { type: 'float32', data: new Float32Array([0.1]), dims: [1, 1] } as unknown as Tensor;
|
|
227
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
228
|
+
|
|
229
|
+
await embed(mockExtractor, 'hello world');
|
|
230
|
+
expect(mockExtractor).toHaveBeenCalledWith('hello world', undefined);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('passes string array to the upstream extractor', async () => {
|
|
234
|
+
const mockTensor = {
|
|
235
|
+
type: 'float32',
|
|
236
|
+
data: new Float32Array([0.1, 0.2]),
|
|
237
|
+
dims: [2, 1]
|
|
238
|
+
} as unknown as Tensor;
|
|
239
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
240
|
+
|
|
241
|
+
await embed(mockExtractor, ['text one', 'text two']);
|
|
242
|
+
expect(mockExtractor).toHaveBeenCalledWith(['text one', 'text two'], undefined);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('passes options through to the upstream extractor', async () => {
|
|
246
|
+
const mockTensor = { type: 'float32', data: new Float32Array([0.5]), dims: [1, 1] } as unknown as Tensor;
|
|
247
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
248
|
+
|
|
249
|
+
const opts = { pooling: 'mean' as const, normalize: true };
|
|
250
|
+
await embed(mockExtractor, 'some text', opts);
|
|
251
|
+
expect(mockExtractor).toHaveBeenCalledWith('some text', opts);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('returns Failure capturing upstream inference error', async () => {
|
|
255
|
+
mockExtractor.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
256
|
+
|
|
257
|
+
const result = await embed(mockExtractor, 'some text');
|
|
258
|
+
expect(result).toFailWith(/inference session error/i);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test('returns Failure capturing upstream tokenisation error', async () => {
|
|
262
|
+
mockExtractor.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));
|
|
263
|
+
|
|
264
|
+
const result = await embed(mockExtractor, 'bad input');
|
|
265
|
+
expect(result).toFailWith(/tokenisation failed/i);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cacheVersion": "9.39.4_v22.22.1",
|
|
3
|
+
"fileVersions": [
|
|
4
|
+
[
|
|
5
|
+
"index.ts",
|
|
6
|
+
"ed0f9c9a76f16a0ff991efc3e5d4826029c3c1d13585a9769d0881b3ed486427_/jLBpLdBcIxlS7jmXPHwb/OLvs4="
|
|
7
|
+
],
|
|
8
|
+
[
|
|
9
|
+
"test/unit/transformers.test.ts",
|
|
10
|
+
"88dc7fd67d94205d56d25e713f4c1bead86f5a6ed0730a8ea43dd74a76e55152_98F1qYg8BszfZSdLXl13FuRYZ5g="
|
|
11
|
+
]
|
|
12
|
+
],
|
|
13
|
+
"filesHash": "uphP2Dqw6z0yoh1kFo4f9Q"
|
|
14
|
+
}
|