5 Commits

Author SHA1 Message Date
d69bea3b38 Fixed ASR whisper models
All checks were successful
Publish Library / Build NPM Project (push) Successful in 30s
Publish Library / Tag Version (push) Successful in 7s
2025-12-13 22:47:35 -05:00
af4b09173c ASR debugging
All checks were successful
Publish Library / Build NPM Project (push) Successful in 30s
Publish Library / Tag Version (push) Successful in 7s
2025-12-13 22:31:54 -05:00
904cc10639 bump
All checks were successful
Publish Library / Build NPM Project (push) Successful in 51s
Publish Library / Tag Version (push) Successful in 7s
2025-12-13 22:05:03 -05:00
07f9593b6a ASR debugging
All checks were successful
Publish Library / Build NPM Project (push) Successful in 29s
Publish Library / Tag Version (push) Successful in 7s
2025-12-13 22:02:13 -05:00
af42506174 ASR fixes
All checks were successful
Publish Library / Build NPM Project (push) Successful in 29s
Publish Library / Tag Version (push) Successful in 7s
2025-12-13 20:48:36 -05:00
2 changed files with 26 additions and 19 deletions

View File

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

View File

@@ -11,15 +11,17 @@ 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;
}
}
export type WhisperModel = 'tiny' | 'base' | 'small' | 'medium' | 'large';
export class Ai {
private downloads: {[key: string]: Promise<void>} = {};
private downloads: {[key: string]: Promise<string>} = {};
private whisperModel!: string;
/** Large Language Models */
@@ -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(<string>this.options.whisper?.path, this.options.whisper?.model + this.options.whisper?.model.endsWith('.bin') ? '' : '.bin');
this.downloadAsrModel();
}
}
/**
@@ -38,11 +43,10 @@ export class Ai {
*/
async asr(path: string, model?: WhisperModel): Promise<string | null> {
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
if(!model) model = this.options.whisper.model;
await this.downloadAsrModel(<string>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}`;
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);
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(() => {}));
}
@@ -51,19 +55,22 @@ export class Ai {
* Downloads the specified Whisper model if it is not already present locally.
*
* @param {string} model Whisper model that will be downloaded
* @return {Promise<void>} A promise that resolves once the model is downloaded and saved locally.
* @return {Promise<string>} Absolute path to model file, resolves once downloaded
*/
async downloadAsrModel(model: string): Promise<void> {
async downloadAsrModel(model?: string): Promise<string> {
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
this.whisperModel = Path.join(<string>this.options.whisper?.path, this.options.whisper?.model + '.bin');
if(await fs.stat(this.whisperModel).then(() => true).catch(() => false)) return;
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`)
.then(resp => resp.arrayBuffer()).then(arr => Buffer.from(arr)).then(async buffer => {
await fs.writeFile(this.whisperModel, buffer);
delete this.downloads[model];
const m = model ? (model.endsWith('.bin') ? model : model + '.bin') : this.whisperModel.split('/').pop()!;
const p = Path.join(this.options.whisper.path, m);
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}`)
.then(resp => resp.arrayBuffer())
.then(arr => Buffer.from(arr)).then(async buffer => {
await fs.writeFile(p, buffer);
delete this.downloads[m];
return p;
});
return this.downloads[model];
return this.downloads[m];
}
/**