-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (111 loc) · 3.1 KB
/
Copy pathserver.js
File metadata and controls
122 lines (111 loc) · 3.1 KB
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
117
118
119
120
121
122
#! /usr/bin/env node
require("dotenv").config({ silent: true });
const axios = require("axios");
const path = require("path");
const express = require("express");
const publicPath = path.join(__dirname, "build");
const app = express();
const TextToSpeechV1 = require("ibm-watson/text-to-speech/v1.js");
const { IamAuthenticator, IamTokenManager } = require("ibm-watson/auth");
const cors = require("cors");
const bodyParser = require("body-parser");
app.use(bodyParser.json({ limit: "10mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "10mb", extended: true }));
// app.use(bodyParser.raw());
app.use(express.static(publicPath));
app.use(cors());
//Host react application on root url
app.get("/", (req, res) => {
res.sendFile(path.join(publicPath, "index.html"));
});
//Text To Speech API block begins
const textToSpeech = new TextToSpeechV1({
version: "2018-04-05",
authenticator: new IamAuthenticator({
apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY || "type-key-here",
}),
url: process.env.TEXT_TO_SPEECH_URL,
});
const getFileExtension = (acceptQuery) => {
const accept = acceptQuery || "";
switch (accept) {
case "audio/ogg;codecs=opus":
case "audio/ogg;codecs=vorbis":
return "ogg";
case "audio/wav":
return "wav";
case "audio/mpeg":
return "mpeg";
case "audio/webm":
return "webm";
case "audio/flac":
return "flac";
default:
return "mp3";
}
};
/**
* Pipe the synthesize method
*/
app.get("/api/v1/synthesize", async (req, res, next) => {
try {
const { result } = await textToSpeech.synthesize(req.query);
const transcript = result;
transcript.on("response", (response) => {
if (req.query.download) {
response.headers[
"content-disposition"
] = `attachment; filename=transcript.${getFileExtension(
req.query.accept
)}`;
}
});
transcript.on("error", next);
transcript.pipe(res);
} catch (error) {
res.send(error);
}
});
//Text to Speech API block ends
//Speech to Text API block begins
const tokenManagerSpeechToText = new IamTokenManager({
apikey: process.env.SPEECH_TO_TEXT_IAM_APIKEY || "<iam_apikey>",
});
const serviceUrl = process.env.SPEECH_TO_TEXT_URL;
app.get("/api/v1/credentials", async (req, res, next) => {
try {
const accessToken = await tokenManagerSpeechToText.getToken();
res.json({
accessToken,
serviceUrl,
});
} catch (err) {
next(err);
}
});
//Speech to Text API block ends
app.post("/api/v1/predict", (req, res) => {
axios
.post(
"http://ec2-54-164-171-127.compute-1.amazonaws.com:3000/" + "predict",
req.body
)
.then((response) => {
console.log(response.data);
res.send(response.data);
})
.catch((e) => {
// console.log("errrt",e);
res.sendStatus(500);
});
// res.sendStatus(200);
});
//Host react application on root url
app.get("*", (req, res) => {
res.sendFile(path.join(publicPath, "index.html"));
});
//Run application
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log("Server running on port: %d", port);
});