Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| af4b09173c | |||
| 904cc10639 | |||
| 07f9593b6a | |||
| af42506174 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "0.1.6",
|
"version": "0.1.9",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
48
src/ai.ts
48
src/ai.ts
@@ -11,15 +11,17 @@ export type AiOptions = LLMOptions & {
|
|||||||
binary: string;
|
binary: string;
|
||||||
/** Model */
|
/** Model */
|
||||||
model: WhisperModel;
|
model: WhisperModel;
|
||||||
/** 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 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 +29,10 @@ 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 = Path.join(<string>this.options.whisper?.path, this.options.whisper?.model + this.options.whisper?.model.endsWith('.bin') ? '' : '.bin');
|
||||||
|
this.downloadAsrModel();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,11 +43,13 @@ 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;
|
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('model:', this.options.whisper?.model);
|
||||||
await $`rm -f /tmp/${name}.txt && ${this.options.whisper.binary} -nt -np -m ${this.whisperModel} -f ${path} -otxt -of ${output}`;
|
console.log(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)
|
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,19 +58,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): 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');
|
let m;
|
||||||
if(await fs.stat(this.whisperModel).then(() => true).catch(() => false)) return;
|
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/${this.options.whisper?.model}.bin`)
|
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