Merge pull request #1261 from JarbasAl/ibm_tts

Add IBM Watson tts
pull/1332/merge
Åke 2018-01-07 16:58:45 +01:00 committed by GitHub
commit c3d0c1bcb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 3 deletions

View File

@ -362,14 +362,15 @@ class TTSFactory(object):
from mycroft.tts.mary_tts import MaryTTS
from mycroft.tts.mimic_tts import Mimic
from mycroft.tts.spdsay_tts import SpdSay
from mycroft.tts.ibm_tts import WatsonTTS
CLASSES = {
"mimic": Mimic,
"google": GoogleTTS,
"marytts": MaryTTS,
"fatts": FATTS,
"espeak": ESpeak,
"spdsay": SpdSay
"spdsay": SpdSay,
"watson": WatsonTTS
}
@staticmethod

61
mycroft/tts/ibm_tts.py Normal file
View File

@ -0,0 +1,61 @@
# 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.
#
from mycroft.tts import TTSValidator
from mycroft.tts.remote_tts import RemoteTTS
from mycroft.configuration import Configuration
from requests.auth import HTTPBasicAuth
class WatsonTTS(RemoteTTS):
PARAMS = {'accept': 'audio/wav'}
def __init__(self, lang, voice="en-US_AllisonVoice",
url="https://stream.watsonplatform.net/text-to-speech/api"):
super(WatsonTTS, self).__init__(lang, voice, url, '/v1/synthesize',
WatsonTTSValidator(self))
self.type = "wav"
self.config = Configuration.get().get("tts", {}).get("watson", {})
user = self.config.get("user") or self.config.get("username")
password = self.config.get("password")
self.auth = HTTPBasicAuth(user, password)
def build_request_params(self, sentence):
params = self.PARAMS.copy()
params['LOCALE'] = self.lang
params['voice'] = self.voice
params['text'] = sentence.encode('utf-8')
return params
class WatsonTTSValidator(TTSValidator):
def __init__(self, tts):
super(WatsonTTSValidator, self).__init__(tts)
def validate_lang(self):
# TODO
pass
def validate_connection(self):
config = Configuration.get().get("tts", {}).get("watson", {})
user = config.get("user") or config.get("username")
password = config.get("password")
if user and password:
return
else:
raise ValueError('user and/or password for IBM tts is not defined')
def get_tts_class(self):
return WatsonTTS

View File

@ -32,6 +32,7 @@ class RemoteTTS(TTS):
def __init__(self, lang, voice, url, api_path, validator):
super(RemoteTTS, self).__init__(lang, voice, validator)
self.api_path = api_path
self.auth = None
self.url = remove_last_slash(url)
self.session = FuturesSession()
@ -64,7 +65,7 @@ class RemoteTTS(TTS):
def __request(self, p):
return self.session.get(
self.url + self.api_path, params=self.build_request_params(p),
timeout=10, verify=False)
timeout=10, verify=False, auth=self.auth)
@abc.abstractmethod
def build_request_params(self, sentence):