From af4250617494988173f79f0eecd9d635f91081eb Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 13 Dec 2025 20:48:36 -0500 Subject: [PATCH] ASR fixes --- package.json | 2 +- src/ai.ts | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 7e236f1..dc11eb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "0.1.6", + "version": "0.1.7", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/ai.ts b/src/ai.ts index b8418d0..4483632 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -11,8 +11,10 @@ export type AiOptions = LLMOptions & { binary: string; /** Model */ model: WhisperModel; - /** Working directory for models and temporary files */ + /** Path to models */ path: string; + /** Path to storage location for temporary files */ + temp?: string; } } @@ -27,7 +29,10 @@ export class Ai { constructor(public readonly options: AiOptions) { this.llm = new LLM(this, options); - if(this.options.whisper?.binary) this.downloadAsrModel(this.options.whisper.model); + if(this.options.whisper?.binary) { + this.whisperModel = Path.join(this.options.whisper?.path, this.options.whisper?.model + this.options.whisper?.model.endsWith('.bin') ? '' : '.bin'); + this.downloadAsrModel(); + } } /** @@ -39,10 +44,10 @@ export class Ai { async asr(path: string, model?: WhisperModel): Promise { if(!this.options.whisper?.binary) throw new Error('Whisper not configured'); if(!model) model = this.options.whisper.model; - await this.downloadAsrModel(model); - const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop(); - const output = Path.join(this.options.whisper.path || '/tmp', name); - await $`rm -f /tmp/${name}.txt && ${this.options.whisper.binary} -nt -np -m ${this.whisperModel} -f ${path} -otxt -of ${output}`; + await this.downloadAsrModel(); + const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop() + '.txt'; + const output = Path.join(this.options.whisper.temp || '/tmp', name); + await $`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${this.whisperModel} -f ${path} -otxt -of ${output}`; return fs.readFile(output, 'utf-8').then(text => text?.trim() || null) .finally(() => fs.rm(output, {force: true}).catch(() => {})); } @@ -53,12 +58,12 @@ export class Ai { * @param {string} model Whisper model that will be downloaded * @return {Promise} A promise that resolves once the model is downloaded and saved locally. */ - async downloadAsrModel(model: string): Promise { + async downloadAsrModel(): Promise { if(!this.options.whisper?.binary) throw new Error('Whisper not configured'); - this.whisperModel = Path.join(this.options.whisper?.path, this.options.whisper?.model + '.bin'); if(await fs.stat(this.whisperModel).then(() => true).catch(() => false)) return; + const model = this.whisperModel.split('/').at(-1); if(!!this.downloads[model]) return this.downloads[model]; - this.downloads[model] = fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${this.options.whisper?.model}.bin`) + this.downloads[model] = fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${model}`) .then(resp => resp.arrayBuffer()).then(arr => Buffer.from(arr)).then(async buffer => { await fs.writeFile(this.whisperModel, buffer); delete this.downloads[model];