Fixed ASR command
All checks were successful
Publish Library / Build NPM Project (push) Successful in 26s
Publish Library / Tag Version (push) Successful in 7s

This commit is contained in:
2025-12-13 23:19:30 -05:00
parent 31d9ee4390
commit a6de121551
2 changed files with 12 additions and 16 deletions

View File

@@ -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",

View File

@@ -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<string>} = {};
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(<string>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<any>} Extracted text
*/
async asr(path: string, model?: WhisperModel): Promise<string | null> {
async asr(path: string, model: string = this.whisperModel): Promise<string | null> {
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<string>} Absolute path to model file, resolves once downloaded
*/
async downloadAsrModel(model?: string): Promise<string> {
async downloadAsrModel(model: string = this.whisperModel): Promise<string> {
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];
}
/**