Removed ASR file intermediary
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "0.1.13",
|
"version": "0.1.14",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
29
src/ai.ts
29
src/ai.ts
@@ -1,9 +1,9 @@
|
|||||||
import {$} from '@ztimson/node-utils';
|
|
||||||
import {createWorker} from 'tesseract.js';
|
import {createWorker} from 'tesseract.js';
|
||||||
import {LLM, LLMOptions} from './llm';
|
import {LLM, LLMOptions} from './llm';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import Path from 'node:path';
|
import Path from 'node:path';
|
||||||
import * as tf from '@tensorflow/tfjs';
|
import * as tf from '@tensorflow/tfjs';
|
||||||
|
import {spawn} from 'node:child_process';
|
||||||
|
|
||||||
export type AiOptions = LLMOptions & {
|
export type AiOptions = LLMOptions & {
|
||||||
whisper?: {
|
whisper?: {
|
||||||
@@ -13,8 +13,6 @@ export type AiOptions = LLMOptions & {
|
|||||||
model: string;
|
model: string;
|
||||||
/** Path to models */
|
/** Path to models */
|
||||||
path: string;
|
path: string;
|
||||||
/** Path to storage location for temporary files */
|
|
||||||
temp?: string;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,16 +38,23 @@ export class Ai {
|
|||||||
* @param model Whisper model
|
* @param model Whisper model
|
||||||
* @returns {Promise<any>} Extracted text
|
* @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');
|
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||||
const m = await this.downloadAsrModel(model);
|
let abort: any = () => {};
|
||||||
const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop() + '.txt';
|
const response = new Promise<string | null>((resolve, reject) => {
|
||||||
const output = Path.join(this.options.whisper.temp || '/tmp', name);
|
this.downloadAsrModel(model).then(m => {
|
||||||
console.log('ASR: ' + this.options.whisper.model + ' -> ' + this.whisperModel);
|
let output = '';
|
||||||
console.log(`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${m} -f ${path} -otxt -of ${output}`);
|
const proc = spawn(<string>this.options.whisper?.binary, ['-nt', '-np', '-m', m, '-f', path], {stdio: ['ignore', 'pipe', 'ignore']});
|
||||||
await $`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${m} -f ${path} -otxt -of ${output}`;
|
abort = () => proc.kill('SIGTERM');
|
||||||
return fs.readFile(output, 'utf-8').then(text => text?.trim() || null)
|
proc.on('error', (err: Error) => reject(err));
|
||||||
.finally(() => fs.rm(output, {force: true}).catch(() => {}));
|
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};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user