-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (87 loc) · 3.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const fs = require('fs');
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const AssistantV2 = require('ibm-watson/assistant/v2');
const express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 80;
app.use(cors())
app.use(fileUpload());
const speechToText = new SpeechToTextV1({
authenticator: new IamAuthenticator({
apikey: '37C9T7XWUjYDiG7YyLSsxu_YXtakx6sSjI5JBHXs_ckF',
}),
serviceUrl: 'https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/8e0682de-d7c2-4852-ada0-277fac27f84b',
});
const assistant = new AssistantV2({
version: '2020-04-01',
authenticator: new IamAuthenticator({
apikey: 'abDuxiF0X6RFS67nwOsVSggBq0uiRgEAssuzqLx-9zjA',
}),
serviceUrl: 'https://api.us-south.assistant.watson.cloud.ibm.com/instances/e0ef648b-38f9-43e3-b4a9-0f0773e97094',
});
app.get('/api', (req, res) => {
res.send('Hello World!')
});
app.post('/api/uploadFile', (req, res) => {
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
let sampleFile = req.files.file;
// Use the mv() method to place the file somewhere on your server
sampleFile.mv( __dirname + '/audios/' + 'temp.mp3', async (err) => {
if (err)
return res.status(500).send(err);
const response = await recognize();
// If it's not undefined, continue to next step.
if(!response){
return res.status(200).send(undefined);
}
const result = await fetchMessage(response);
if(result === "Don't know how to get started? Try to say hello world!")
return res.status(200).send("Some secret here" + response);
res.status(200).send(result);
});
});
app.post('/text/getResponse', async (req, res) => {
const text = req.body.userInput;
if(!text){
return res.status(500).send("Invalid input here!");
}
const result = await fetchMessage(text);
res.status(200).send(result);
});
app.post('/text/onlyOnePart', async (req, res) => {
const text = req.query.userInput;
if(!text){
return res.status(500).send("Invalid input here!");
}
const result = await fetchMessage(text);
res.status(200).send(result);
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
const recognize = async () => {
const recognizeParams = {
audio: fs.createReadStream('audios/temp.mp3'),
contentType: 'audio/mp3',
wordAlternativesThreshold: 0.9
};
const result = await speechToText.recognize(recognizeParams);
return result.result?.results[0]?.alternatives[0]?.transcript;
}
const fetchMessage = async (msg) => {
const result = await assistant.messageStateless({
assistantId: '6213741c-8276-4b5e-a9a9-10f318472033',
input: {
'message_type': 'text',
'text': msg
}
})
console.log(msg);
return (result.result?.output?.generic[0].text);
}