Beispiel #1
0
                transcribed = [
                    x[0].upper()
                    for x in sorted(results, key=lambda x: x[1], reverse=True)
                ]
                self._logger.info('Transcribed: %r', transcribed)
                return transcribed

    def _get_response(self, data):
        headers = {
            'authorization': 'Bearer %s' % self.token,
            'accept': 'application/json',
            'content-type': 'audio/wav'
        }
        return requests.post('https://api.att.com/speech/v3/speechToText',
                             data=data,
                             headers=headers)

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


def is_valid():
    '''
    Module validator.
    '''
    return True


initiator = _module_getter(AttSTT)
Beispiel #2
0
        ]
        cmd = [str(x) for x in cmd]
        self._logger.debug('Executing: %r', cmd)
        with tempfile.SpooledTemporaryFile() as out_f:
            with tempfile.SpooledTemporaryFile() as err_f:
                subprocess.call(cmd, stdin=fp, stdout=out_f, stderr=err_f)
            out_f.seek(0)
            results = [(int(i), text)
                       for i, text in self._pattern.findall(out_f.read())]
        transcribed = [
            text for i, text in sorted(results, key=lambda x: x[0]) if text
        ]
        if not transcribed:
            transcribed.append('')
        self._logger.info('Transcribed: %r', transcribed)
        return transcribed

    @classmethod
    def is_available(cls):
        return diagnose.check_executable('julius')


def is_valid():
    '''
    Module validator.
    '''
    return True


initiator = _module_getter(JuliusSTT)
Beispiel #3
0
    def say(self, phrase, *args):
        self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
            fname = f.name
        cmd = [
            'espeak', '-v', self.voice, '-p', self.pitch_adjustment, '-s',
            self.words_per_minute, '-w', fname, phrase
        ]
        cmd = [str(x) for x in cmd]
        self._logger.debug('Executing %s',
                           ' '.join([pipes.quote(arg) for arg in cmd]))
        with tempfile.TemporaryFile() as f:
            subprocess.call(cmd, stdout=f, stderr=f)
            f.seek(0)
            output = f.read()
            if output:
                self._logger.debug("Output was: '%s'", output)
        self.play(fname)
        os.remove(fname)


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(EspeakTTS)
Beispiel #4
0
                self._logger.warning('Status 403 is probably caused by invalid credentials.')
            return []
        r.encoding = 'utf-8'
        results = []
        try:
            response = r.json()
            if not response['results']:
                raise ValueError('Nothing has been transcribed.')
            results = tuple([alt['transcript'].strip().upper() for alt in response['results'][0]['alternatives']])
            self._logger.info('Transcribed: %r', results)
        except ValueError as e:
            self._logger.critical('Empty response: %s', e.args[0])
        except (KeyError, IndexError):
            self._logger.critical('Cannot parse response.', exc_info=True)

        return results

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


def is_valid():
    '''
    Module validator.
    '''
    return True


initiator = _module_getter(WatsonSTT)
Beispiel #5
0
        # FIXME: Can't use the Decoder.decode_raw() here, because
        # pocketsphinx segfaults with tempfile.SpooledTemporaryFile()
        data = fp.read()
        self._decoder.start_utt()
        self._decoder.process_raw(data, False, True)
        self._decoder.end_utt()

        result = self._decoder.get_hyp()
        with open(self._logfile, 'r+') as f:
            for line in f:
                self._logger.debug(line.strip())
            f.truncate()

        transcribed = [result[0]]
        self._logger.info('Transcribed: %r', transcribed)
        return transcribed

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


def is_valid():
    '''
    Module validator.
    '''
    return True


initiator = _module_getter(PocketSphinxSTT)
Beispiel #6
0
            if 'speech_rate' in profile['ivona-tts']:
                config['speech_rate'] = profile['ivona-tts']['speech_rate']
            if 'sentence_break' in profile['ivona-tts']:
                config['sentence_break'] = profile['ivona-tts']['sentence_break']

        return config

    @classmethod
    def is_available(cls):
        return (super(cls, cls).is_available() and
                diagnose.check_python_import('pyvona') and
                diagnose.check_network_connection())

    def say(self, phrase, *args):
        self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
        with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f:
            tmpfile = f.name
        self._pyvonavoice.fetch_voice(phrase, tmpfile)
        self.play_mp3(tmpfile)
        os.remove(tmpfile)


def is_valid():
    '''
    Validator.
    '''
    return HAS_PYVONA


initiator = _module_getter(IvonaTTS)
Beispiel #7
0
        cmd = ['say', str(phrase)]
        self._logger.debug('Executing %s',
                           ' '.join([pipes.quote(arg) for arg in cmd]))
        with tempfile.TemporaryFile() as f:
            subprocess.call(cmd, stdout=f, stderr=f)
            f.seek(0)
            output = f.read()
            if output:
                self._logger.debug("Output was: '%s'", output)

    def play(self, filename):
        cmd = ['afplay', str(filename)]
        self._logger.debug('Executing %s',
                           ' '.join([pipes.quote(arg) for arg in cmd]))
        with tempfile.TemporaryFile() as f:
            subprocess.call(cmd, stdout=f, stderr=f)
            f.seek(0)
            output = f.read()
            if output:
                self._logger.debug("Output was: '%s'", output)


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(MacOSXTTS)
Beispiel #8
0
    def languages(self):
        langs = [
            'af', 'sq', 'ar', 'hy', 'ca', 'zh-CN', 'zh-TW', 'hr', 'cs', 'da',
            'nl', 'en', 'eo', 'fi', 'fr', 'de', 'el', 'ht', 'hi', 'hu', 'is',
            'id', 'it', 'ja', 'ko', 'la', 'lv', 'mk', 'no', 'pl', 'pt', 'ro',
            'ru', 'sr', 'sk', 'es', 'sw', 'sv', 'ta', 'th', 'tr', 'vi', 'cy'
        ]
        return langs

    def say(self, phrase, *args):
        self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
        if self.language not in self.languages:
            raise ValueError("Language '%s' not supported by '%s'",
                             self.language, self.SLUG)
        tts = gtts.gTTS(text=phrase, lang=self.language)
        with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f:
            tmpfile = f.name
        tts.save(tmpfile)
        self.play_mp3(tmpfile)
        os.remove(tmpfile)


