Move sendTranscriptEvent to redis object
This commit is contained in:
parent
14f5a85bd8
commit
990589a776
@ -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 = {
|
||||
|
||||
@ -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,34 +42,13 @@ 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');
|
||||
}
|
||||
} catch {
|
||||
console.log('[PY]', line);
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
py.stderr.on("data", (data) => {
|
||||
console.error("[PY ERR]", data.toString());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user