From a6de121551e97e9d32c715988bf460f992d92752 Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 13 Dec 2025 23:19:30 -0500 Subject: [PATCH] Fixed ASR command --- package.json | 2 +- src/ai.ts | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 5c5f379..590caa2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "0.1.11", + "version": "0.1.12", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/ai.ts b/src/ai.ts index d72df06..8f80cad 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -9,8 +9,8 @@ export type AiOptions = LLMOptions & { whisper?: { /** Whisper binary location */ binary: string; - /** Model */ - model: WhisperModel; + /** Model: `ggml-base.en.bin` */ + model: string; /** Path to models */ path: string; /** Path to storage location for temporary files */ @@ -18,8 +18,6 @@ export type AiOptions = LLMOptions & { } } -export type WhisperModel = 'tiny' | 'base' | 'small' | 'medium' | 'large'; - export class Ai { private downloads: {[key: string]: Promise} = {}; private whisperModel!: string; @@ -30,7 +28,7 @@ export class Ai { constructor(public readonly options: AiOptions) { this.llm = new LLM(this, options); 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.whisperModel = this.options.whisper?.model.endsWith('.bin') ? this.options.whisper?.model : this.options.whisper?.model + '.bin'; this.downloadAsrModel(); } } @@ -41,13 +39,11 @@ export class Ai { * @param model Whisper model * @returns {Promise} Extracted text */ - async asr(path: string, model?: WhisperModel): Promise { + async asr(path: string, model: string = this.whisperModel): Promise { if(!this.options.whisper?.binary) throw new Error('Whisper not configured'); const m = await this.downloadAsrModel(model); const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop() + '.txt'; const output = Path.join(this.options.whisper.temp || '/tmp', name); - console.log(this.options.whisper?.model + ' -> ' + this.whisperModel); - console.log(`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${m} -f ${path} -otxt -of ${output}`); await $`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${m} -f ${path} -otxt -of ${output}`; return fs.readFile(output, 'utf-8').then(text => text?.trim() || null) .finally(() => fs.rm(output, {force: true}).catch(() => {})); @@ -59,20 +55,20 @@ export class Ai { * @param {string} model Whisper model that will be downloaded * @return {Promise} Absolute path to model file, resolves once downloaded */ - async downloadAsrModel(model?: string): Promise { + async downloadAsrModel(model: string = this.whisperModel): Promise { if(!this.options.whisper?.binary) throw new Error('Whisper not configured'); - const m = model ? (model.endsWith('.bin') ? model : model + '.bin') : this.whisperModel.split('/').pop()!; - const p = Path.join(this.options.whisper.path, m); + if(!model.endsWith('.bin')) model += '.bin'; + const p = Path.join(this.options.whisper.path, model); if(await fs.stat(p).then(() => true).catch(() => false)) return p; - if(!!this.downloads[m]) return this.downloads[m]; - this.downloads[m] = fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${m}`) + if(!!this.downloads[model]) return this.downloads[model]; + 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(p, buffer); - delete this.downloads[m]; + delete this.downloads[model]; return p; }); - return this.downloads[m]; + return this.downloads[model]; } /**