def is_valid():
    '''
    Validator.
    '''
    return HAS_GTTS


initiator = _module_getter(GoogleTTS)
Beispiel #9
0
                             (self.language, self.SLUG))

        if self.voice not in self.voices:
            raise ValueError("Voice '%s' not supported by '%s'" %
                             (self.voice, self.SLUG))
        query = {
            'OUTPUT_TYPE': 'AUDIO',
            'AUDIO': 'WAVE_FILE',
            'INPUT_TYPE': 'TEXT',
            'INPUT_TEXT': phrase,
            'LOCALE': self.language,
            'VOICE': self.voice
        }

        r = self.session.get(self._makeurl('/process', query=query))
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
            f.write(r.content)
            tmpfile = f.name
        self.play(tmpfile)
        os.remove(tmpfile)


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(MaryTTS)
Beispiel #10
0
    def say(self, phrase, *args):
        self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
        cmd = ['flite']
        if self.voice:
            cmd.extend(['-voice', self.voice])
        cmd.extend(['-t', phrase])
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
            fname = f.name
        cmd.append(fname)
        with tempfile.SpooledTemporaryFile() as out_f:
            self._logger.debug('Executing %s',
                               ' '.join([pipes.quote(arg) for arg in cmd]))
            subprocess.call(cmd, stdout=out_f, stderr=out_f)
            out_f.seek(0)
            output = out_f.read().strip()
        if output:
            self._logger.debug("Output was: '%s'", output)
        self.play(fname)
        os.remove(fname)


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(FliteTTS)
Beispiel #11
0
            if len(response['result']) == 0:
                # Response result is empty
                raise ValueError('Nothing has been transcribed.')
            results = [alt['transcript'] for alt
                       in response['result'][0]['alternative']]
        except ValueError as e:
            self._logger.warning('Empty response: %s', e.args[0])
            results = []
        except (KeyError, IndexError):
            self._logger.warning('Cannot parse response.', exc_info=True)
            results = []
        else:
            # Convert all results to uppercase
            results = tuple(result.upper() for result in results)
            self._logger.info('Transcribed: %r', results)
        return results

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


def is_valid():
    '''
    Module validator.
    '''
    return True


initiator = _module_getter(GoogleSTT)
Beispiel #12
0
            text = r.json()['_text']
        except requests.exceptions.HTTPError:
            self._logger.critical('Request failed with response: %r',
                                  r.text,
                                  exc_info=True)
            return []
        except requests.exceptions.RequestException:
            self._logger.critical('Request failed.', exc_info=True)
            return []
        except ValueError as e:
            self._logger.critical('Cannot parse response: %s',
                                  e.args[0])
            return []
        except KeyError:
            self._logger.critical('Cannot parse response.',
                                  exc_info=True)
            return []
        else:
            transcribed = []
            if text:
                transcribed.append(text.upper())
            self._logger.info('Transcribed: %r', transcribed)
            return transcribed

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


initiator = _module_getter(WitAiSTT)
Beispiel #13
0

class DummyTTS(AbstractTTSEngine):
    """
    Dummy TTS engine that logs phrases with INFO level instead of synthesizing
    speech.
    """

    SLUG = "dummy-tts"

    @classmethod
    def is_available(cls):
        return True

    def say(self, phrase, *args):
        self._logger.info(phrase)

    def play(self, filename):
        self._logger.debug("Playback of file '%s' requested")
        pass


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(DummyTTS)
Beispiel #14
0
        return False

    def say(self, phrase, *args):
        self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
        cmd = ['text2wave']
        with tempfile.NamedTemporaryFile(suffix='.wav') as out_f:
            with tempfile.SpooledTemporaryFile() as in_f:
                in_f.write(phrase)
                in_f.seek(0)
                with tempfile.SpooledTemporaryFile() as err_f:
                    self._logger.debug('Executing %s',
                                       ' '.join([pipes.quote(arg)
                                                 for arg in cmd]))
                    subprocess.call(cmd, stdin=in_f, stdout=out_f,
                                    stderr=err_f)
                    err_f.seek(0)
                    output = err_f.read()
                    if output:
                        self._logger.debug("Output was: '%s'", output)
            self.play(out_f.name)


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(FestivalTTS)
Beispiel #15
0
    def say(self, phrase, *args):
        self._logger.debug("Saying '%s' with '%s'", phrase, self.SLUG)
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
            fname = f.name
        cmd = ['pico2wave', '--wave', fname]
        if self.language not in self.languages:
            raise ValueError("Language '%s' not supported by '%s'",
                             self.language, self.SLUG)
        cmd.extend(['-l', self.language])
        cmd.append(phrase)
        self._logger.debug('Executing %s',
                           ' '.join([pipes.quote(arg) for arg in cmd]))
        with tempfile.TemporaryFile() as f:
            subprocess.call(cmd, stdout=f, stderr=f)
            f.seek(0)
            output = f.read()
            if output:
                self._logger.debug("Output was: '%s'", output)
        self.play(fname)
        os.remove(fname)


def is_valid():
    '''
    Validator.
    '''
    return True


initiator = _module_getter(PicoTTS)