Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0112c92505 | |||
| 2351f590b5 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.1.13",
|
||||
"version": "0.1.15",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
36
src/ai.ts
36
src/ai.ts
@@ -1,9 +1,9 @@
|
||||
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?: {
|
||||
@@ -13,8 +13,6 @@ export type AiOptions = LLMOptions & {
|
||||
model: string;
|
||||
/** Path to models */
|
||||
path: string;
|
||||
/** Path to storage location for temporary files */
|
||||
temp?: string;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +27,6 @@ export class Ai {
|
||||
this.llm = new LLM(this, options);
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -40,16 +37,23 @@ export class Ai {
|
||||
* @param model Whisper model
|
||||
* @returns {Promise<any>} Extracted text
|
||||
*/
|
||||
async asr(path: string, model: string = this.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');
|
||||
const m = 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('ASR: ' + this.options.whisper.model + ' -> ' + 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)
|
||||
.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};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,11 +66,7 @@ export class Ai {
|
||||
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||
if(!model.endsWith('.bin')) model += '.bin';
|
||||
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(await fs.stat(p).then(() => true).catch(() => false)) return p;
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user