Compare commits

...

2 Commits

Author SHA1 Message Date
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
a94b153c6d Fixed embedder autostart bug
All checks were successful
Publish Library / Build NPM Project (push) Successful in 36s
Publish Library / Tag Version (push) Successful in 5s
2026-02-21 00:30:38 -05:00
3 changed files with 17 additions and 15 deletions

View File

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

View File

@@ -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';
@@ -40,6 +40,7 @@ print(json.dumps(segments))
this.downloadAsrModel(opts.model).then(m => { this.downloadAsrModel(opts.model).then(m => {
let output = ''; let output = '';
const args = [opts.diarization ? '-owts' : '-nt', '-m', m, '-f', file]; const args = [opts.diarization ? '-owts' : '-nt', '-m', m, '-f', file];
console.log(<string>this.ai.options.whisper + ' ' + args.join(' '))
proc = spawn(<string>this.ai.options.whisper, args, {stdio: ['ignore', 'pipe', 'ignore']}); proc = spawn(<string>this.ai.options.whisper, args, {stdio: ['ignore', 'pipe', 'ignore']});
proc.on('error', (err: Error) => reject(err)); proc.on('error', (err: Error) => reject(err));
proc.stdout.on('data', (data: Buffer) => output += data.toString()); proc.stdout.on('data', (data: Buffer) => output += data.toString());
@@ -76,13 +77,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 +93,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 +127,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 +153,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});
} }

View File

@@ -1,7 +1,6 @@
export * from './ai'; export * from './ai';
export * from './antrhopic'; export * from './antrhopic';
export * from './audio'; export * from './audio';
export * from './embedder'
export * from './llm'; export * from './llm';
export * from './open-ai'; export * from './open-ai';
export * from './provider'; export * from './provider';