diff --git a/src/transformers.mjs b/src/transformers.mjs index e0465f30a..87f0f49c0 100644 --- a/src/transformers.mjs +++ b/src/transformers.mjs @@ -94,7 +94,11 @@ function getModelForTask(task) { */ async function getPipeline(task, forceModel = '') { if (tasks[task].pipeline) { - return tasks[task].pipeline; + if (forceModel === '' || tasks[task].currentModel === forceModel) { + return tasks[task].pipeline; + } + console.log('Disposing transformers.js pipeline for for task', task, 'with model', tasks[task].currentModel); + await tasks[task].pipeline.dispose(); } const cache_dir = path.join(process.cwd(), 'cache'); @@ -103,6 +107,7 @@ async function getPipeline(task, forceModel = '') { console.log('Initializing transformers.js pipeline for task', task, 'with model', model); const instance = await pipeline(task, model, { cache_dir, quantized: tasks[task].quantized ?? true, local_files_only: localOnly }); tasks[task].pipeline = instance; + tasks[task].currentModel = model; return instance; }