Spaces:
Running
Running
Download src/app/worker.js from yaach/hf-sentiment: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/yaach/hf-sentiment/resolve/main/src/app/worker.js
- Command line
-
hf download hf://spaces/yaach/hf-sentiment/src/app/worker.js
-
curl -L -o worker.js https://huggingface.co/spaces/yaach/hf-sentiment/resolve/main/src/app/worker.js
1.26 kB
| import { pipeline, env } from "@xenova/transformers"; | |
| // Skip local model check | |
| env.allowLocalModels = false; | |
| // Use the Singleton pattern to enable lazy construction of the pipeline. | |
| class PipelineSingleton { | |
| static task = 'text-classification'; | |
| static model = 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'; | |
| static instance = null; | |
| static async getInstance(progress_callback = null) { | |
| if (this.instance === null) { | |
| this.instance = pipeline(this.task, this.model, { progress_callback }); | |
| } | |
| return this.instance; | |
| } | |
| } | |
| // Listen for messages from the main thread | |
| self.addEventListener('message', async (event) => { | |
| // Retrieve the classification pipeline. When called for the first time, | |
| // this will load the pipeline and save it for future use. | |
| let classifier = await PipelineSingleton.getInstance(x => { | |
| // We also add a progress callback to the pipeline so that we can | |
| // track model loading. | |
| self.postMessage(x); | |
| }); | |
| // Actually perform the classification | |
| let output = await classifier(event.data.text); | |
| // Send the output back to the main thread | |
| self.postMessage({ | |
| status: 'complete', | |
| output: output, | |
| }); | |
| }); | |