|
|
|
import { pipeline } from "@huggingface/transformers"; |
|
|
|
class MyTextClassificationPipeline { |
|
static task = "sentiment-analysis"; |
|
static model = 'Xenova/bert-base-multilingual-uncased-sentiment'; |
|
static instance = null; |
|
|
|
static async getInstance(progress_callback = null) { |
|
this.instance ??= pipeline(this.task, this.model, { |
|
progress_callback, |
|
}); |
|
|
|
return this.instance; |
|
} |
|
} |
|
|
|
|
|
self.addEventListener("message", async (event) => { |
|
|
|
|
|
const classifier = await MyTextClassificationPipeline.getInstance((x) => { |
|
|
|
|
|
self.postMessage(x); |
|
}); |
|
|
|
const { text } = event.data; |
|
|
|
const split = text.split("\n"); |
|
for (const line of split) { |
|
if (line.trim()) { |
|
const output = await classifier(line); |
|
|
|
self.postMessage({ |
|
status: "output", |
|
output: { |
|
sequence: line, |
|
label: output[0].label, |
|
score: output[0].score |
|
} |
|
}); |
|
} |
|
} |
|
|
|
self.postMessage({ status: "complete" }); |
|
}); |
|
|