mirror-jetstream/transformersjs/task-bert.js
Camillo Bruni 8270d839bf Reset main-custom to latest main version
Following a different approach to better keep main-custom up to date
with main by resetting and merging pending upstream PRs on top.

Change-Id: I26c7a2c63b166715d01b50da0bd47aaf99aa3c1a
Reviewed-on: https://chromium-review.googlesource.com/c/external/github.com/WebKit/JetStream/+/6976606
Reviewed-by: Victor Gomes <victorgomes@chromium.org>
2025-09-24 01:18:07 -07:00

35 lines
1.1 KiB
JavaScript

// 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]);
}
}