Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cec892563e | |||
| 91066e070f |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "0.7.9",
|
"version": "0.7.11",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
30
src/audio.ts
30
src/audio.ts
@@ -1,6 +1,6 @@
|
|||||||
import {execSync, spawn} from 'node:child_process';
|
import {execSync, spawn} from 'node:child_process';
|
||||||
import {mkdtempSync, rmSync} from 'node:fs';
|
import {mkdtempSync} from 'node:fs';
|
||||||
import fs from 'node:fs/promises';
|
import fs, {rm} from 'node:fs/promises';
|
||||||
import {tmpdir} from 'node:os';
|
import {tmpdir} from 'node:os';
|
||||||
import Path, {join} from 'node:path';
|
import Path, {join} from 'node:path';
|
||||||
import {AbortablePromise, Ai} from './ai.ts';
|
import {AbortablePromise, Ai} from './ai.ts';
|
||||||
@@ -12,7 +12,7 @@ export class Audio {
|
|||||||
|
|
||||||
constructor(private ai: Ai) {
|
constructor(private ai: Ai) {
|
||||||
if(ai.options.whisper) {
|
if(ai.options.whisper) {
|
||||||
this.whisperModel = ai.options.asr?.endsWith('.bin') ? ai.options.asr : ai.options.asr + '.bin';
|
this.whisperModel = ai.options.asr || 'ggml-base.en.bin';
|
||||||
this.downloadAsrModel();
|
this.downloadAsrModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,13 +76,10 @@ print(json.dumps(segments))
|
|||||||
if(aborted) return;
|
if(aborted) return;
|
||||||
if(!p && !p3) throw new Error('Pyannote is not installed: pip install pyannote.audio');
|
if(!p && !p3) throw new Error('Pyannote is not installed: pip install pyannote.audio');
|
||||||
const binary = p3 ? 'python3' : 'python';
|
const binary = p3 ? 'python3' : 'python';
|
||||||
let tmp: string | null = null;
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
tmp = join(mkdtempSync(join(tmpdir(), 'audio-')), 'converted.wav');
|
|
||||||
execSync(`ffmpeg -i "${file}" -ar 16000 -ac 1 -f wav "${tmp}"`, { stdio: 'ignore' });
|
|
||||||
if(aborted) return;
|
if(aborted) return;
|
||||||
let output = '';
|
let output = '';
|
||||||
const proc = spawn(binary, ['-c', this.pyannote, tmp]);
|
const proc = spawn(binary, ['-c', this.pyannote, file]);
|
||||||
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) => {
|
||||||
@@ -95,7 +92,7 @@ print(json.dumps(segments))
|
|||||||
});
|
});
|
||||||
proc.on('error', reject);
|
proc.on('error', reject);
|
||||||
abort = () => proc.kill('SIGTERM');
|
abort = () => proc.kill('SIGTERM');
|
||||||
}).finally(() => { if(tmp) rmSync(Path.dirname(tmp), { recursive: true, force: true }); });
|
});
|
||||||
}));
|
}));
|
||||||
return <any>Object.assign(p, {abort});
|
return <any>Object.assign(p, {abort});
|
||||||
}
|
}
|
||||||
@@ -129,17 +126,22 @@ print(json.dumps(segments))
|
|||||||
asr(file: string, options: { model?: string; diarization?: boolean | 'id' } = {}): AbortablePromise<string | null> {
|
asr(file: string, options: { model?: string; diarization?: boolean | 'id' } = {}): AbortablePromise<string | null> {
|
||||||
if(!this.ai.options.whisper) throw new Error('Whisper not configured');
|
if(!this.ai.options.whisper) throw new Error('Whisper not configured');
|
||||||
|
|
||||||
const transcript = this.runAsr(file, {model: options.model, diarization: !!options.diarization});
|
const tmp = join(mkdtempSync(join(tmpdir(), 'audio-')), 'converted.wav');
|
||||||
const diarization: any = options.diarization ? this.runDiarization(file) : Promise.resolve(null);
|
execSync(`ffmpeg -i "${file}" -ar 16000 -ac 1 -f wav "${tmp}"`, { stdio: 'ignore' });
|
||||||
const abort = () => {
|
const clean = () => rm(Path.dirname(tmp), { recursive: true, force: true }).catch(() => {});
|
||||||
|
const transcript = this.runAsr(tmp, {model: options.model, diarization: !!options.diarization});
|
||||||
|
const diarization: any = options.diarization ? this.runDiarization(tmp) : Promise.resolve(null);
|
||||||
|
let aborted = false, abort = () => {
|
||||||
|
aborted = true;
|
||||||
transcript.abort();
|
transcript.abort();
|
||||||
diarization?.abort?.();
|
diarization?.abort?.();
|
||||||
|
clean();
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = Promise.all([transcript, diarization]).then(async ([t, d]) => {
|
const response = Promise.all([transcript, diarization]).then(async ([t, d]) => {
|
||||||
if(!options.diarization) return t;
|
if(aborted || !options.diarization) return t;
|
||||||
t = this.combineSpeakerTranscript(t, d);
|
t = this.combineSpeakerTranscript(t, d);
|
||||||
if(options.diarization === 'id') {
|
if(!aborted && options.diarization === 'id') {
|
||||||
if(!this.ai.language.defaultModel) throw new Error('Configure an LLM for advanced ASR speaker detection');
|
if(!this.ai.language.defaultModel) throw new Error('Configure an LLM for advanced ASR speaker detection');
|
||||||
let chunks = this.ai.language.chunk(t, 500, 0);
|
let chunks = this.ai.language.chunk(t, 500, 0);
|
||||||
if(chunks.length > 4) chunks = [...chunks.slice(0, 3), <string>chunks.at(-1)];
|
if(chunks.length > 4) chunks = [...chunks.slice(0, 3), <string>chunks.at(-1)];
|
||||||
@@ -150,7 +152,7 @@ print(json.dumps(segments))
|
|||||||
Object.entries(names).forEach(([speaker, name]) => t = t.replaceAll(`[Speaker ${speaker}]`, `[${name}]`));
|
Object.entries(names).forEach(([speaker, name]) => t = t.replaceAll(`[Speaker ${speaker}]`, `[${name}]`));
|
||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
});
|
}).finally(() => clean());
|
||||||
return <any>Object.assign(response, {abort});
|
return <any>Object.assign(response, {abort});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user