Commit d7df6453 authored by Yoann HOUPERT's avatar Yoann HOUPERT
Browse files

reconstruct file body for enhancement API

parent 5585cf2d
Pipeline #6035 passed with stage
in 24 seconds
......@@ -17,19 +17,30 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const request = require('request');
const enhancement = (wavFile, speechEnhancement, enhancementlCallback) => {
const formData = {
wavFile: wavFile.data,
msg: '-n'
};
const enhancement = (wavFile, speechEnhancement, nameFile, enhancementlCallback) => {
const url = 'http://' + speechEnhancement.host + ':' + speechEnhancement.port + '/' + speechEnhancement.api.upload;
if (!speechEnhancement.debug) {
return request.post({url, form: formData}, enhancementlCallback);
}
fs.writeFile(nameFile + '.wav', wavFile.data, err => {
if (err) {
throw err;
}
const options = {
url,
headers: {
'content-type': 'multipart/form-data; boundary=---011000010111000001101001'
},
formData: {
wavFile: fs.createReadStream(nameFile + '.wav'),
msg: '-n'
}
};
request.post(options, enhancementlCallback);
});
return wavFile;
};
......
......@@ -22,7 +22,6 @@ const request = require('request');
const transcript = (audio, gstreamer, sttCallback) => {
const url = 'http://' + gstreamer.host + ':' + gstreamer.port + '/' + gstreamer.api.recognize;
// TODO until no docker-compose use debug mode
return request.post({url, body: audio}, sttCallback);
};
......
......@@ -18,6 +18,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const isFileValid = audio => {
if (audio.mimetype !== 'audio/wave') {
return false;
......@@ -25,6 +27,24 @@ const isFileValid = audio => {
return true;
};
const cleanFile = nameFile => {
fs.unlink(nameFile + '.wav', err => {
if (err) {
throw err;
}
});
};
const writeFile = (nameFile, data) => {
fs.writeFile('enhancementFile.wav', data, err => {
if (err) {
throw err;
}
});
};
module.exports = {
isFileValid
isFileValid,
cleanFile,
writeFile
};
......@@ -23,36 +23,38 @@ const routesFactory = config => {
const routes = require('express').Router();
const enhancer = require('../speech-enhancement');
const stt = require('../speech-to-text');
const control = require('../controller');
const util = require('../util');
routes.post('/api/transcript', (req, res) => {
if (!req.files) {
return res.status(400).send('No files were uploaded.');
}
if (!control.isFileValid(req.files.wav)) {
if (!util.isFileValid(req.files.wav)) {
return res.status(400).send('File is not valid.');
}
enhancer.enhancement(req.files.wav, config.speechEnhancement,
const nameFile = Math.random().toString(36).substring(7);
enhancer.enhancement(req.files.wav, config.speechEnhancement, nameFile,
(err, httpResponse, body) => {
if (err) {
return res.status(500).send('Error while enhancing audio.' + err);
}
let buff = new Buffer(body);
util.cleanFile(nameFile);
util.writeFile('enchancementFile', body);
// TODO Manage 'body' raw for kaldis api
stt.transcript(req.files.wav.data, config.gstreamer,
stt.transcript(body, config.gstreamer,
(err, httpResponse, body) => {
if (err) {
return res.status(500).send('Error while transcript the audio.' + err);
} else if(body.indexOf('No workers available') !== -1){
} else if (body.indexOf('No workers available') !== -1) {
return res.status(500).send('No worker available for the moment.');
}
const json = JSON.parse(body)
if(json.status === 5)
const json = JSON.parse(body);
if (json.status === 5) {
return res.status(500).send('Error while transcript the audio.');
}
return res.status(200).json({message: 'transcript done', transcript: json.hypotheses[0].utterance});
}
);
......
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