File size: 1,056 Bytes
7a52bc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { pipeline } 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).
const P = () =>
	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;
		}
	};

let PipelineSingleton;
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
	if (!global.PipelineSingleton) {
		global.PipelineSingleton = P();
	}
	PipelineSingleton = global.PipelineSingleton;
} else {
	PipelineSingleton = P();
}
export default PipelineSingleton;