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 = saschapath.config('profile.yml') name_default = 'default' path_default = saschapath.config('vocabularies') name_keyword = 'keyword' path_keyword = saschapath.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 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 = saschapath.config('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 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 = saschapath.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
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 """ # Read config new_configfile = saschapath.config('profile.yml') try: with open(new_configfile, "r") as f: self.config = yaml.safe_load(f) except OSError: raise 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, self.config['stt_lang'])) 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 "SASCHA: " + ', '.join(texts) print "===================" return texts else: return [] except Exception: traceback.print_exc()
def get_config(cls): # FIXME: Replace this as soon as pull request # jasperproject/jasper-client#128 has been merged conf = {'fst_model': saschapath.home("phonetisaurus/g014b2b.fst")} # Try to get fst_model from config profile_path = saschapath.config('profile.yml') if os.path.exists(profile_path): with open(profile_path, 'r') as f: profile = yaml.safe_load(f) if 'pocketsphinx' in profile: if 'fst_model' in profile['pocketsphinx']: conf['fst_model'] = \ profile['pocketsphinx']['fst_model'] if 'nbest' in profile['pocketsphinx']: conf['nbest'] = int(profile['pocketsphinx']['nbest']) return conf
def __init__(self): self._logger = logging.getLogger(__name__) # Create config dir if it does not exist yet if not os.path.exists(saschapath.CONFIG_PATH): try: os.makedirs(saschapath.CONFIG_PATH) except OSError: self._logger.error("Could not create config dir: '%s'", saschapath.CONFIG_PATH, exc_info=True) raise # Check if config dir is writable if not os.access(saschapath.CONFIG_PATH, os.W_OK): self._logger.critical("Config dir %s is not writable. Jasper " + "won't work correctly.", saschapath.CONFIG_PATH) # FIXME: For backwards compatibility, move old config file to newly # created config dir old_configfile = os.path.join(saschapath.LIB_PATH, 'profile.yml') new_configfile = saschapath.config('profile.yml') if os.path.exists(old_configfile): if os.path.exists(new_configfile): self._logger.warning("Deprecated profile file found: '%s'. " + "Please remove it.", old_configfile) else: self._logger.warning("Deprecated profile file found: '%s'. " + "Trying to copy it to new location '%s'.", old_configfile, new_configfile) try: shutil.copy2(old_configfile, new_configfile) except shutil.Error: self._logger.error("Unable to copy config file. " + "Please copy it manually.", exc_info=True) raise # Read config self._logger.debug("Trying to read config file: '%s'", new_configfile) try: with open(new_configfile, "r") as f: self.config = yaml.safe_load(f) except OSError: self._logger.error("Can't open config file: '%s'", new_configfile) raise try: api_key = self.config['keys']['GOOGLE_SPEECH'] except KeyError: api_key = None try: stt_engine_type = self.config['stt_engine'] except KeyError: stt_engine_type = "sphinx" self._logger.warning("stt_engine not specified in profile, " + "defaulting to '%s'", stt_engine_type) try: tts_engine_slug = self.config['tts_engine'] except KeyError: tts_engine_slug = tts.get_default_engine_slug() logger.warning("tts_engine not specified in profile, defaulting " + "to '%s'", tts_engine_slug) tts_engine_class = tts.get_engine_by_slug(tts_engine_slug) # Initialize Mic self.mic = Mic(tts_engine_class(), stt.PocketSphinxSTT(**stt.PocketSphinxSTT.get_config()), stt.newSTTEngine(stt_engine_type, api_key=api_key))
def run(): profile = {} print("Welcome to the profile populator. If, at any step, you'd prefer " + "not to enter the requested information, just hit 'Enter' with a " + "blank field to continue.") def simple_request(var, cleanVar, cleanInput=None): input = raw_input(cleanVar + ": ") if input: if cleanInput: input = cleanInput(input) profile[var] = input # name simple_request('first_name', 'First name') simple_request('last_name', 'Last name') # timezone print("\nPlease enter a timezone from the list located in the TZ* " + "column at http://en.wikipedia.org/wiki/" + "List_of_tz_database_time_zones, or none at all.") tz = raw_input("Timezone: ") while tz: try: timezone(tz) profile['timezone'] = tz break except: print("Not a valid timezone. Try again.") tz = raw_input("Timezone: ") response = raw_input("\nWould you prefer to have notifications sent by " + "email (E) or text message (T)? ") while not response or (response != 'E' and response != 'T'): response = raw_input("Please choose email (E) or text message (T): ") profile['prefers_email'] = (response == 'E') stt_engines = { "sphinx": None, "google": "GOOGLE_SPEECH" } response = raw_input("\nIf you would like to choose a specific STT " + "engine, please specify which.\nAvailable " + "implementations: %s. (Press Enter to default " + "to PocketSphinx): " % stt_engines.keys()) if (response in stt_engines): profile["stt_engine"] = response api_key_name = stt_engines[response] if api_key_name: key = raw_input("\nPlease enter your API key: ") profile["keys"] = {api_key_name: key} else: print("Unrecognized STT engine. Available implementations: %s" % stt_engines.keys()) profile["stt_engine"] = "sphinx" # write to profile print("Writing to profile...") if not os.path.exists(saschapath.CONFIG_PATH): os.makedirs(saschapath.CONFIG_PATH) outputFile = open(saschapath.config("profile.yml"), "w") yaml.dump(profile, outputFile, default_flow_style=False) print("Done.")