Use either python or python3 or diarization
This commit is contained in:
25
src/asr.ts
25
src/asr.ts
@@ -9,15 +9,20 @@ import wavefile from 'wavefile';
|
|||||||
|
|
||||||
let whisperPipeline: any;
|
let whisperPipeline: any;
|
||||||
|
|
||||||
export async function canDiarization(): Promise<boolean> {
|
export async function canDiarization(): Promise<string | null> {
|
||||||
return new Promise((resolve) => {
|
const checkPython = (cmd: string) => {
|
||||||
const proc = spawn('python', ['-c', 'import pyannote.audio']);
|
return new Promise<boolean>((resolve) => {
|
||||||
proc.on('close', (code: number) => resolve(code === 0));
|
const proc = spawn(cmd, ['-c', 'import pyannote.audio']);
|
||||||
proc.on('error', () => resolve(false));
|
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 = `
|
const script = `
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
@@ -37,7 +42,7 @@ print(json.dumps(segments))
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let output = '';
|
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.stdout.on('data', (data: Buffer) => output += data.toString());
|
||||||
proc.stderr.on('data', (data: Buffer) => console.error(data.toString()));
|
proc.stderr.on('data', (data: Buffer) => console.error(data.toString()));
|
||||||
proc.on('close', (code: number) => {
|
proc.on('close', (code: number) => {
|
||||||
@@ -112,10 +117,10 @@ parentPort?.on('message', async ({ file, speaker, model, modelDir, token }) => {
|
|||||||
const [f, buffer] = prepareAudioBuffer(file);
|
const [f, buffer] = prepareAudioBuffer(file);
|
||||||
|
|
||||||
// Fetch transcript and speakers
|
// Fetch transcript and speakers
|
||||||
const hasDiarization = speaker && await canDiarization();
|
const hasDiarization = await canDiarization();
|
||||||
const [transcript, speakers] = await Promise.all([
|
const [transcript, speakers] = await Promise.all([
|
||||||
whisperPipeline(buffer, {return_timestamps: speaker ? 'word' : false}),
|
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 });
|
if(file != f) rmSync(f, { recursive: true, force: true });
|
||||||
|
|
||||||
|
|||||||
@@ -56,5 +56,5 @@ export class Audio {
|
|||||||
return Object.assign(p, { abort });
|
return Object.assign(p, { abort });
|
||||||
}
|
}
|
||||||
|
|
||||||
canDiarization = canDiarization;
|
canDiarization = () => canDiarization().then(resp => !!resp);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user