-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoquihttp.js
executable file
·348 lines (255 loc) · 9.42 KB
/
coquihttp.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env node
const path = require('path')
const http = require('http')
const url = require('url')
const { transcriptProcess } = require('./process_transcript')
const { getArgs } = require('../lib/getArgs')
const { unixTimeMsecs, setTimer, getTimer } = require('../lib/chronos')
const { rtf, duration } = require('../lib/audioutils')
const { log } = require('../lib/log')
const { info } = require('../lib/info')
const HTTP_PATH = '/transcript'
const HTTP_PORT = 3000
/**
* Module global variables
*/
let debug
let activeRequests = 0
let modelName
let scorerName
let pathRegexp
function helpAndExit(programName) {
console.log('Simple demo HTTP JSON server, loading a Coqui STT engine model to transcript speeches.')
console.log( info() )
console.log()
console.log('The server has a single endpoint:')
console.log()
console.log(' HTTP POST /transcript')
console.log(' The request query string arguments contain parameters,')
console.log(' the request body contains the WAV file name to be submitted to the server.')
console.log()
console.log('Usage:')
console.log()
console.log(` ${programName} --model=<model file>.pbmm> \\ `)
console.log(' --scorer=<scorer file>.scorer> \\ ')
console.log(` [--port=<server port number. Default: ${HTTP_PORT}>] \\ `)
console.log(` [--path=<server endpoint path. Default: ${HTTP_PATH}>] \\ `)
console.log()
console.log('Server settings example:')
console.log()
console.log(' stdout includes minimal info, default port number is 3000')
console.log(` node ${programName} --model=../models/coqui-stt-0.9.3-models.pbmm --scorer=../models/coqui-stt-0.9.3-models.scorer`)
console.log()
console.log('Client requests examples:')
console.log()
console.log(' POST /transcript - body includes the speech file')
console.log()
console.log(' curl -s \\ ')
console.log(' -X POST \\ ')
console.log(' -H "Accept: application/json" \\ ')
console.log(' -H "Content-Type: audio/wav" \\ ')
console.log(' --data-binary="@../audio/2830-3980-0043.wav" \\ ')
console.log(' "http://localhost:3000/transcript?id=1620060067830&model=coqui-stt-0.9.3-models.pbmm&scorer=coqui-stt-0.9.3-models.scorer"')
console.log()
process.exit(1)
}
/**
* validateArgs
* command line parsing
*
* @param {String} args
* @param {String} programName
*
* @returns {SentenceAndAttributes}
* @typedef {Object} SentenceAndAttributes
* @property {String} language
*
*/
function validateArgs(args, programName) {
// model is a cli mandatory argument
const modelName = args.model
const scorerName = args.scorer
if ( !modelName || !scorerName )
helpAndExit(programName)
const serverPort = !args.port ? HTTP_PORT : args.port
const serverPath = !args.path ? HTTP_PATH : args.path
return { modelName, scorerName, serverPort, serverPath }
}
/**
* errorResponse
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
*/
function errorResponse(message, statusCode, res) {
res.statusCode = statusCode
res.end(`{"error":"${message}"}`)
log(message, 'ERROR')
}
// return JSON data structure
function responseJson(id, latency, text, rtf, duration, res) {
res.setHeader('Content-Type', 'application/json')
return JSON.stringify({
//... { request: JSON.stringify(queryObject) },
... { id },
... { latency },
... { duration },
... { rtf },
... { text }
})
}
function responseText(text, res) {
res.setHeader('Content-Type', 'text/plain')
return text
}
function successResponse(requestId, json, res) {
log(`response ${requestId} ${json}`)
res.end(json)
}
/**
* get request body
*
* @function
* @async
*
* @param {Object} req
* @return {Promise<Buffer>}
*/
function getRequestBody(req) {
return new Promise( (resolve) => {
let speechAsBuffer = Buffer.alloc(0)
req.on('data', (chunk) => speechAsBuffer = Buffer.concat([speechAsBuffer, chunk]) )
// all the body is received
req.on('end', () => resolve(speechAsBuffer) )
})
}
async function requestListener(req, res) {
//if ( !req.url.match(/^\/transcript/) )
if ( !req.url.match(pathRegexp) )
return errorResponse(`path not allowed ${req.url}`, 405, res)
//
// if request header accept attribute is 'text/plain'
// response body is text, otherwise
// response body is 'application/json'
//
const requestAcceptText = (req.headers.accept === 'text/plain') ? true : false
// HTTP POST /transcript
if (req.method === 'POST') {
// get request headers attribute: "content-type"
const { 'content-type': contentType } = req.headers
if (debug)
log(`request POST content type ${contentType}`, 'DEBUG')
// get request query string arguments
const queryObject = url.parse(req.url,true).query
const requestedModelName = queryObject.model
const requestedScorerName = queryObject.scorer
const requestedId = queryObject.id
// set id to the is query string argument
// if id argument is not present in the quesy string,
// set the id with current timestamp in msecs.
const currentTime = unixTimeMsecs()
const id = requestedId ? requestedId : currentTime
// log POST request
log(`request POST ${id} ${'speechBuffer'} ${requestedModelName? requestedModelName: ''} ${requestedScorerName? requestedScorerName: ''}`, null, currentTime)
/*
// TODO
// if query arguments "model" and "scorer" are specified in the client request,
// they must be equal to the model name and the scorer name loaded by the server
if ( requestedModelName && (requestedModelName !== modelName) )
return errorResponse(`id ${id} model ${requestedModelName} unknown`, 404, res)
if ( requestedScorerName && (requestedScorerName !== scorerName) )
return errorResponse(`id ${id} scorer ${requestedScorerName} unknown`, 404, res)
*/
// get request body binary data, containing speech WAV file
// TODO Validation of body
const attachedFileTime = setTimer()
const speechAsBuffer = await getRequestBody(req)
if (debug)
log(`HTTP POST attached file elapsed ${getTimer(attachedFileTime)}ms`, 'DEBUG')
return responseTranscriptPost(id, speechAsBuffer, modelName, scorerName, requestAcceptText, res)
}
// all other HTTP methods
else
return errorResponse(`method not allowed ${req.method}`, 405, res)
}
async function responseTranscriptPost(id, buffer, model, scorer, acceptText, res) {
if (debug)
log(`Body Buffer length ${Buffer.byteLength(buffer)}`)
try {
if (debug) {
// new thread started, increment global counter of active thread running
activeRequests++
log(`active requests ${activeRequests}`, 'DEBUG')
}
const transcriptTime = setTimer()
// speech recognition of an audio file
const text = await transcriptProcess( model, scorer, buffer )
if (debug) {
// thread finished, decrement global counter of active thread running
activeRequests--
log(`active requests ${activeRequests}`, 'DEBUG')
}
const latency = getTimer(transcriptTime)
// audio length in milliseconds
const durationMsecs = Math.floor(duration(buffer) * 1000)
// calculate realtime factor
const realtimeFactor = rtf(latency, durationMsecs)
if (debug)
log(`latency ${id} ${latency}ms`, 'DEBUG')
const body = acceptText ?
responseText(text, res) :
responseJson(id, latency, text, realtimeFactor, durationMsecs, res)
return successResponse(id, body, res)
}
catch (error) {
return errorResponse(`id ${id} transcript function ${error}`, 415, res)
}
}
function shutdown(signal) {
log(`${signal} received`)
log('Shutdown done')
process.exit(0)
}
async function main() {
// get command line arguments
const { args } = getArgs()
//const programName = path.basename(__filename, '.js')
const programName = 'httpServer'
const validatedArgs = validateArgs(args, programName )
const { serverPort, serverPath } = validatedArgs
// set modelName and scorerName as a global variable
;( { modelName, scorerName } = validatedArgs )
pathRegexp = new RegExp('^' + serverPath )
log( info() )
log(`Model name: ${modelName}`)
log(`Scorer name: ${scorerName}`)
log(`HTTP server port: ${serverPort}`)
log(`HTTP server path: ${serverPath}`)
/*
log(`wait loading Coqui STT model ${modelName} and scorer ${scorerName}scorer. Be patient)`);
setTimer('loadModel')
// create a Coqui STT runtime model
model = loadModel(modelDirectory)
log(`Coqui STT model loaded in ${getTimer('loadModel')} msecs`)
*/
// create the HTTP server instance
const server = http.createServer( async (req, res) => requestListener(req, res) )
// listen incoming client requests
server.listen( serverPort, () => {
log(`server ${path.basename(__filename)} running at http://localhost:${serverPort}`)
log(`endpoint http://localhost:${serverPort}${serverPath}`)
log('press Ctrl-C to shutdown')
log('ready to listen incoming requests')
})
// shutdown management
process.on('SIGTERM', shutdown )
process.on('SIGINT', shutdown )
//
// Shutdown on uncaughtException
// https://flaviocopes.com/node-exceptions/
//
process.on('uncaughtException', (err) => {
log(`there was an uncaught error: ${err}`, 'FATAL')
shutdown('uncaughtException')
})
}
main()