summaryrefslogtreecommitdiff
path: root/client/stt.py
blob: 9ac1a182b52a23edaac7af5c4ce098a15b3c9014 (plain)
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
#!/usr/bin/env python2
# -*- coding: utf-8-*-
import os
import traceback
import wave
import json
import tempfile
import logging
from abc import ABCMeta, abstractmethod
import requests
import yaml
import jasperpath
import diagnose
import vocabcompiler


class TranscriptionMode:
    NORMAL, KEYWORD, MUSIC = range(3)


class AbstractSTTEngine(object):
    """
    Generic parent class for all STT engines
    """

    __metaclass__ = ABCMeta

    @classmethod
    def get_config(cls):
        return {}

    @classmethod
    @abstractmethod
    def is_available(cls):
        return True

    @abstractmethod
    def transcribe(self, fp, mode=TranscriptionMode.NORMAL):
        pass


class PocketSphinxSTT(AbstractSTTEngine):
    """
    The default Speech-to-Text implementation which relies on PocketSphinx.
    """

    SLUG = 'sphinx'

    def __init__(self, vocabulary=None, vocabulary_keyword=None,
                 vocabulary_music=None, hmm_dir="/usr/local/share/" +
                 "pocketsphinx/model/hmm/en_US/hub4wsj_sc_8k"):

        """
        Initiates the pocketsphinx instance.

        Arguments:
            vocabulary -- a PocketsphinxVocabulary instance
            vocabulary_keyword -- a PocketsphinxVocabulary instance
                                  (containing, e.g., 'Jasper')
            vocabulary_music -- (optional) a PocketsphinxVocabulary instance
            hmm_dir -- the path of the Hidden Markov Model (HMM)
        """

        self._logger = logging.getLogger(__name__)

        # quirky bug where first import doesn't work
        try:
            import pocketsphinx as ps
        except:
            import pocketsphinx as ps

        self._logfiles = {}
        with tempfile.NamedTemporaryFile(prefix='psdecoder_music_',
                                         suffix='.log', delete=False) as f:
            self._logfiles[TranscriptionMode.MUSIC] = f.name
        with tempfile.NamedTemporaryFile(prefix='psdecoder_keyword_',
                                         suffix='.log', delete=False) as f:
            self._logfiles[TranscriptionMode.KEYWORD] = f.name
        with tempfile.NamedTemporaryFile(prefix='psdecoder_normal_',
                                         suffix='.log', delete=False) as f:
            self._logfiles[TranscriptionMode.NORMAL] = f.name

        self._decoders = {}
        if vocabulary_music is not None:
            self._decoders[TranscriptionMode.MUSIC] = \
                ps.Decoder(hmm=hmm_dir,
                           logfn=self._logfiles[TranscriptionMode.MUSIC],
                           **vocabulary_music.decoder_kwargs)
        self._decoders[TranscriptionMode.KEYWORD] = \
            ps.Decoder(hmm=hmm_dir,
                       logfn=self._logfiles[TranscriptionMode.KEYWORD],
                       **vocabulary_keyword.decoder_kwargs)
        self._decoders[TranscriptionMode.NORMAL] = \
            ps.Decoder(hmm=hmm_dir,
                       logfn=self._logfiles[TranscriptionMode.NORMAL],
                       **vocabulary.decoder_kwargs)

    def __del__(self):
        for filename in self._logfiles.values():
            os.remove(filename)

    @classmethod
    def get_config(cls):
        # FIXME: Replace this as soon as we have a config module
        config = {}
        # HMM dir
        # Try to get hmm_dir from config
        profile_path = os.path.join(os.path.dirname(__file__), 'profile.yml')

        name_default = 'default'
        path_default = jasperpath.config('vocabularies')

        name_keyword = 'keyword'
        path_keyword = jasperpath.config('vocabularies')

        if os.path.exists(profile_path):
            with open(profile_path, 'r') as f:
                profile = yaml.safe_load(f)
                if 'pocketsphinx' in profile:
                    if 'hmm_dir' in profile['pocketsphinx']:
                        config['hmm_dir'] = profile['pocketsphinx']['hmm_dir']

                    if 'vocabulary_default_name' in profile['pocketsphinx']:
                        name_default = \
                            profile['pocketsphinx']['vocabulary_default_name']

                    if 'vocabulary_default_path' in profile['pocketsphinx']:
                        path_default = \
                            profile['pocketsphinx']['vocabulary_default_path']

                    if 'vocabulary_keyword_name' in profile['pocketsphinx']:
                        name_keyword = \
                            profile['pocketsphinx']['vocabulary_keyword_name']

                    if 'vocabulary_keyword_path' in profile['pocketsphinx']:
                        path_keyword = \
                            profile['pocketsphinx']['vocabulary_keyword_path']

        config['vocabulary'] = vocabcompiler.PocketsphinxVocabulary(
            name_default, path=path_default)
        config['vocabulary_keyword'] = vocabcompiler.PocketsphinxVocabulary(
            name_keyword, path=path_keyword)

        config['vocabulary'].compile(vocabcompiler.get_all_phrases())
        config['vocabulary_keyword'].compile(
            vocabcompiler.get_keyword_phrases())

        return config

    def transcribe(self, fp, mode=TranscriptionMode.NORMAL):
        """
        Performs STT, transcribing an audio file and returning the result.

        Arguments:
        audio_file_path -- the path to the audio file to-be transcribed
        PERSONA_ONLY -- if True, uses the 'Persona' language model and
                        dictionary
        MUSIC -- if True, uses the 'Music' language model and dictionary
        """
        decoder = self._decoders[mode]

        fp.seek(44)

        # FIXME: Can't use the Decoder.decode_raw() here, because
        # pocketsphinx segfaults with tempfile.SpooledTemporaryFile()
        data = fp.read()
        decoder.start_utt()
        decoder.process_raw(data, False, True)
        decoder.end_utt()

        result = decoder.get_hyp()
        with open(self._logfiles[mode], 'r+') as f:
                if mode == TranscriptionMode.KEYWORD:
                    modename = "[KEYWORD]"
                elif mode == TranscriptionMode.MUSIC:
                    modename = "[MUSIC]"
                else:
                    modename = "[NORMAL]"
                for line in f:
                    self._logger.debug("%s %s", modename, line.strip())
                f.truncate()

        print "==================="
        print "JASPER: " + result[0]
        print "==================="

        return [result[0]]

    @classmethod
    def is_available(cls):
        return diagnose.check_python_import('pocketsphinx')


