Switching to processes and whisper.cpp to avoid transformers.js memory leaks
All checks were successful
Publish Library / Build NPM Project (push) Successful in 38s
Publish Library / Tag Version (push) Successful in 5s

This commit is contained in:
2026-02-20 21:50:01 -05:00
parent 790608f020
commit 39537a4a8f
9 changed files with 215 additions and 262 deletions

View File

@@ -1,11 +1,13 @@
import { pipeline } from '@xenova/transformers';
import { parentPort } from 'worker_threads';
let embedder: any;
const [modelDir, model] = process.argv.slice(2);
parentPort?.on('message', async ({text, model, modelDir }) => {
if(!embedder) embedder = await pipeline('feature-extraction', 'Xenova/' + model, {quantized: true, cache_dir: modelDir});
let text = '';
process.stdin.on('data', chunk => text += chunk);
process.stdin.on('end', async () => {
const embedder = await pipeline('feature-extraction', 'Xenova/' + model, {quantized: true, cache_dir: modelDir});
const output = await embedder(text, { pooling: 'mean', normalize: true });
const embedding = Array.from(output.data);
parentPort?.postMessage({embedding});
console.log(JSON.stringify({embedding}));
process.exit();
});