From e4399e1b7b5960a1d70a1a65f8bba0e413d42b50 Mon Sep 17 00:00:00 2001 From: ztimson Date: Thu, 12 Feb 2026 20:14:00 -0500 Subject: [PATCH] Updataes? --- package.json | 2 +- src/asr.ts | 10 +++++----- src/audio.ts | 8 ++++---- src/embedder.ts | 7 ++----- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 9e21d97..6c52ba5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "0.6.6", + "version": "0.6.7", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/asr.ts b/src/asr.ts index d177a5f..57a42f9 100644 --- a/src/asr.ts +++ b/src/asr.ts @@ -82,12 +82,12 @@ function combineSpeakerTranscript(chunks: any[], speakers: any[]): string { return lines.join('\n'); } -parentPort?.on('message', async ({ path, model, speaker, torchHome }) => { +parentPort?.on('message', async ({ file, speaker, model, modelDir }) => { try { - if(!whisperPipeline) whisperPipeline = await pipeline('automatic-speech-recognition', `Xenova/${model}`, {cache_dir: torchHome, quantized: true}); + if(!whisperPipeline) whisperPipeline = await pipeline('automatic-speech-recognition', `Xenova/${model}`, {cache_dir: modelDir, quantized: true}); // Prepare audio file (convert to mono channel wave) - const wav = new wavefile.WaveFile(fs.readFileSync(path)); + const wav = new wavefile.WaveFile(fs.readFileSync(file)); wav.toBitDepth('32f'); wav.toSampleRate(16000); const samples = wav.getSamples(); @@ -111,11 +111,11 @@ parentPort?.on('message', async ({ path, model, speaker, torchHome }) => { // Speaker Diarization const hasDiarization = await canDiarization(); if(!hasDiarization) { - parentPort?.postMessage({ text: transcriptResult.text?.trim() || null, warning: 'Speaker diarization unavailable' }); + parentPort?.postMessage({ text: transcriptResult.text?.trim() || null, error: 'Speaker diarization unavailable' }); return; } - const speakers = await runDiarization(path, torchHome); + const speakers = await runDiarization(file, modelDir); const combined = combineSpeakerTranscript(transcriptResult.chunks || [], speakers); parentPort?.postMessage({ text: combined }); } catch (err) { diff --git a/src/audio.ts b/src/audio.ts index d41d99b..5dd523f 100644 --- a/src/audio.ts +++ b/src/audio.ts @@ -1,18 +1,18 @@ import {Worker} from 'worker_threads'; -import path from 'node:path'; +import Path from 'node:path'; import {AbortablePromise, Ai} from './ai.ts'; import {canDiarization} from './asr.ts'; export class Audio { constructor(private ai: Ai) {} - asr(filepath: string, options: { model?: string; speaker?: boolean } = {}): AbortablePromise { + asr(file: string, options: { model?: string; speaker?: boolean } = {}): AbortablePromise { const { model = this.ai.options.asr || 'whisper-base', speaker = false } = options; let aborted = false; const abort = () => { aborted = true; }; const p = new Promise((resolve, reject) => { - const worker = new Worker(path.join(import.meta.dirname, 'asr.js')); + const worker = new Worker(Path.join(import.meta.dirname, 'asr.js')); const handleMessage = ({ text, warning, error }: any) => { worker.terminate(); if(aborted) return; @@ -31,7 +31,7 @@ export class Audio { worker.on('exit', (code) => { if(code !== 0 && !aborted) reject(new Error(`Worker exited with code ${code}`)); }); - worker.postMessage({path: filepath, model, speaker, torchHome: this.ai.options.path,}); + worker.postMessage({file, model, speaker, modelDir: this.ai.options.path}); }); return Object.assign(p, { abort }); } diff --git a/src/embedder.ts b/src/embedder.ts index e6f4ed3..49f5e7e 100644 --- a/src/embedder.ts +++ b/src/embedder.ts @@ -3,11 +3,8 @@ import { parentPort } from 'worker_threads'; let embedder: any; -parentPort?.on('message', async ({ id, text, model, path }) => { - if(!embedder) embedder = await pipeline('feature-extraction', 'Xenova/' + model, { - quantized: true, - cache_dir: path, - }); +parentPort?.on('message', async ({ id, text, model, modelDir }) => { + if(!embedder) embedder = await pipeline('feature-extraction', 'Xenova/' + model, {quantized: true, cache_dir: modelDir}); const output = await embedder(text, { pooling: 'mean', normalize: true }); const embedding = Array.from(output.data); parentPort?.postMessage({ id, embedding });