|
|
|
import { pipeline } from '@huggingface/transformers'; |
|
|
|
class MyZeroShotClassificationPipeline { |
|
static task = 'zero-shot-classification'; |
|
static instance = null; |
|
|
|
static async getInstance(model, progress_callback = null) { |
|
this.instance ??= pipeline(this.task, model, { |
|
progress_callback |
|
}); |
|
|
|
return this.instance; |
|
} |
|
} |
|
|
|
|
|
self.addEventListener('message', async (event) => { |
|
const { text, labels, model } = event.data; |
|
if (!model) { |
|
self.postMessage({ |
|
status: 'error', |
|
output: 'No model provided' |
|
}); |
|
return; |
|
} |
|
|
|
|
|
|
|
const classifier = await MyZeroShotClassificationPipeline.getInstance(model, (x) => { |
|
|
|
|
|
self.postMessage({ status: 'progress', output: x }); |
|
}); |
|
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' }); |
|
}); |
|
|