From 1f8c41e100f721dc1d0ddc873a83434a7fbadd2e Mon Sep 17 00:00:00 2001 From: Apher Date: Mon, 6 Jul 2026 23:02:27 +0000 Subject: [PATCH] Merge transcribe code with server.js --- src/scripts/py/transcribe.js | 46 ---------------------------------- src/server.js | 48 ++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 48 deletions(-) delete mode 100644 src/scripts/py/transcribe.js diff --git a/src/scripts/py/transcribe.js b/src/scripts/py/transcribe.js deleted file mode 100644 index 50137ca..0000000 --- a/src/scripts/py/transcribe.js +++ /dev/null @@ -1,46 +0,0 @@ -const { spawn } = require('child_process'); -const fs = require('fs'); - -const config = JSON.parse(fs.readFileSync('./config.json', 'utf8')); - -const py = spawn('python', ['wakeword.py', JSON.stringify(config)], { - stdio: ['ignore', 'pipe', 'pipe'] -}); - -let buffer = ''; - -py.stdout.on('data', (data) => { - buffer += data.toString(); - const lines = buffer.split('\n'); - buffer = lines.pop(); - - for (const line of lines) { - if (!line.trim()) continue; - - try { - const msg = JSON.parse(line); - - if (msg.event === 'ready') { - console.log('Python ready'); - } else if (msg.event === 'wakeword') { - console.log(`Wakeword detected: ${msg.model} (${msg.score})`); - } else if (msg.event === 'partial') { - console.log('Partial:', msg.text); - } else if (msg.event === 'text' || msg.event === 'final') { - console.log('Transcript:', msg.text); - } else if (msg.event === 'done') { - console.log('Back to wakeword mode'); - } - } catch { - console.log('[PY]', line); - } - } -}); - -py.stderr.on('data', (data) => { - console.error('[PY ERR]', data.toString()); -}); - -py.on('close', (code) => { - console.log('Python exited with code', code); -}); diff --git a/src/server.js b/src/server.js index 1536db7..96de790 100644 --- a/src/server.js +++ b/src/server.js @@ -3,10 +3,19 @@ if (process.env.NODE_ENV !== "production") { require("dotenv").config(); } +const { spawn } = require('child_process'); + const models = require("./models"); const redis = models.redis; const config = require("./config"); +const transConfig = require("./config/transcribe.config") + +const py = spawn('python', ['./src/scripts/py/transcribe.py', JSON.stringify(transConfig)], { + stdio: ['ignore', 'pipe', 'pipe'] +}); + +let buffer = ''; // Send Speech to Text to Redis Stream const sendTranscriptEvent = async (streamKey, device_id, text) => { @@ -26,8 +35,6 @@ const sendTranscriptEvent = async (streamKey, device_id, text) => { const processor = async (group, consumer, streamKey) => { await redis.client.connect().catch(console.error); - sendTranscriptEvent("transcripts", config.DEVICE_ID, "TEST"); - while (true) { const res = await redis.client.xReadGroup( group, @@ -53,6 +60,43 @@ const processor = async (group, consumer, streamKey) => { const main = async () => { await redis.ensureGroup("commands", 'assistant'); + py.stdout.on('data', (data) => { + buffer += data.toString(); + const lines = buffer.split('\n'); + buffer = lines.pop(); + + for (const line of lines) { + if (!line.trim()) continue; + + try { + const msg = JSON.parse(line); + + if (msg.event === 'ready') { + console.log('Python ready'); + } else if (msg.event === 'wakeword') { + console.log(`Wakeword detected: ${msg.model} (${msg.score})`); + } else if (msg.event === 'partial') { + console.log('Partial:', msg.text); + } else if (msg.event === 'text' || msg.event === 'final') { + console.log('Transcript:', msg.text); + sendTranscriptEvent("transcripts", config.DEVICE_ID, msg.text); + } else if (msg.event === 'done') { + console.log('Back to wakeword mode'); + } + } catch { + console.log('[PY]', line); + } + } + }); + + py.stderr.on('data', (data) => { + console.error('[PY ERR]', data.toString()); + }); + + py.on('close', (code) => { + console.log('Python exited with code', code); + }); + processor('assistant', `${config.DEVICE_ID}`, "commands"); }