This repository has been archived by the owner on Jul 20, 2019. It is now read-only.
forked from daniel-bolanos/speech-to-text-websockets-python
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsttClient.py
executable file
·406 lines (346 loc) · 14.8 KB
/
sttClient.py
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#
# Copyright IBM Corp. 2014
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Daniel Bolanos
# Date: 2015
# coding=utf-8
import json # json
import threading # multi threading
import os # for listing directories
import queue as Queue # queue used for thread syncronization
import sys # system calls
import argparse # for parsing arguments
import base64 # necessary to encode in base64
# # according to the RFC2045 standard
import requests # python HTTP requests library
# WebSockets
from autobahn.twisted.websocket import WebSocketClientProtocol, \
WebSocketClientFactory, connectWS
from twisted.python import log
from twisted.internet import ssl, reactor
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
class Utils:
@staticmethod
def getAuthenticationToken(hostname, serviceName, username, password):
fmt = hostname + "{0}/authorization/api/v1/token?url={0}/{1}/api"
uri = fmt.format(hostname, serviceName)
uri = uri.replace("wss://", "https://").replace("ws://", "https://")
print(uri)
auth = (username, password)
headers = {'Accept': 'application/json'}
resp = requests.get(uri, auth=auth, verify=False, headers=headers,
timeout=(30, 30))
print(resp.text)
jsonObject = resp.json()
return jsonObject['token']
class WSInterfaceFactory(WebSocketClientFactory):
def __init__(self, queue, summary, dirOutput, contentType, model,
url=None, headers=None, debug=None):
WebSocketClientFactory.__init__(self, url=url, headers=headers)
self.queue = queue
self.summary = summary
self.dirOutput = dirOutput
self.contentType = contentType
self.model = model
self.queueProto = Queue.Queue()
self.openHandshakeTimeout = 10
self.closeHandshakeTimeout = 10
# start the thread that takes care of ending the reactor so
# the script can finish automatically (without ctrl+c)
endingThread = threading.Thread(target=self.endReactor, args=())
endingThread.daemon = True
endingThread.start()
def prepareUtterance(self):
try:
utt = self.queue.get_nowait()
self.queueProto.put(utt)
return True
except Queue.Empty:
print("getUtterance: no more utterances to process, queue is "
"empty!")
return False
def endReactor(self):
self.queue.join()
print("about to stop the reactor!")
reactor.stop()
# this function gets called every time connectWS is called (once
# per WebSocket connection/session)
def buildProtocol(self, addr):
try:
utt = self.queueProto.get_nowait()
proto = WSInterfaceProtocol(self, self.queue, self.summary,
self.dirOutput, self.contentType)
proto.setUtterance(utt)
return proto
except Queue.Empty:
print("queue should not be empty, otherwise this function should "
"not have been called")
return None
# WebSockets interface to the STT service
#
# note: an object of this class is created for each WebSocket
# connection, every time we call connectWS
class WSInterfaceProtocol(WebSocketClientProtocol):
def __init__(self, factory, queue, summary, dirOutput, contentType):
self.factory = factory
self.queue = queue
self.summary = summary
self.dirOutput = dirOutput
self.contentType = contentType
self.packetRate = 20
self.listeningMessages = 0
self.timeFirstInterim = -1
self.bytesSent = 0
self.chunkSize = 2000 # in bytes
super(self.__class__, self).__init__()
print(dirOutput)
print("contentType: {} queueSize: {}".format(self.contentType,
self.queue.qsize()))
def setUtterance(self, utt):
self.uttNumber = utt[0]
self.uttFilename = utt[1]
self.summary[self.uttNumber] = {"hypothesis": "",
"status": {"code": "", "reason": ""}}
self.fileJson = "{}/{}.json.txt".format(self.dirOutput, self.uttNumber)
try:
os.remove(self.fileJson)
except OSError:
pass
# helper method that sends a chunk of audio if needed (as required
# what the specified pacing is)
def maybeSendChunk(self, data):
def sendChunk(chunk, final=False):
self.bytesSent += len(chunk)
self.sendMessage(chunk, isBinary=True)
if final:
self.sendMessage(b'', isBinary=True)
if (self.bytesSent + self.chunkSize >= len(data)):
if (len(data) > self.bytesSent):
sendChunk(data[self.bytesSent:len(data)], True)
return
sendChunk(data[self.bytesSent:self.bytesSent + self.chunkSize])
self.factory.reactor.callLater(0.01, self.maybeSendChunk, data=data)
return
def onConnect(self, response):
print("onConnect, server connected: {}".format(response.peer))
def onOpen(self):
print("onOpen")
data = {"action": "start",
"content-type": str(self.contentType),
"continuous": True,
"interim_results": True,
"inactivity_timeout": 600,
'max_alternatives': 3,
'timestamps': True,
'word_confidence': True}
print("sendMessage(init)")
# send the initialization parameters
self.sendMessage(json.dumps(data).encode('utf8'))
# start sending audio right away (it will get buffered in the
# STT service)
print(self.uttFilename)
with open(str(self.uttFilename), 'rb') as f:
self.bytesSent = 0
dataFile = f.read()
self.maybeSendChunk(dataFile)
print("onOpen ends")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print(u"Text message received: {0}".format(payload.decode('utf8')))
# if uninitialized, receive the initialization response
# from the server
jsonObject = json.loads(payload.decode('utf8'))
if 'state' in jsonObject:
self.listeningMessages += 1
if self.listeningMessages == 2:
print("sending close 1000")
# close the connection
self.sendClose(1000)
# if in streaming
elif 'results' in jsonObject:
jsonObject = json.loads(payload.decode('utf8'))
hypothesis = ""
# empty hypothesis
if len(jsonObject['results']) == 0:
print("empty hypothesis!")
# regular hypothesis
else:
# dump the message to the output directory
jsonObject = json.loads(payload.decode('utf8'))
with open(self.fileJson, "a") as f:
f.write(json.dumps(jsonObject, indent=4,
sort_keys=True))
res = jsonObject['results'][0]
hypothesis = res['alternatives'][0]['transcript']
bFinal = (res['final'] is True)
if bFinal:
print('final hypothesis: "' + hypothesis + '"')
self.summary[self.uttNumber]['hypothesis'] += hypothesis
else:
print('interim hyp: "' + hypothesis + '"')
def onClose(self, wasClean, code, reason):
print("onClose")
print("WebSocket connection closed: {0}, code: {1}, clean: {2}, "
"reason: {0}".format(reason, code, wasClean))
self.summary[self.uttNumber]['status']['code'] = code
self.summary[self.uttNumber]['status']['reason'] = reason
# create a new WebSocket connection if there are still
# utterances in the queue that need to be processed
self.queue.task_done()
if not self.factory.prepareUtterance():
return
# SSL client context: default
if self.factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
connectWS(self.factory, contextFactory)
# function to check that a value is a positive integer
def check_positive_int(value):
ivalue = int(value)
if ivalue < 1:
raise argparse.ArgumentTypeError(
'"%s" is an invalid positive int value' % value)
return ivalue
# function to check the credentials format
def check_credentials(credentials):
elements = credentials.split(":")
if len(elements) == 2:
return elements
else:
raise argparse.ArgumentTypeError(
'"%s" is not a valid format for the credentials ' % credentials)
if __name__ == '__main__':
# parse command line parameters
parser = argparse.ArgumentParser(
description=('client to do speech recognition using the WebSocket '
'interface to the Watson STT service'))
parser.add_argument(
'-credentials', action='store', dest='credentials',
help="Basic Authentication credentials in the form 'username:password'",
required=True, type=check_credentials)
parser.add_argument(
'-in', action='store', dest='fileInput', default='./recordings.txt',
help='text file containing audio files')
parser.add_argument(
'-out', action='store', dest='dirOutput', default='./output',
help='output directory')
parser.add_argument(
'-type', action='store', dest='contentType', default='audio/wav',
help='audio content type, for example: \'audio/l16; rate=44100\'')
parser.add_argument(
'-model', action='store', dest='model', default='en-US_BroadbandModel',
help='STT model that will be used')
parser.add_argument(
'-amcustom', action='store', dest='am_custom_id', default=None,
help='id of the acoustic model customization that will be used', required=False)
parser.add_argument(
'-lmcustom', action='store', dest='lm_custom_id', default=None,
help='id of the language model customization that will be used', required=False)
parser.add_argument(
'-threads', action='store', dest='threads', default='1',
help='number of simultaneous STT sessions', type=check_positive_int)
parser.add_argument(
'-optout', action='store_true', dest='optOut',
help=('specify opt-out header so user data, such as speech and '
'hypotheses are not logged into the server'))
parser.add_argument(
'-tokenauth', action='store_true', dest='tokenauth',
help='use token based authentication')
args = parser.parse_args()
# create output directory if necessary
if os.path.isdir(args.dirOutput):
fmt = 'the output directory "{}" already exists, overwrite? (y/n)? '
while True:
answer = raw_input(fmt.format(args.dirOutput)).strip().lower()
if answer == "n":
sys.stderr.write("exiting...")
sys.exit()
elif answer == "y":
break
else:
os.makedirs(args.dirOutput)
# logging
log.startLogging(sys.stdout)
# add audio files to the processing queue
q = Queue.Queue()
lines = [line.rstrip('\n') for line in open(args.fileInput)]
fileNumber = 0
for fileName in lines:
print(fileName)
q.put((fileNumber, fileName))
fileNumber += 1
hostname = "stream.watsonplatform.net"
headers = {'X-WDC-PL-OPT-OUT': '1'} if args.optOut else {}
# authentication header
if args.tokenauth:
headers['X-Watson-Authorization-Token'] = (
Utils.getAuthenticationToken('https://' + hostname,
'speech-to-text',
args.credentials[0],
args.credentials[1]))
else:
auth = args.credentials[0] + ":" + args.credentials[1]
headers["Authorization"] = "Basic " + base64.b64encode(auth.encode()).decode('utf-8')
print(headers)
# create a WS server factory with our protocol
fmt = "wss://{}/speech-to-text/api/v1/recognize?model={}"
url = fmt.format(hostname, args.model)
if args.am_custom_id != None:
url += "&acoustic_customization_id=" + args.am_custom_id
if args.lm_custom_id != None:
url += "&customization_id=" + args.lm_custom_id
print(url)
summary = {}
factory = WSInterfaceFactory(q, summary, args.dirOutput, args.contentType,
args.model, url, headers, debug=False)
factory.protocol = WSInterfaceProtocol
for i in range(min(int(args.threads), q.qsize())):
factory.prepareUtterance()
# SSL client context: default
if factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
connectWS(factory, contextFactory)
reactor.run()
# dump the hypotheses to the output file
fileHypotheses = args.dirOutput + "/hypotheses.txt"
f = open(fileHypotheses, "w")
successful = 0
emptyHypotheses = 0
print(sorted(summary.items()))
counter = 0
for key, value in enumerate(sorted(summary.items())):
value = value[1]
if value['status']['code'] == 1000:
print('{}: {} {}'.format(key, value['status']['code'],
value['hypothesis'].encode('utf-8')))
successful += 1
if value['hypothesis'][0] == "":
emptyHypotheses += 1
else:
fmt = '{}: {status[code]} REASON: {status[reason]}'
print(fmt.format(key, **status))
f.write('{}: {}\n'.format(counter + 1, value['hypothesis'].encode('utf-8')))
counter += 1
f.close()
fmt = "successful sessions: {} ({} errors) ({} empty hypotheses)"
print(fmt.format(successful, len(summary) - successful, emptyHypotheses))