summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2015-01-07 13:49:57 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2015-01-07 13:49:57 +0100
commit5049d186f1fa71e81f5d16329d64dabe1556b3a0 (patch)
treead8064ab881a8d52772cd69c77a3c8b45ca18827
parentb188e8029c850b5deb1a90114845a116dd814260 (diff)
parent04a7d4c6d0c4aef37c95efa63d10d393b0a4e3b5 (diff)
downloadjasper-client-5049d186f1fa71e81f5d16329d64dabe1556b3a0.tar.gz
jasper-client-5049d186f1fa71e81f5d16329d64dabe1556b3a0.zip
Merge pull request #273 from Holzhaus/wit-stt-engine
Added witai-stt-engine to client.stt module
-rw-r--r--client/stt.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/client/stt.py b/client/stt.py
index 86cc83b..4298cd2 100644
--- a/client/stt.py
+++ b/client/stt.py
@@ -413,6 +413,90 @@ class AttSTT(AbstractSTTEngine):
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: