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

This commit is contained in:
2025-12-13 20:48:36 -05:00
parent 08e105b033
commit af42506174
2 changed files with 15 additions and 10 deletions

View File

@@ -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(<string>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<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}`;
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<void>} A promise that resolves once the model is downloaded and saved locally.
*/
async downloadAsrModel(model: string): Promise<void> {
async downloadAsrModel(): Promise<void> {
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;
const model = <string>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];