Queue OCR & ASR work
This commit is contained in:
64
src/audio.ts
64
src/audio.ts
@@ -5,37 +5,59 @@ import {canDiarization} from './asr.ts';
|
||||
import {dirname, join} from 'path';
|
||||
|
||||
export class Audio {
|
||||
private busy = false;
|
||||
private currentJob: any;
|
||||
private queue: Array<{file: string, model: string, speaker: boolean | 'id', modelDir: string, token: string, resolve: any, reject: any}> = [];
|
||||
private worker: Worker | null = null;
|
||||
|
||||
constructor(private ai: Ai) {}
|
||||
|
||||
private processQueue() {
|
||||
if(this.busy || !this.queue.length) return;
|
||||
|
||||
this.busy = true;
|
||||
const job = this.queue.shift()!;
|
||||
if(!this.worker) {
|
||||
this.worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'asr.js'));
|
||||
this.worker.on('message', this.handleMessage.bind(this));
|
||||
this.worker.on('error', this.handleError.bind(this));
|
||||
}
|
||||
|
||||
this.currentJob = job;
|
||||
this.worker.postMessage({file: job.file, model: job.model, speaker: job.speaker, modelDir: job.modelDir, token: job.token});
|
||||
}
|
||||
|
||||
private handleMessage({text, warning, error}: any) {
|
||||
const job = this.currentJob!;
|
||||
this.busy = false;
|
||||
if(error) job.reject(new Error(error));
|
||||
else {
|
||||
if(warning) console.warn(warning);
|
||||
job.resolve(text);
|
||||
}
|
||||
this.processQueue();
|
||||
}
|
||||
|
||||
private handleError(err: Error) {
|
||||
if(this.currentJob) {
|
||||
this.currentJob.reject(err);
|
||||
this.busy = false;
|
||||
this.processQueue();
|
||||
}
|
||||
}
|
||||
|
||||
asr(file: string, options: { model?: string; speaker?: boolean | 'id' } = {}): AbortablePromise<string | null> {
|
||||
const { model = this.ai.options.asr || 'whisper-base', speaker = false } = options;
|
||||
let aborted = false;
|
||||
const abort = () => { aborted = true; };
|
||||
|
||||
let p = new Promise<string | null>((resolve, reject) => {
|
||||
const worker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'asr.js'));
|
||||
const handleMessage = ({ text, warning, error }: any) => {
|
||||
setTimeout(() => worker.terminate(), 1000);
|
||||
if(aborted) return;
|
||||
if(error) reject(new Error(error));
|
||||
else {
|
||||
if(warning) console.warn(warning);
|
||||
resolve(text);
|
||||
}
|
||||
};
|
||||
const handleError = (err: Error) => {
|
||||
setTimeout(() => worker.terminate(), 1000);
|
||||
if(!aborted) reject(err);
|
||||
};
|
||||
worker.on('message', handleMessage);
|
||||
worker.on('error', handleError);
|
||||
worker.on('exit', (code) => {
|
||||
if(code !== 0 && !aborted) reject(new Error(`Worker exited with code ${code}`));
|
||||
this.queue.push({file, model, speaker, modelDir: <string>this.ai.options.path, token: <string>this.ai.options.hfToken,
|
||||
resolve: (text: string | null) => !aborted && resolve(text),
|
||||
reject: (err: Error) => !aborted && reject(err)
|
||||
});
|
||||
worker.postMessage({file, model, speaker, modelDir: this.ai.options.path, token: this.ai.options.hfToken});
|
||||
this.processQueue();
|
||||
});
|
||||
|
||||
// Name speakers using AI
|
||||
if(options.speaker == 'id') {
|
||||
if(!this.ai.language.defaultModel) throw new Error('Configure an LLM for advanced ASR speaker detection');
|
||||
p = p.then(async transcript => {
|
||||
|
||||
Reference in New Issue
Block a user