-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
__init__.py
667 lines (542 loc) · 22 KB
/
__init__.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# Copyright 2017 Mycroft AI Inc.
#
# 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.
#
import re
import json
from abc import ABCMeta, abstractmethod
from requests import post, put, exceptions
from speech_recognition import Recognizer
from queue import Queue
from threading import Thread
from mycroft.api import STTApi, HTTPError
from mycroft.configuration import Configuration
from mycroft.util.log import LOG
from mycroft.util.plugins import load_plugin
class STT(metaclass=ABCMeta):
"""STT Base class, all STT backends derive from this one. """
def __init__(self):
config_core = Configuration.get()
self.lang = str(self.init_language(config_core))
config_stt = config_core.get("stt", {})
self.config = config_stt.get(config_stt.get("module"), {})
self.credential = self.config.get("credential", {})
self.recognizer = Recognizer()
self.can_stream = False
@property
def available_languages(self) -> set:
"""Return languages supported by this STT implementation in this state
This property should be overridden by the derived class to advertise
what languages that engine supports.
Returns:
set: supported languages
"""
return set()
@staticmethod
def init_language(config_core):
"""Helper method to get language code from Mycroft config."""
lang = config_core.get("lang", "en-US")
langs = lang.split("-")
if len(langs) == 2:
return langs[0].lower() + "-" + langs[1].upper()
return lang
@abstractmethod
def execute(self, audio, language=None):
"""Implementation of STT functionallity.
This method needs to be implemented by the derived class to implement
the specific STT engine connection.
The method gets passed audio and optionally a language code and is
expected to return a text string.
Args:
audio (AudioData): audio recorded by mycroft.
language (str): optional language code
Returns:
str: parsed text
"""
class TokenSTT(STT, metaclass=ABCMeta):
def __init__(self):
super(TokenSTT, self).__init__()
self.token = str(self.credential.get("token"))
class GoogleJsonSTT(STT, metaclass=ABCMeta):
def __init__(self):
super(GoogleJsonSTT, self).__init__()
self.json_credentials = json.dumps(self.credential.get("json"))
class BasicSTT(STT, metaclass=ABCMeta):
def __init__(self):
super(BasicSTT, self).__init__()
self.username = str(self.credential.get("username"))
self.password = str(self.credential.get("password"))
class KeySTT(STT, metaclass=ABCMeta):
def __init__(self):
super(KeySTT, self).__init__()
self.id = str(self.credential.get("client_id"))
self.key = str(self.credential.get("client_key"))
class GoogleSTT(TokenSTT):
def __init__(self):
super(GoogleSTT, self).__init__()
def execute(self, audio, language=None):
self.lang = language or self.lang
return self.recognizer.recognize_google(audio, self.token, self.lang)
class GoogleCloudSTT(GoogleJsonSTT):
def __init__(self):
super(GoogleCloudSTT, self).__init__()
# override language with module specific language selection
self.lang = self.config.get('lang') or self.lang
def execute(self, audio, language=None):
self.lang = language or self.lang
return self.recognizer.recognize_google_cloud(audio,
self.json_credentials,
self.lang)
class WITSTT(TokenSTT):
def __init__(self):
super(WITSTT, self).__init__()
def execute(self, audio, language=None):
LOG.warning("WITSTT language should be configured at wit.ai settings.")
return self.recognizer.recognize_wit(audio, self.token)
class IBMSTT(TokenSTT):
"""
IBM Speech to Text
Enables IBM Speech to Text access using API key. To use IBM as a
service provider, it must be configured locally in your config file. An
IBM Cloud account with Speech to Text enabled is required (limited free
tier may be available). STT config should match the following format:
"stt": {
"module": "ibm",
"ibm": {
"credential": {
"token": "YOUR_API_KEY"
},
"url": "URL_FROM_SERVICE"
}
}
"""
def __init__(self):
super(IBMSTT, self).__init__()
def execute(self, audio, language=None):
if not self.token:
raise ValueError('API key (token) for IBM Cloud is not defined.')
url_base = self.config.get('url', '')
if not url_base:
raise ValueError('URL for IBM Cloud is not defined.')
url = url_base + '/v1/recognize'
self.lang = language or self.lang
supported_languages = [
'ar-AR', 'pt-BR', 'zh-CN', 'nl-NL', 'en-GB', 'en-US', 'fr-FR',
'de-DE', 'it-IT', 'ja-JP', 'ko-KR', 'es-AR', 'es-ES', 'es-CL',
'es-CO', 'es-MX', 'es-PE'
]
if self.lang not in supported_languages:
raise ValueError(
'Unsupported language "{}" for IBM STT.'.format(self.lang))
audio_model = 'BroadbandModel'
if audio.sample_rate < 16000 and not self.lang == 'ar-AR':
audio_model = 'NarrowbandModel'
params = {
'model': '{}_{}'.format(self.lang, audio_model),
'profanity_filter': 'false'
}
headers = {
'Content-Type': 'audio/x-flac',
'X-Watson-Learning-Opt-Out': 'true'
}
response = post(url, auth=('apikey', self.token), headers=headers,
data=audio.get_flac_data(), params=params)
if response.status_code == 200:
result = json.loads(response.text)
if result.get('error_code') is None:
if ('results' not in result or len(result['results']) < 1 or
'alternatives' not in result['results'][0]):
raise Exception(
'Transcription failed. Invalid or empty results.')
transcription = []
for utterance in result['results']:
if 'alternatives' not in utterance:
raise Exception(
'Transcription failed. Invalid or empty results.')
for hypothesis in utterance['alternatives']:
if 'transcript' in hypothesis:
transcription.append(hypothesis['transcript'])
return '\n'.join(transcription)
elif response.status_code == 401: # Unauthorized
raise Exception('Invalid API key for IBM Cloud.')
else:
raise Exception(
'Request to IBM Cloud failed. Code: {} Body: {}'.format(
response.status_code, response.text))
class YandexSTT(STT):
"""
Yandex SpeechKit STT
To use create service account with role 'editor' in your cloud folder,
create API key for account and add it to local mycroft.conf file.
The STT config will look like this:
"stt": {
"module": "yandex",
"yandex": {
"lang": "en-US",
"credential": {
"api_key": "YOUR_API_KEY"
}
}
}
"""
def __init__(self):
super(YandexSTT, self).__init__()
self.lang = self.config.get('lang') or self.lang
self.api_key = self.credential.get("api_key")
if self.api_key is None:
raise ValueError("API key for Yandex STT is not defined")
def execute(self, audio, language=None):
self.lang = language or self.lang
if self.lang not in ["en-US", "ru-RU", "tr-TR"]:
raise ValueError(
"Unsupported language '{}' for Yandex STT".format(self.lang))
# Select sample rate based on source sample rate
# and supported sample rate list
supported_sample_rates = [8000, 16000, 48000]
sample_rate = audio.sample_rate
if sample_rate not in supported_sample_rates:
for supported_sample_rate in supported_sample_rates:
if audio.sample_rate < supported_sample_rate:
sample_rate = supported_sample_rate
break
if sample_rate not in supported_sample_rates:
sample_rate = supported_sample_rates[-1]
raw_data = audio.get_raw_data(convert_rate=sample_rate,
convert_width=2)
# Based on https://cloud.yandex.com/docs/speechkit/stt#request
url = "https://stt.api.cloud.yandex.net/speech/v1/stt:recognize"
headers = {"Authorization": "Api-Key {}".format(self.api_key)}
params = "&".join([
"lang={}".format(self.lang),
"format=lpcm",
"sampleRateHertz={}".format(sample_rate)
])
response = post(url + "?" + params, headers=headers, data=raw_data)
if response.status_code == 200:
result = json.loads(response.text)
if result.get("error_code") is None:
return result.get("result")
elif response.status_code == 401: # Unauthorized
raise Exception("Invalid API key for Yandex STT")
else:
raise Exception(
"Request to Yandex STT failed: code: {}, body: {}".format(
response.status_code, response.text))
def requires_pairing(func):
"""Decorator kicking of pairing sequence if client is not allowed access.
Checks the http status of the response if an HTTP error is recieved. If
a 401 status is detected returns "pair my device" to trigger the pairing
skill.
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except HTTPError as e:
if e.response.status_code == 401:
LOG.warning('Access Denied at mycroft.ai')
# phrase to start the pairing process
return 'pair my device'
else:
raise
return wrapper
class MycroftSTT(STT):
"""Default mycroft STT."""
def __init__(self):
super(MycroftSTT, self).__init__()
self.api = STTApi("stt")
@requires_pairing
def execute(self, audio, language=None):
self.lang = language or self.lang
try:
return self.api.stt(audio.get_flac_data(convert_rate=16000),
self.lang, 1)[0]
except Exception:
return self.api.stt(audio.get_flac_data(), self.lang, 1)[0]
class MycroftDeepSpeechSTT(STT):
"""Mycroft Hosted DeepSpeech"""
def __init__(self):
super(MycroftDeepSpeechSTT, self).__init__()
self.api = STTApi("deepspeech")
@requires_pairing
def execute(self, audio, language=None):
language = language or self.lang
if not language.startswith("en"):
raise ValueError("Deepspeech is currently english only")
return self.api.stt(audio.get_wav_data(), self.lang, 1)
class DeepSpeechServerSTT(STT):
"""
STT interface for the deepspeech-server:
https://github.com/MainRo/deepspeech-server
use this if you want to host DeepSpeech yourself
"""
def __init__(self):
super(DeepSpeechServerSTT, self).__init__()
def execute(self, audio, language=None):
language = language or self.lang
response = post(self.config.get("uri"), data=audio.get_wav_data())
return response.text
class StreamThread(Thread, metaclass=ABCMeta):
"""ABC class to be used with StreamingSTT class implementations.
This class reads audio chunks from a queue and sends it to a parsing
STT engine.
Args:
queue (Queue): Input Queue
language (str): language code for the current language.
"""
def __init__(self, queue, language):
super().__init__()
self.language = language
self.queue = queue
self.text = None
def _get_data(self):
"""Generator reading audio data from queue."""
while True:
d = self.queue.get()
if d is None:
break
yield d
self.queue.task_done()
def run(self):
"""Thread entry point."""
return self.handle_audio_stream(self._get_data(), self.language)
@abstractmethod
def handle_audio_stream(self, audio, language):
"""Handling of audio stream.
Needs to be implemented by derived class to process audio data and
optionally update `self.text` with the current hypothesis.
Argumens:
audio (bytes): raw audio data.
language (str): language code for the current session.
"""
class StreamingSTT(STT, metaclass=ABCMeta):
"""ABC class for threaded streaming STT implemenations."""
def __init__(self):
super().__init__()
self.stream = None
self.can_stream = True
def stream_start(self, language=None):
"""Indicate start of new audio stream.
This creates a new thread for handling the incomming audio stream as
it's collected by Mycroft.
Args:
language (str): optional language code for the new stream.
"""
self.stream_stop()
language = language or self.lang
self.queue = Queue()
self.stream = self.create_streaming_thread()
self.stream.start()
def stream_data(self, data):
"""Receiver of audio data.
Args:
data (bytes): raw audio data.
"""
self.queue.put(data)
def stream_stop(self):
"""Indicate that the audio stream has ended.
This will tear down the processing thread and collect the result
Returns:
str: parsed text
"""
if self.stream is not None:
self.queue.put(None)
self.stream.join()
text = self.stream.text
self.stream = None
self.queue = None
return text
return None
def execute(self, audio, language=None):
"""End the parsing thread and collect data."""
return self.stream_stop()
@abstractmethod
def create_streaming_thread(self):
"""Create thread for parsing audio chunks.
This method should be implemented by the derived class to return an
instance derived from StreamThread to handle the audio stream and
send it to the STT engine.
Returns:
StreamThread: Thread to handle audio data.
"""
class DeepSpeechStreamThread(StreamThread):
def __init__(self, queue, language, url):
if not language.startswith("en"):
raise ValueError("Deepspeech is currently english only")
super().__init__(queue, language)
self.url = url
def handle_audio_stream(self, audio, language):
self.response = post(self.url, data=audio, stream=True)
self.text = self.response.text if self.response else None
return self.text
class DeepSpeechStreamServerSTT(StreamingSTT):
"""
Streaming STT interface for the deepspeech-server:
https://github.com/JPEWdev/deep-dregs
use this if you want to host DeepSpeech yourself
STT config will look like this:
"stt": {
"module": "deepspeech_stream_server",
"deepspeech_stream_server": {
"stream_uri": "http://localhost:8080/stt?format=16K_PCM16"
...
"""
def create_streaming_thread(self):
self.queue = Queue()
return DeepSpeechStreamThread(
self.queue,
self.lang,
self.config.get('stream_uri')
)
class GoogleStreamThread(StreamThread):
def __init__(self, queue, lang, client, streaming_config):
super().__init__(queue, lang)
self.client = client
self.streaming_config = streaming_config
def handle_audio_stream(self, audio, language):
req = (types.StreamingRecognizeRequest(audio_content=x) for x in audio)
responses = self.client.streaming_recognize(self.streaming_config, req)
for res in responses:
if res.results and res.results[0].is_final:
self.text = res.results[0].alternatives[0].transcript
return self.text
class GoogleCloudStreamingSTT(StreamingSTT):
"""
Streaming STT interface for Google Cloud Speech-To-Text
To use pip install google-cloud-speech and add the
Google API key to local mycroft.conf file. The STT config
will look like this:
"stt": {
"module": "google_cloud_streaming",
"google_cloud_streaming": {
"credential": {
"json": {
# Paste Google API JSON here
...
"""
def __init__(self):
global SpeechClient, types, enums, Credentials
from google.cloud.speech import SpeechClient, types, enums
from google.oauth2.service_account import Credentials
super(GoogleCloudStreamingSTT, self).__init__()
# override language with module specific language selection
self.language = self.config.get('lang') or self.lang
credentials = Credentials.from_service_account_info(
self.credential.get('json')
)
self.client = SpeechClient(credentials=credentials)
recognition_config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=16000,
language_code=self.language,
model='command_and_search',
max_alternatives=1,
)
self.streaming_config = types.StreamingRecognitionConfig(
config=recognition_config,
interim_results=True,
single_utterance=True,
)
def create_streaming_thread(self):
self.queue = Queue()
return GoogleStreamThread(
self.queue,
self.language,
self.client,
self.streaming_config
)
class KaldiSTT(STT):
def __init__(self):
super(KaldiSTT, self).__init__()
def execute(self, audio, language=None):
language = language or self.lang
response = post(self.config.get("uri"), data=audio.get_wav_data())
return self.get_response(response)
def get_response(self, response):
try:
hypotheses = response.json()["hypotheses"]
return re.sub(r'\s*\[noise\]\s*', '', hypotheses[0]["utterance"])
except Exception:
return None
class BingSTT(TokenSTT):
def __init__(self):
super(BingSTT, self).__init__()
def execute(self, audio, language=None):
self.lang = language or self.lang
return self.recognizer.recognize_bing(audio, self.token,
self.lang)
class HoundifySTT(KeySTT):
def __init__(self):
super(HoundifySTT, self).__init__()
def execute(self, audio, language=None):
self.lang = language or self.lang
return self.recognizer.recognize_houndify(audio, self.id, self.key)
class GoVivaceSTT(TokenSTT):
def __init__(self):
super(GoVivaceSTT, self).__init__()
self.default_uri = "https://services.govivace.com:49149/telephony"
if not self.lang.startswith("en") and not self.lang.startswith("es"):
LOG.error("GoVivace STT only supports english and spanish")
raise NotImplementedError
def execute(self, audio, language=None):
url = self.config.get("uri", self.default_uri) + "?key=" + \
self.token + "&action=find&format=8K_PCM16&validation_string="
response = put(url,
data=audio.get_wav_data(convert_rate=8000))
return self.get_response(response)
def get_response(self, response):
return response.json()["result"]["hypotheses"][0]["transcript"]
def load_stt_plugin(module_name):
"""Wrapper function for loading stt plugin.
Args:
module_name (str): Mycroft stt module name from config
Returns:
class: STT plugin class
"""
return load_plugin('mycroft.plugin.stt', module_name)
class STTFactory:
CLASSES = {
"mycroft": MycroftSTT,
"google": GoogleSTT,
"google_cloud": GoogleCloudSTT,
"google_cloud_streaming": GoogleCloudStreamingSTT,
"wit": WITSTT,
"ibm": IBMSTT,
"kaldi": KaldiSTT,
"bing": BingSTT,
"govivace": GoVivaceSTT,
"houndify": HoundifySTT,
"deepspeech_server": DeepSpeechServerSTT,
"deepspeech_stream_server": DeepSpeechStreamServerSTT,
"mycroft_deepspeech": MycroftDeepSpeechSTT,
"yandex": YandexSTT
}
@staticmethod
def create():
try:
config = Configuration.get().get("stt", {})
module = config.get("module", "mycroft")
if module in STTFactory.CLASSES:
clazz = STTFactory.CLASSES[module]
else:
clazz = load_stt_plugin(module)
LOG.info('Loaded the STT plugin {}'.format(module))
return clazz()
except Exception:
# The STT backend failed to start. Report it and fall back to
# default.
LOG.exception('The selected STT backend could not be loaded, '
'falling back to default...')
if module != 'mycroft':
return MycroftSTT()
else:
raise