Use either python or python3 or diarization
This commit is contained in:
19
src/asr.ts
19
src/asr.ts
@@ -9,15 +9,20 @@ import wavefile from 'wavefile';
|
||||
|
||||
let whisperPipeline: any;
|
||||
|
||||
export async function canDiarization(): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
const proc = spawn('python', ['-c', 'import pyannote.audio']);
|
||||
export async function canDiarization(): Promise<string | null> {
|
||||
const checkPython = (cmd: string) => {
|
||||
return new Promise<boolean>((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<any[]> {
|
||||
async function runDiarization(binary: string, audioPath: string, dir: string, token: string): Promise<any[]> {
|
||||
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 });
|
||||
|
||||
|
||||
@@ -56,5 +56,5 @@ export class Audio {
|
||||
return Object.assign(p, { abort });
|
||||
}
|
||||
|
||||
canDiarization = canDiarization;
|
||||
canDiarization = () => canDiarization().then(resp => !!resp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user