Compare commits

...

2 Commits

Author SHA1 Message Date
cec892563e Whisper ASR
All checks were successful
Publish Library / Build NPM Project (push) Successful in 33s
Publish Library / Tag Version (push) Successful in 5s
2026-02-21 01:03:25 -05:00
91066e070f WIP ASR
All checks were successful
Publish Library / Build NPM Project (push) Successful in 33s
Publish Library / Tag Version (push) Successful in 5s
2026-02-21 00:51:01 -05:00
2 changed files with 17 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@ztimson/ai-utils",
"version": "0.7.9",
"version": "0.7.11",
"description": "AI Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@@ -1,6 +1,6 @@
import {execSync, spawn} from 'node:child_process';
import {mkdtempSync, rmSync} from 'node:fs';
import fs from 'node:fs/promises';
import {mkdtempSync} from 'node:fs';
import fs, {rm} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import Path, {join} from 'node:path';
import {AbortablePromise, Ai} from './ai.ts';
@@ -12,7 +12,7 @@ export class Audio {
constructor(private ai: Ai) {
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();
}
@@ -76,13 +76,10 @@ print(json.dumps(segments))
if(aborted) return;
if(!p && !p3) throw new Error('Pyannote is not installed: pip install pyannote.audio');
const binary = p3 ? 'python3' : 'python';
let tmp: string | null = null;
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;
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.stderr.on('data', (data: Buffer) => console.error(data.toString()));
proc.on('close', (code: number) => {
@@ -95,7 +92,7 @@ print(json.dumps(segments))
});
proc.on('error', reject);
abort = () => proc.kill('SIGTERM');
}).finally(() => { if(tmp) rmSync(Path.dirname(tmp), { recursive: true, force: true }); });
});
}));
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> {
if(!this.ai.options.whisper) throw new Error('Whisper not configured');
const transcript = this.runAsr(file, {model: options.model, diarization: !!options.diarization});
const diarization: any = options.diarization ? this.runDiarization(file) : Promise.resolve(null);
const abort = () => {
const tmp = join(mkdtempSync(join(tmpdir(), 'audio-')), 'converted.wav');
execSync(`ffmpeg -i "${file}" -ar 16000 -ac 1 -f wav "${tmp}"`, { stdio: 'ignore' });
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();
diarization?.abort?.();
clean();
};
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);
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');
let chunks = this.ai.language.chunk(t, 500, 0);
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}]`));
}
return t;
});
}).finally(() => clean());
return <any>Object.assign(response, {abort});
}