Commit 336d380c authored by Yoann HOUPERT's avatar Yoann HOUPERT

better return when error

parent b08a4eee
Pipeline #9847 passed with stage
in 1 minute and 11 seconds
......@@ -5,7 +5,6 @@
"isOffline": false,
"gstreamer": {
"host": "linsttpoc_kaldi_1",
"host2": "linsttcontroller_kaldi_1",
"port": "80",
"api" : {
"recognize" : "client/dynamic/recognize"
......@@ -20,7 +19,6 @@
},
"speechEnhancement": {
"host": "linsttpoc_speech-enhencement_1",
"host2": "linsttcontroller_speech-enhencement_1",
"port": "5000",
"api" : {
"upload" : "upload"
......
......@@ -18,37 +18,54 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const routes = require('express').Router();
const uuidv1 = require('uuid/v1');
const successCode = 200;
const badRequestCode = 400;
const interalServerCode = 500;
const routesFactory = config => {
/* eslint new-cap: ["error", { "capIsNew": false }] */
const routes = require('express').Router();
const uuidv1 = require('uuid/v1');
const enhancer = require('../controller/speech-enhancement')(config.speechEnhancement);
const stt = require('../controller/speech-to-text')(config.gstreamer);
const offline = require('../controller/offline')(config.offline);
routes.post('/api/transcript', (req, res) => {
if (!req.body) {
return res.status(400).send('No files were uploaded.');
return res.status(badRequestCode).send('No files were uploaded.');
}
enhancer.enhancement(req.body, (err, httpResponse, body) => {
enhancer.enhancement(req.body, (err, response, body) => {
if (err) {
return res.status(500).send('Error while enhancing audio.' + err);
return res.status(interalServerCode).json({module: 'Enhancement', error: 'Error during audio enhancing', info: err.toString()});
} else if (response.statusCode !== successCode) {
return res.status(interalServerCode).json({module: 'Enhancement', error: 'Error during audio enhancing'});
}
if (config.isOffline) {
offline.transcript(body, (err, httpResponse, body) => {
offline.transcript(body, (err, response, body) => {
if (err) {
return res.status(interalServerCode).json({module: 'Offline transcription', error: 'Error during audio transcription', info: err.toString()});
} else if (response === undefined || body === undefined || response.statusCode !== successCode) {
return res.status(interalServerCode).json({module: 'Offline transcription', error: 'Error during audio transcription'});
}
const json = JSON.parse(body);
if (json.status === 5) {
return res.status(500).send('Error while transcript the audio.');
} else if (err) {
return res.status(500).send('Error while transcript the audio : ' + err);
return res.status(interalServerCode).json({module: 'Offline transcription', error: 'Error during audio transcription'});
}
// Default Offline
const transcription = json.transcript;
// TODO use this when enhancement whill be integrate
// const transcription = JSON.parse(json.transcript)
const jsonResponse = {
status: 0,
hypotheses: [{
utterance: json.transcript
utterance: transcription
}],
id: uuidv1()
};
......@@ -56,18 +73,18 @@ const routesFactory = config => {
return res.status(200).json({message: 'transcript done', transcript: jsonResponse});
});
} else {
stt.transcript(body, (err, httpResponse, body) => {
if (err) {
return res.status(500).send('Error while transcript the audio.' + err);
stt.transcript(body, (err, response, body) => {
if (err || response === undefined || body === undefined || response.statusCode !== successCode) {
return res.status(interalServerCode).json({module: 'Online transcription', error: 'Error during audio transcription', info: err.toString()});
} else if (body.indexOf('No workers available') !== -1) {
return res.status(500).send('No worker available for the moment.');
return res.status(interalServerCode).json({module: 'Online transcription', error: 'No worker available for the moment.'});
}
const json = JSON.parse(body);
if (json.status === 5) {
return res.status(500).send('Error while transcript the audio.');
return res.status(interalServerCode).json({module: 'Online transcription', error: 'Error during audio transcription'});
}
return res.status(200).json({message: 'transcript done', transcript: json});
return res.status(successCode).json({message: 'Transcript done', transcript: json});
});
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment