From 473424ae23ccd1b6de6b6bdb1ad8388c324c0a13 Mon Sep 17 00:00:00 2001 From: ztimson Date: Fri, 20 Feb 2026 17:31:49 -0500 Subject: [PATCH] segfault fix --- package.json | 2 +- src/asr.ts | 17 ++++++----------- src/audio.ts | 4 ++-- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index a5aaa76..dd3c3dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "0.7.5", + "version": "0.7.6", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/asr.ts b/src/asr.ts index 73ab060..fea7e15 100644 --- a/src/asr.ts +++ b/src/asr.ts @@ -7,8 +7,6 @@ import { join } from 'node:path'; import { tmpdir } from 'node:os'; import wavefile from 'wavefile'; -let whisperPipeline: any; - export async function canDiarization(): Promise { const checkPython = (cmd: string) => { return new Promise((resolve) => { @@ -110,30 +108,27 @@ function prepareAudioBuffer(file: string): [string, Float32Array] { } parentPort?.on('message', async ({ file, speaker, model, modelDir, token }) => { + let tempFile = null; try { - if(!whisperPipeline) whisperPipeline = await pipeline('automatic-speech-recognition', `Xenova/${model}`, {cache_dir: modelDir, quantized: true}); - - // Prepare audio file + const asr: any = await pipeline('automatic-speech-recognition', `Xenova/${model}`, {cache_dir: modelDir, quantized: true}); const [f, buffer] = prepareAudioBuffer(file); - - // Fetch transcript and speakers + tempFile = f !== file ? f : null; const hasDiarization = await canDiarization(); const [transcript, speakers] = await Promise.all([ - whisperPipeline(buffer, {return_timestamps: speaker ? 'word' : false}), + asr(buffer, {return_timestamps: speaker ? 'word' : false}), (!speaker || !token || !hasDiarization) ? Promise.resolve(): runDiarization(hasDiarization, f, modelDir, token), ]); - if(file != f) rmSync(f, { recursive: true, force: true }); - // Return any results / errors if no more processing required const text = transcript.text?.trim() || null; if(!speaker) return parentPort?.postMessage({ text }); if(!token) return parentPort?.postMessage({ text, error: 'HuggingFace token required' }); if(!hasDiarization) return parentPort?.postMessage({ text, error: 'Speaker diarization unavailable' }); - // Combine transcript and speakers const combined = combineSpeakerTranscript(transcript.chunks || [], speakers || []); parentPort?.postMessage({ text: combined }); } catch (err: any) { parentPort?.postMessage({ error: err.stack || err.message }); + } finally { + if(tempFile) rmSync(tempFile, { recursive: true, force: true }); } }); diff --git a/src/audio.ts b/src/audio.ts index 3d05cad..2474b45 100644 --- a/src/audio.ts +++ b/src/audio.ts @@ -15,7 +15,7 @@ export class Audio { let p = new Promise((resolve, reject) => { const worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'asr.js')); const handleMessage = ({ text, warning, error }: any) => { - worker.terminate(); + setTimeout(() => worker.terminate(), 1000); if(aborted) return; if(error) reject(new Error(error)); else { @@ -24,7 +24,7 @@ export class Audio { } }; const handleError = (err: Error) => { - worker.terminate(); + setTimeout(() => worker.terminate(), 1000); if(!aborted) reject(err); }; worker.on('message', handleMessage);