ASR debugging
This commit is contained in:
33
src/ai.ts
33
src/ai.ts
@@ -21,7 +21,7 @@ export type AiOptions = LLMOptions & {
|
|||||||
export type WhisperModel = 'tiny' | 'base' | 'small' | 'medium' | 'large';
|
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 */
|
||||||
@@ -43,11 +43,11 @@ export class Ai {
|
|||||||
*/
|
*/
|
||||||
async asr(path: string, model?: WhisperModel): Promise<string | null> {
|
async asr(path: string, model?: 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;
|
await this.downloadAsrModel(model);
|
||||||
await this.downloadAsrModel();
|
|
||||||
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() + '.txt';
|
||||||
const output = Path.join(this.options.whisper.temp || '/tmp', name);
|
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}`;
|
console.log(`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${model ? Path.join(this.options.whisper.path, model) : this.whisperModel} -f ${path} -otxt -of ${output}`);
|
||||||
|
await $`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${model ? Path.join(this.options.whisper.path, model) : this.whisperModel} -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(() => {}));
|
||||||
}
|
}
|
||||||
@@ -56,19 +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(): Promise<void> {
|
async downloadAsrModel(model?: string): Promise<string> {
|
||||||
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||||
if(await fs.stat(this.whisperModel).then(() => true).catch(() => false)) return;
|
let m;
|
||||||
const model = <string>this.whisperModel.split('/').at(-1);
|
if(model) m = model?.endsWith('.bin') ? model : model + '.bin';
|
||||||
if(!!this.downloads[model]) return this.downloads[model];
|
else m = <string>this.whisperModel.split('/').at(-1);
|
||||||
this.downloads[model] = fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${model}`)
|
const p = Path.join(this.options.whisper.path, m);
|
||||||
.then(resp => resp.arrayBuffer()).then(arr => Buffer.from(arr)).then(async buffer => {
|
if(await fs.stat(p).then(() => true).catch(() => false)) return p;
|
||||||
await fs.writeFile(this.whisperModel, buffer);
|
if(!!this.downloads[m]) return this.downloads[m];
|
||||||
delete this.downloads[model];
|
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(Path.join((<any>this.options.whisper).path, m), buffer);
|
||||||
|
delete this.downloads[m];
|
||||||
|
return p;
|
||||||
});
|
});
|
||||||
return this.downloads[model];
|
return this.downloads[m];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user