WeLe Agentic AI with Docker deployment
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// ============================================
|
||||
// Speech models — all ONNX, all CPU, all in this Node process.
|
||||
//
|
||||
// There is no GPU and no Python. That is the whole point: the AWS host has
|
||||
// neither, and a second service was one more thing to deploy and keep alive.
|
||||
//
|
||||
// VAD Silero 2 MB endpointing
|
||||
// STT Whisper base ~80 MB Tamil + English + language detection
|
||||
// TTS MMS-TTS VITS ~114 MB per language, feed-forward
|
||||
//
|
||||
// Measured on an i7-10850H, CPU only:
|
||||
// TTS RTF 0.28x (3.5x faster than realtime)
|
||||
// STT ~1.2 s for 4 s of audio
|
||||
//
|
||||
// Two findings worth keeping:
|
||||
//
|
||||
// * VITS is feed-forward. The earlier Parler-TTS attempt was autoregressive
|
||||
// and ran at RTF ~5x — i.e. 5x SLOWER than realtime — which is why voice was
|
||||
// unusable even on a GPU. Architecture mattered far more than hardware here.
|
||||
//
|
||||
// * int8 is a trap for a model this small: dynamic quantisation made TTS 5.7x
|
||||
// SLOWER than fp32 (RTF 1.67x vs 0.28x) because the quantise/dequantise
|
||||
// overhead dominates. We ship fp32 deliberately.
|
||||
// ============================================
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { pipeline, env } from '@huggingface/transformers';
|
||||
import logger from '../utils/logger.js';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
|
||||
// Hub downloads are cached here so a container restart does not re-fetch.
|
||||
env.cacheDir = process.env.SPEECH_CACHE_DIR || path.join(ROOT, '.transformers-cache');
|
||||
// Tamil is loaded from a folder we exported ourselves — no public ONNX build
|
||||
// of mms-tts-tam exists. See scripts/export-tamil-tts.py.
|
||||
env.localModelPath = path.join(ROOT, 'assets/tts');
|
||||
|
||||
const STT_MODEL = process.env.STT_MODEL || 'onnx-community/whisper-base';
|
||||
|
||||
/** TTS voice per language. Tamil is local; English comes from the Hub. */
|
||||
const VOICES = {
|
||||
ta: { id: 'mms-tts-tam', local: true },
|
||||
en: { id: 'Xenova/mms-tts-eng', local: false },
|
||||
};
|
||||
|
||||
export const LANGUAGES = [
|
||||
{ code: 'auto', label: 'Auto-detect', native: 'Auto' },
|
||||
{ code: 'ta', label: 'Tamil', native: 'தமிழ்' },
|
||||
{ code: 'en', label: 'English', native: 'English' },
|
||||
];
|
||||
|
||||
const cache = new Map();
|
||||
let sttPromise = null;
|
||||
|
||||
/**
|
||||
* Models load on first use, not at boot. A CRM restart should not wait ~10 s
|
||||
* for speech models that most sessions never touch.
|
||||
*/
|
||||
async function loadOnce(key, build) {
|
||||
if (!cache.has(key)) {
|
||||
const t0 = Date.now();
|
||||
cache.set(key, build().then((m) => {
|
||||
logger.info(`🔊 loaded ${key} in ${((Date.now() - t0) / 1000).toFixed(1)}s`);
|
||||
return m;
|
||||
}).catch((e) => {
|
||||
cache.delete(key); // let the next attempt retry
|
||||
throw e;
|
||||
}));
|
||||
}
|
||||
return cache.get(key);
|
||||
}
|
||||
|
||||
export async function getSTT() {
|
||||
if (!sttPromise) {
|
||||
sttPromise = loadOnce(STT_MODEL, () =>
|
||||
// q8 is the right call for Whisper — unlike VITS it is big enough that
|
||||
// quantisation is a clear win.
|
||||
pipeline('automatic-speech-recognition', STT_MODEL, { dtype: 'q8' }),
|
||||
).catch((e) => { sttPromise = null; throw e; });
|
||||
}
|
||||
return sttPromise;
|
||||
}
|
||||
|
||||
export async function getTTS(lang = 'ta') {
|
||||
const voice = VOICES[lang] || VOICES.ta;
|
||||
const prev = env.allowRemoteModels;
|
||||
try {
|
||||
// Local folders must not be looked up on the Hub, and vice versa.
|
||||
env.allowRemoteModels = !voice.local;
|
||||
return await loadOnce(`tts:${voice.id}`, () =>
|
||||
pipeline('text-to-speech', voice.id, { dtype: 'fp32' }),
|
||||
);
|
||||
} finally {
|
||||
env.allowRemoteModels = prev;
|
||||
}
|
||||
}
|
||||
|
||||
export const supportsTTS = (lang) => Boolean(VOICES[lang]);
|
||||
|
||||
/** Warm the models the deployment actually expects to use. */
|
||||
export async function warmup(langs = ['ta', 'en']) {
|
||||
try {
|
||||
await getSTT();
|
||||
for (const l of langs) await getTTS(l);
|
||||
logger.info('🔊 speech models warm');
|
||||
} catch (e) {
|
||||
logger.warn(`speech warmup failed (will retry on first use): ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function speechStatus() {
|
||||
return {
|
||||
stt_model: STT_MODEL,
|
||||
tts_voices: Object.fromEntries(Object.entries(VOICES).map(([k, v]) => [k, v.id])),
|
||||
loaded: [...cache.keys()],
|
||||
languages: LANGUAGES,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user