8 Commits

Author SHA1 Message Date
2c2acef84e ASR logging
All checks were successful
Publish Library / Build NPM Project (push) Successful in 37s
Publish Library / Tag Version (push) Successful in 8s
2025-12-14 08:49:02 -05:00
a6de121551 Fixed ASR command
All checks were successful
Publish Library / Build NPM Project (push) Successful in 26s
Publish Library / Tag Version (push) Successful in 7s
2025-12-13 23:19:30 -05:00
31d9ee4390 ASR Debugging
All checks were successful
Publish Library / Build NPM Project (push) Successful in 43s
Publish Library / Tag Version (push) Successful in 17s
2025-12-13 22:59:23 -05:00
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 33 additions and 21 deletions

View File

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

View File

@@ -9,17 +9,17 @@ export type AiOptions = LLMOptions & {
whisper?: { whisper?: {
/** Whisper binary location */ /** Whisper binary location */
binary: string; binary: string;
/** Model */ /** Model: `ggml-base.en.bin` */
model: WhisperModel; model: string;
/** Working directory for models and temporary files */ /** Path to models */
path: string; path: string;
/** Path to storage location for temporary files */
temp?: string;
} }
} }
export type WhisperModel = 'tiny' | 'base' | 'small' | 'medium' | 'large';
export class Ai { export class Ai {
private downloads: {[key: string]: Promise<void>} = {}; private downloads: {[key: string]: Promise<string>} = {};
private whisperModel!: string; private whisperModel!: string;
/** Large Language Models */ /** Large Language Models */
@@ -27,7 +27,11 @@ export class Ai {
constructor(public readonly options: AiOptions) { constructor(public readonly options: AiOptions) {
this.llm = new LLM(this, options); this.llm = new LLM(this, options);
if(this.options.whisper?.binary) this.downloadAsrModel(this.options.whisper.model); if(this.options.whisper?.binary) {
this.whisperModel = this.options.whisper?.model.endsWith('.bin') ? this.options.whisper?.model : this.options.whisper?.model + '.bin';
console.log('constructor: ' + this.options.whisper.model + ' -> ' + this.whisperModel);
this.downloadAsrModel();
}
} }
/** /**
@@ -36,13 +40,14 @@ export class Ai {
* @param model Whisper model * @param model Whisper model
* @returns {Promise<any>} Extracted text * @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'); if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
if(!model) model = this.options.whisper.model; const m = await this.downloadAsrModel(model);
await this.downloadAsrModel(<string>model); const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop() + '.txt';
const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop(); const output = Path.join(this.options.whisper.temp || '/tmp', name);
const output = Path.join(this.options.whisper.path || '/tmp', name); console.log('ASR: ' + this.options.whisper.model + ' -> ' + this.whisperModel);
await $`rm -f /tmp/${name}.txt && ${this.options.whisper.binary} -nt -np -m ${this.whisperModel} -f ${path} -otxt -of ${output}`; 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) return fs.readFile(output, 'utf-8').then(text => text?.trim() || null)
.finally(() => fs.rm(output, {force: true}).catch(() => {})); .finally(() => fs.rm(output, {force: true}).catch(() => {}));
} }
@@ -51,17 +56,24 @@ export class Ai {
* Downloads the specified Whisper model if it is not already present locally. * Downloads the specified Whisper model if it is not already present locally.
* *
* @param {string} model Whisper model that will be downloaded * @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 = this.whisperModel): Promise<string> {
if(!this.options.whisper?.binary) throw new Error('Whisper not configured'); 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(!model.endsWith('.bin')) model += '.bin';
if(await fs.stat(this.whisperModel).then(() => true).catch(() => false)) return; const p = Path.join(this.options.whisper.path, model);
console.log('Download: ' + p);
if(await fs.stat(p).then(() => true).catch(() => false)) {
console.log('Exists!');
return p;
}
if(!!this.downloads[model]) return this.downloads[model]; 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 => { .then(resp => resp.arrayBuffer())
await fs.writeFile(this.whisperModel, buffer); .then(arr => Buffer.from(arr)).then(async buffer => {
await fs.writeFile(p, buffer);
delete this.downloads[model]; delete this.downloads[model];
return p;
}); });
return this.downloads[model]; return this.downloads[model];
} }