WS & TTS urls configurable
This commit is contained in:
@ -2,17 +2,27 @@ const WebSocket = require('ws');
|
||||
const wss = new WebSocket.Server({ port: 8081 });
|
||||
console.log('WebSocket server started on port 8081');
|
||||
|
||||
|
||||
|
||||
//load TTS_BACHEND_URL from .env file
|
||||
//require('dotenv').config();
|
||||
|
||||
console.log(process.env)
|
||||
console.log(process.env.TTS_BACKEND_URL)
|
||||
console.log(process.env.WS_URL)
|
||||
|
||||
//we use https://hub.docker.com/r/onerahmet/openai-whisper-asr-webservice to transcribe the audio
|
||||
//docker run -p 9009:9009 -d onerahmet/openai-whisper-asr-webservice
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
console.log('Client ' + ws._socket.remoteAddress + ' connected');
|
||||
ws.on('message', (data) => {
|
||||
console.log('Received data from client: ' + data.length + ' bytes');
|
||||
//show the size of the audio data as 0.000 MB
|
||||
console.log('Received data from client: ' + (data.length / 1024 / 1024).toFixed(3) + ' MB');
|
||||
var request = require('request');
|
||||
var formData = {
|
||||
task: 'transcribe',
|
||||
language: 'en-US',
|
||||
language: 'en-US', //bg-BG|en-US
|
||||
output: 'json',
|
||||
audio_file: {
|
||||
value: data,
|
||||
@ -22,15 +32,28 @@ wss.on('connection', (ws) => {
|
||||
}
|
||||
}
|
||||
};
|
||||
request.post({url:'http://192.168.0.10:9009/asr', formData: formData}, function optionalCallback(err, httpResponse, body) {
|
||||
//save the audio data to a file to /rec folder
|
||||
var fs = require('fs');
|
||||
var timestampfilename = Date.now();
|
||||
fs.writeFile('./rec/audio' + timestampfilename + '.ogg', data, function (err) {
|
||||
|
||||
|
||||
//fs.writeFile('audio' + timestampfilename + '.ogg', data, function (err) {
|
||||
if (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
console.log('Audio data saved to audio.ogg');
|
||||
});
|
||||
|
||||
|
||||
|
||||
request.post({url:'http://192.168.0.10:9008/asr', formData: formData}, function optionalCallback(err, httpResponse, body) {
|
||||
if (err) {
|
||||
return console.error('upload failed:', err);
|
||||
}
|
||||
console.log('Upload successful! Server responded with:', body);
|
||||
ws.send(">>: " + body);
|
||||
console.log('Whisper decoded:', body);
|
||||
ws.send(body);
|
||||
});
|
||||
|
||||
ws.send("Processing audio...");
|
||||
});
|
||||
});
|
||||
|
||||
@ -50,6 +73,13 @@ app.get('/', (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'client.html'));
|
||||
});
|
||||
|
||||
|
||||
//get WS url from .env file
|
||||
app.get('/wsurl', (req, res) => {
|
||||
res.send(process.env.WS_URL, 200, {'Content-Type': 'text/plain'});
|
||||
});
|
||||
|
||||
|
||||
app.listen(8080, () => {
|
||||
console.log('Server listening on port 8080');
|
||||
});
|
||||
|
Reference in New Issue
Block a user