-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtask-bert.js
More file actions
35 lines (31 loc) · 1.13 KB
/
task-bert.js
File metadata and controls
35 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2025 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// End-to-end task 1: Sentiment analysis, so NLP.
globalThis.initPipeline = async function(pipeline) {
return await pipeline(
'sentiment-analysis',
'Xenova/distilbert-base-uncased-finetuned-sst-2-english',
// Use quantized models for smaller model weights.
{ dtype: 'uint8' }
);
}
globalThis.doTask = async function(pipeline) {
const inputs = [
'I love transformers!',
'Benchmarking is hard.',
];
const outputs = await pipeline(inputs);
return outputs;
}
globalThis.validate = function(outputs) {
if (outputs.length !== 2) {
throw new Error('Expected output to be an array matching the inputs, but got:' + outputs);
}
if (outputs[0].label !== 'POSITIVE' || outputs[0].score < 0.9) {
throw new Error('Expected positive sentiment for first input, but got: ' + outputs[0]);
}
if (outputs[1].label !== 'NEGATIVE' || outputs[1].score < 0.9) {
throw new Error('Expected negative sentiment for second input, but got: ' + outputs[1]);
}
}