diff --git a/src/models/index.js b/src/models/index.js index 1bcc29b..605d2f2 100644 --- a/src/models/index.js +++ b/src/models/index.js @@ -10,8 +10,6 @@ redis.client = Redis.createClient({ }); redis.ensureGroup = async (stream, group) => { - await redis.client.connect().catch(console.error); - try { await redis.client.xGroupCreate(stream, group, "0", {MKSTREAM: true}); @@ -23,7 +21,19 @@ redis.ensureGroup = async (stream, group) => { console.error(err); } } - await redis.client.quit(); +} + +redis.sendTranscriptEvent = async (streamKey, device_id, text) => { + try { + await redis.client.xAdd(streamKey, "*", { + text, + device_id: device_id, + }); + + console.log("Transcript added to stream"); + } catch (err) { + console.error(err); + } } module.exports = { diff --git a/src/server.js b/src/server.js index 39122c0..bdb4b2e 100644 --- a/src/server.js +++ b/src/server.js @@ -14,22 +14,7 @@ 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) => { - await redis.client.xAdd( - streamKey, - '*', - { - text: text, - device_id: device_id - } - ) - - console.log("Transcript added to stream"); -} +const { handlePythonOutput } = require("./scripts/py/pythonOutput"); // Read Redis Stream group async function processor (group, consumer, streamKey) { @@ -57,33 +42,12 @@ async function main() { await redis.client.connect().catch(console.error); 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'); + py.stdout.on("data", (data) => { + handlePythonOutput(data.toString(), async (msg) => { + if (msg.event === "text" || msg.event === "final") { + await redis.sendTranscriptEvent("transcripts", config.DEVICE_ID, msg.text); } - } catch { - console.log('[PY]', line); - } - } + }); }); py.stderr.on("data", (data) => {