File size: 1,212 Bytes
948b11c
9283c8b
948b11c
85a4687
 
 
948b11c
 
 
85a4687
 
9283c8b
85a4687
 
948b11c
 
 
 
 
 
9283c8b
85a4687
 
 
 
 
948b11c
 
85a4687
948b11c
 
9283c8b
948b11c
 
85a4687
 
 
 
9283c8b
948b11c
 
9283c8b
85a4687
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
/* eslint-disable no-restricted-globals */
import { pipeline } from '@huggingface/transformers';

class PipelineFactory {
  static task = null;
  static model = null;
  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) => {
  const { task, model, input } = event.data;

  PipelineFactory.task = task;
  PipelineFactory.model = model;

  // Retrieve the pipeline. When called for the first time,
  // this will load the pipeline and save it for future use.
  const pipe = await PipelineFactory.getInstance((x) => {
    // We also add a progress callback to the pipeline so that we can
    // track model loading.
    self.postMessage({ status: 'progress', data: x });
  });

  // Run the pipeline
  const output = await pipe(...input);

  // 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' });
});