|
|
|
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; |
|
} |
|
} |
|
|
|
|
|
self.addEventListener("message", async (event) => { |
|
const { task, model, input } = event.data; |
|
|
|
PipelineFactory.task = task; |
|
PipelineFactory.model = model; |
|
|
|
|
|
|
|
const pipe = await PipelineFactory.getInstance((x) => { |
|
|
|
|
|
self.postMessage(x); |
|
}); |
|
|
|
|
|
const output = await pipe(...input); |
|
|
|
|
|
self.postMessage({ status: "output", output }); |
|
|
|
|
|
self.postMessage({ status: "complete" }); |
|
}); |
|
|