Merge transcribe code with server.js
This commit is contained in:
parent
8041d5123c
commit
1f8c41e100
@ -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);
|
||||
});
|
||||
@ -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");
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user