File size: 1,378 Bytes
f5d25f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f43196f
f5d25f5
f43196f
f5d25f5
 
f43196f
f5d25f5
 
 
 
 
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
import { AllTasks, pipeline, PipelineType } from "@huggingface/transformers";

// Use the Singleton pattern to enable lazy construction of the pipeline.
// NOTE: We wrap the class in a function to prevent code duplication (see below).
class PipelineSingletonClass {
  static task = 'text-classification';
  static model = 'SamLowe/roberta-base-go_emotions-onnx';
  static instance: Promise<AllTasks[PipelineType]> | null = null;

  static async getInstance(progress_callback = undefined) {
    if (this.instance === null) {
      this.instance = pipeline(this.task as PipelineType, this.model, { progress_callback });
    }
    return this.instance;
  }
}
const P = () => PipelineSingletonClass

let PipelineSingleton: typeof PipelineSingletonClass;
if (process.env.NODE_ENV !== 'production') {
  // When running in development mode, attach the pipeline to the
  // global object so that it's preserved between hot reloads.
  // For more information, see https://vercel.com/guides/nextjs-prisma-postgres
  //@ts-expect-error I don't know how to define globalThis
  if (!global.PipelineSingleton) {
    //@ts-expect-error I don't know how to define globalThis
    global.PipelineSingleton = P();
  }
  //@ts-expect-error I don't know how to define globalThis
  PipelineSingleton = global.PipelineSingleton;
} else {
  PipelineSingleton = P();
}
export default PipelineSingleton;