From 0360f2493df249706cd3874caa768e935fcfdc4b Mon Sep 17 00:00:00 2001 From: ztimson Date: Thu, 12 Feb 2026 22:15:57 -0500 Subject: [PATCH] Added hugging face token --- package.json | 2 +- src/ai.ts | 2 ++ src/asr.ts | 11 ++++++----- src/audio.ts | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 4750e83..908ac7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "0.6.9", + "version": "0.6.10", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/ai.ts b/src/ai.ts index 18b63a9..a3dc58a 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -8,6 +8,8 @@ export type AbortablePromise = Promise & { }; export type AiOptions = { + /** Token to pull models from hugging face */ + hfToken?: string; /** Path to models */ path?: string; /** ASR model: whisper-tiny, whisper-base */ diff --git a/src/asr.ts b/src/asr.ts index 1bfa874..f076766 100644 --- a/src/asr.ts +++ b/src/asr.ts @@ -14,14 +14,15 @@ export async function canDiarization(): Promise { }); } -async function runDiarization(audioPath: string, torchHome: string): Promise { +async function runDiarization(audioPath: string, dir: string, token: string): Promise { const script = ` import sys import json import os from pyannote.audio import Pipeline -os.environ['TORCH_HOME'] = "${torchHome}" +os.environ['TORCH_HOME'] = "${dir}" +os.environ['HF_TOKEN'] = "${token}" pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1") diarization = pipeline(sys.argv[1]) @@ -82,7 +83,7 @@ function combineSpeakerTranscript(chunks: any[], speakers: any[]): string { return lines.join('\n'); } -parentPort?.on('message', async ({ file, speaker, model, modelDir }) => { +parentPort?.on('message', async ({ file, speaker, model, modelDir, token }) => { try { console.log('worker', file); if(!whisperPipeline) whisperPipeline = await pipeline('automatic-speech-recognition', `Xenova/${model}`, {cache_dir: modelDir, quantized: true}); @@ -111,12 +112,12 @@ parentPort?.on('message', async ({ file, speaker, model, modelDir }) => { // Speaker Diarization const hasDiarization = await canDiarization(); - if(!hasDiarization) { + if(!token || !hasDiarization) { parentPort?.postMessage({ text: transcriptResult.text?.trim() || null, error: 'Speaker diarization unavailable' }); return; } - const speakers = await runDiarization(file, modelDir); + const speakers = await runDiarization(file, modelDir, token); const combined = combineSpeakerTranscript(transcriptResult.chunks || [], speakers); parentPort?.postMessage({ text: combined }); } catch (err) { diff --git a/src/audio.ts b/src/audio.ts index c7fe00f..e17c885 100644 --- a/src/audio.ts +++ b/src/audio.ts @@ -32,7 +32,7 @@ export class Audio { worker.on('exit', (code) => { if(code !== 0 && !aborted) reject(new Error(`Worker exited with code ${code}`)); }); - worker.postMessage({file, model, speaker, modelDir: this.ai.options.path}); + worker.postMessage({file, model, speaker, modelDir: this.ai.options.path, token: this.ai.options.hfToken}); }); return Object.assign(p, { abort }); }