diff --git a/src/asr.ts b/src/asr.ts index b544214..73ab060 100644 --- a/src/asr.ts +++ b/src/asr.ts @@ -9,15 +9,20 @@ import wavefile from 'wavefile'; let whisperPipeline: any; -export async function canDiarization(): Promise { - return new Promise((resolve) => { - const proc = spawn('python', ['-c', 'import pyannote.audio']); - proc.on('close', (code: number) => resolve(code === 0)); - proc.on('error', () => resolve(false)); - }); +export async function canDiarization(): Promise { + const checkPython = (cmd: string) => { + return new Promise((resolve) => { + const proc = spawn(cmd, ['-c', 'import pyannote.audio']); + proc.on('close', (code: number) => resolve(code === 0)); + proc.on('error', () => resolve(false)); + }); + }; + if(await checkPython('python3')) return 'python3'; + if(await checkPython('python')) return 'python'; + return null; } -async function runDiarization(audioPath: string, dir: string, token: string): Promise { +async function runDiarization(binary: string, audioPath: string, dir: string, token: string): Promise { const script = ` import sys import json @@ -37,7 +42,7 @@ print(json.dumps(segments)) return new Promise((resolve, reject) => { let output = ''; - const proc = spawn('python', ['-c', script, audioPath]); + const proc = spawn(binary, ['-c', script, audioPath]); proc.stdout.on('data', (data: Buffer) => output += data.toString()); proc.stderr.on('data', (data: Buffer) => console.error(data.toString())); proc.on('close', (code: number) => { @@ -112,10 +117,10 @@ parentPort?.on('message', async ({ file, speaker, model, modelDir, token }) => { const [f, buffer] = prepareAudioBuffer(file); // Fetch transcript and speakers - const hasDiarization = speaker && await canDiarization(); + const hasDiarization = await canDiarization(); const [transcript, speakers] = await Promise.all([ whisperPipeline(buffer, {return_timestamps: speaker ? 'word' : false}), - (!speaker || !token || !hasDiarization) ? Promise.resolve(): runDiarization(f, modelDir, token), + (!speaker || !token || !hasDiarization) ? Promise.resolve(): runDiarization(hasDiarization, f, modelDir, token), ]); if(file != f) rmSync(f, { recursive: true, force: true }); diff --git a/src/audio.ts b/src/audio.ts index c227242..2621d08 100644 --- a/src/audio.ts +++ b/src/audio.ts @@ -56,5 +56,5 @@ export class Audio { return Object.assign(p, { abort }); } - canDiarization = canDiarization; + canDiarization = () => canDiarization().then(resp => !!resp); }