File size: 1,791 Bytes
356ec31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e45b97b
492c60e
356ec31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a17bf5b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * Worker script for zero-shot classification.
 * Loads the pipeline and handles classification requests.
 */
import { env, pipeline } from '@huggingface/transformers';

// Skip local model check since we are downloading the model from the Hugging Face Hub.
env.allowLocalModels = false;

/**
 * Class for zero-shot classification.
 * Loads the pipeline and handles classification requests.
 */
class MyZeroShotClassificationPipeline {
  // Task and model for zero-shot classification.
  static task = 'zero-shot-classification';
  static model = 'MoritzLaurer/ModernBERT-large-zeroshot-v2.0';
  static instance = null;

  // Get the pipeline instance.
  static async getInstance(progress_callback = null) {
    if (this.instance === null) {
      this.instance = pipeline(this.task, this.model, {
        dtype: "fp16",
        device: "webgpu",
        progress_callback,
      });
    }

    return this.instance;
  }
}

// Listen for messages from the main thread
self.addEventListener('message', async (event) => {
  // Retrieve the pipeline. When called for the first time,
  // this will load the pipeline and save it for future use.
  const classifier = await MyZeroShotClassificationPipeline.getInstance(x => {
    // We also add a progress callback to the pipeline so that we can
    // track model loading.
    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,
    });
    // Send the output back to the main thread
    self.postMessage({ status: 'output', output });
  }
  // Send the output back to the main thread
  self.postMessage({ status: 'complete' });
});