class GoogleSTT(AbstractSTTEngine):
    """
    Speech-To-Text implementation which relies on the Google Speech API.

    This implementation requires a Google API key to be present in profile.yml

    To obtain an API key:
    1. Join the Chromium Dev group:
       https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev
    2. Create a project through the Google Developers console:
       https://console.developers.google.com/project
    3. Select your project. In the sidebar, navigate to "APIs & Auth." Activate
       the Speech API.
    4. Under "APIs & Auth," navigate to "Credentials." Create a new key for
       public API access.
    5. Add your credentials to your profile.yml. Add an entry to the 'keys'
       section using the key name 'GOOGLE_SPEECH.' Sample configuration:
    6. Set the value of the 'stt_engine' key in your profile.yml to 'google'


    Excerpt from sample profile.yml:

        ...
        timezone: US/Pacific
        stt_engine: google
        keys:
            GOOGLE_SPEECH: $YOUR_KEY_HERE

    """

    SLUG = 'google'

    def __init__(self, api_key=None):
        # FIXME: get init args from config
        """
        Arguments:
        api_key - the public api key which allows access to Google APIs
        """
        if not api_key:
            raise ValueError("No Google API Key given")
        self.api_key = api_key
        self.http = requests.Session()

    @classmethod
    def get_config(cls):
        # FIXME: Replace this as soon as we have a config module
        config = {}
        # HMM dir
        # Try to get hmm_dir from config
        profile_path = os.path.join(os.path.dirname(__file__), 'profile.yml')
        if os.path.exists(profile_path):
            with open(profile_path, 'r') as f:
                profile = yaml.safe_load(f)
                if 'keys' in profile and 'GOOGLE_SPEECH' in profile['keys']:
                    config['api_key'] = profile['keys']['GOOGLE_SPEECH']
        return config

    def transcribe(self, fp, mode=TranscriptionMode.NORMAL):
        """
        Performs STT via the Google Speech API, transcribing an audio file and
        returning an English string.

        Arguments:
        audio_file_path -- the path to the .wav file to be transcribed
        """

        wav = wave.open(fp, 'rb')
        frame_rate = wav.getframerate()
        wav.close()

        url = (("https://www.google.com/speech-api/v2/recognize?output=json" +
                "&client=chromium&key=%s&lang=%s&maxresults=6&pfilter=2") %
               (self.api_key, "en-us"))

        data = fp.read()

        try:
            headers = {'Content-type': 'audio/l16; rate=%s' % frame_rate}
            response = self.http.post(url, data=data, headers=headers)
            response.encoding = 'utf-8'
            response_read = response.text

            response_parts = response_read.strip().split("\n")
            decoded = json.loads(response_parts[-1])
            if decoded['result']:
                texts = [alt['transcript'] for alt in
                         decoded['result'][0]['alternative']]
                if texts:
                    print "==================="
                    print "JASPER: " + ', '.join(texts)
                    print "==================="
                return texts
            else:
                return []
        except Exception:
            traceback.print_exc()

    @classmethod
    def is_available(cls):
        return diagnose.check_network_connection()


def get_engines():
    return [stt_engine for stt_engine in AbstractSTTEngine.__subclasses__()
            if hasattr(stt_engine, 'SLUG') and stt_engine.SLUG]


def newSTTEngine(stt_engine, **kwargs):
    """
    Returns a Speech-To-Text engine.

    Currently, the supported implementations are the default Pocket Sphinx and
    the Google Speech API

    Arguments:
        engine_type -- one of "sphinx" or "google"
        kwargs -- keyword arguments passed to the constructor of the STT engine
    """
    selected_engines = filter(lambda engine: hasattr(engine, "SLUG") and
                              engine.SLUG == stt_engine, get_engines())
    if len(selected_engines) == 0:
        raise ValueError("No STT engine found for slug '%s'" % stt_engine)
    else:
        if len(selected_engines) > 1:
            print(("WARNING: Multiple STT engines found for slug '%s'. This " +
                   "is most certainly a bug.") % stt_engine)
        engine = selected_engines[0]
        if not engine.is_available():
            raise ValueError(("STT engine '%s' is not available (due to " +
                              "missing dependencies, missing dependencies, " +
                              "etc.)") % stt_engine)
        return engine(**engine.get_config())