File size: 756 Bytes
8b95140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { pipeline, env } from "@xenova/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).
process.env.HF_ACCESS_TOKEN = process.env.NEXT_PUBLIC_APP_HF_TOKEN

const P = () => class PipelineSingleton {
    static task = 'image-classification';
    static model = 'enzostvs/nsfw-detector-transformersjs';
    static instance = null;

    static async getInstance(progress_callback: any = null) {
        if (this.instance === null) {
            this.instance = pipeline(this.task, this.model, { progress_callback }) as any;
        }
        return this.instance;
    }
}

const PipelineSingleton = P();
export default PipelineSingleton;