Skip to content

Commit

Permalink
Merge pull request #273 from Holzhaus/wit-stt-engine
Browse files Browse the repository at this point in the history
Added witai-stt-engine to client.stt module
  • Loading branch information
Holzhaus committed Jan 7, 2015
2 parents 8b917ca + 7993577 commit 6ef351f
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions client/stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,90 @@ def is_available(cls):
return diagnose.check_network_connection()


class WitAiSTT(AbstractSTTEngine):
"""
Speech-To-Text implementation which relies on the Wit.ai Speech API.
This implementation requires an Wit.ai Access Token to be present in
profile.yml. Please sign up at https://wit.ai and copy your instance
token, which can be found under Settings in the Wit console to your
profile.yml:
...
stt_engine: witai
witai-stt:
access_token: ERJKGE86SOMERANDOMTOKEN23471AB
"""

SLUG = "witai"

def __init__(self, access_token):
self._logger = logging.getLogger(__name__)
self.token = access_token

@classmethod
def get_config(cls):
# FIXME: Replace this as soon as we have a config module
config = {}
# Try to get wit.ai Auth token from config
profile_path = jasperpath.config('profile.yml')
if os.path.exists(profile_path):
with open(profile_path, 'r') as f:
profile = yaml.safe_load(f)
if 'witai-stt' in profile:
if 'access_token' in profile['witai-stt']:
config['access_token'] = \
profile['witai-stt']['access_token']
return config

@property
def token(self):
return self._token

@token.setter
def token(self, value):
self._token = value
self._headers = {'Authorization': 'Bearer %s' % self.token,
'accept': 'application/json',
'Content-Type': 'audio/wav'}

@property
def headers(self):
return self._headers

def transcribe(self, fp):
data = fp.read()
r = requests.post('https://api.wit.ai/speech?v=20150101',
data=data,
headers=self.headers)
try:
r.raise_for_status()
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 = [text.upper()]
self._logger.info('Transcribed: %r', transcribed)
return transcribed

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


def get_engine_by_slug(slug=None):
"""
Returns:
Expand Down

0 comments on commit 6ef351f

Please sign in to comment.