Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0112c92505 | |||
| 2351f590b5 | |||
| 2c2acef84e | |||
| a6de121551 | |||
| 31d9ee4390 | |||
| d69bea3b38 | |||
| af4b09173c |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.1.8",
|
||||
"version": "0.1.15",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
54
src/ai.ts
54
src/ai.ts
@@ -1,25 +1,21 @@
|
||||
import {$} from '@ztimson/node-utils';
|
||||
import {createWorker} from 'tesseract.js';
|
||||
import {LLM, LLMOptions} from './llm';
|
||||
import fs from 'node:fs/promises';
|
||||
import Path from 'node:path';
|
||||
import * as tf from '@tensorflow/tfjs';
|
||||
import {spawn} from 'node:child_process';
|
||||
|
||||
export type AiOptions = LLMOptions & {
|
||||
whisper?: {
|
||||
/** Whisper binary location */
|
||||
binary: string;
|
||||
/** Model */
|
||||
model: WhisperModel;
|
||||
/** Model: `ggml-base.en.bin` */
|
||||
model: string;
|
||||
/** 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<string>} = {};
|
||||
private whisperModel!: string;
|
||||
@@ -30,7 +26,7 @@ export class Ai {
|
||||
constructor(public readonly options: AiOptions) {
|
||||
this.llm = new LLM(this, options);
|
||||
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.whisperModel = this.options.whisper?.model.endsWith('.bin') ? this.options.whisper?.model : this.options.whisper?.model + '.bin';
|
||||
this.downloadAsrModel();
|
||||
}
|
||||
}
|
||||
@@ -41,15 +37,23 @@ export class Ai {
|
||||
* @param model Whisper model
|
||||
* @returns {Promise<any>} Extracted text
|
||||
*/
|
||||
async asr(path: string, model?: WhisperModel): Promise<string | null> {
|
||||
asr(path: string, model: string = this.whisperModel): {abort: () => void, response: Promise<string | null>} {
|
||||
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||
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);
|
||||
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)
|
||||
.finally(() => fs.rm(output, {force: true}).catch(() => {}));
|
||||
let abort: any = () => {};
|
||||
const response = new Promise<string | null>((resolve, reject) => {
|
||||
this.downloadAsrModel(model).then(m => {
|
||||
let output = '';
|
||||
const proc = spawn(<string>this.options.whisper?.binary, ['-nt', '-np', '-m', m, '-f', path], {stdio: ['ignore', 'pipe', 'ignore']});
|
||||
abort = () => proc.kill('SIGTERM');
|
||||
proc.on('error', (err: Error) => reject(err));
|
||||
proc.stdout.on('data', (data: Buffer) => output += data.toString());
|
||||
proc.on('close', (code: number) => {
|
||||
if(code === 0) resolve(output.trim() || null);
|
||||
else reject(new Error(`Exit code ${code}`));
|
||||
});
|
||||
});
|
||||
});
|
||||
return {response, abort};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,22 +62,20 @@ export class Ai {
|
||||
* @param {string} model Whisper model that will be downloaded
|
||||
* @return {Promise<string>} Absolute path to model file, resolves once downloaded
|
||||
*/
|
||||
async downloadAsrModel(model?: string): Promise<string> {
|
||||
async downloadAsrModel(model: string = this.whisperModel): Promise<string> {
|
||||
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||
let m;
|
||||
if(model) m = model?.endsWith('.bin') ? model : model + '.bin';
|
||||
else m = <string>this.whisperModel.split('/').at(-1);
|
||||
const p = Path.join(this.options.whisper.path, m);
|
||||
if(!model.endsWith('.bin')) model += '.bin';
|
||||
const p = Path.join(this.options.whisper.path, model);
|
||||
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}`)
|
||||
if(!!this.downloads[model]) return this.downloads[model];
|
||||
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(Path.join((<any>this.options.whisper).path, m), buffer);
|
||||
delete this.downloads[m];
|
||||
await fs.writeFile(p, buffer);
|
||||
delete this.downloads[model];
|
||||
return p;
|
||||
});
|
||||
return this.downloads[m];
|
||||
return this.downloads[model];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user