|
|
|
import { pipeline } from "@huggingface/transformers"; |
|
|
|
class MyZeroShotClassificationPipeline { |
|
static task = "zero-shot-classification"; |
|
static model = "MoritzLaurer/deberta-v3-xsmall-zeroshot-v1.1-all-33"; |
|
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 MyZeroShotClassificationPipeline.getInstance((x) => { |
|
|
|
|
|
self.postMessage(x); |
|
}); |
|
|
|
const { text, labels } = event.data; |
|
|
|
const split = text.split("\n"); |
|
for (const line of split) { |
|
const output = await classifier(line, labels, { |
|
hypothesis_template: "This text is about {}.", |
|
multi_label: true, |
|
}); |
|
|
|
self.postMessage({ status: "output", output }); |
|
} |
|
|
|
self.postMessage({ status: "complete" }); |
|
}); |