class RhasspyTestCore: def __init__(self, profile_name, train=True): self.profile_name = profile_name self.system_profiles_dir = os.path.join(os.getcwd(), "profiles") self.train = train def __enter__(self): self.user_profiles_dir = tempfile.TemporaryDirectory() self.core = RhasspyCore(self.profile_name, self.system_profiles_dir, self.user_profiles_dir.name) self.core.profile.set("wake.system", "dummy") self.core.start(preload=False) if self.train: self.core.train() return self.core def __exit__(self, *args): self.core.shutdown() try: self.user_profiles_dir.cleanup() except: pass
def start_rhasspy() -> None: """Create actor system and Rhasspy core.""" global core # Load core system = ActorSystem() core = RhasspyCore(args.profile, system_profiles_dir, user_profiles_dir, actor_system=system) # Set environment variables os.environ["RHASSPY_BASE_DIR"] = os.getcwd() os.environ["RHASSPY_PROFILE"] = core.profile.name os.environ["RHASSPY_PROFILE_DIR"] = core.profile.write_dir() # Add profile settings from the command line extra_settings = {} for key, value in args.set: try: value = json.loads(value) except: pass logger.debug("Profile: {0}={1}".format(key, value)) extra_settings[key] = value core.profile.set(key, value) # Load observer actor to catch intents observer = system.createActor(WebSocketObserver) system.ask(observer, ConfigureEvent(core.profile)) core.start(observer=observer) logger.info("Started")
def start_rhasspy() -> None: global core default_settings = Profile.load_defaults(profiles_dirs) # Get name of profile profile_name = ( args.profile or os.environ.get("RHASSPY_PROFILE", None) or pydash.get(default_settings, "rhasspy.default_profile", "en") ) # Load core core = RhasspyCore(profile_name, profiles_dirs) # Set environment variables os.environ["RHASSPY_BASE_DIR"] = os.getcwd() os.environ["RHASSPY_PROFILE"] = core.profile.name os.environ["RHASSPY_PROFILE_DIR"] = core.profile.write_dir() # Add profile settings from the command line extra_settings = {} for key, value in args.set: try: value = json.loads(value) except: pass logger.debug("Profile: {0}={1}".format(key, value)) extra_settings[key] = value core.profile.set(key, value) core.start() logger.info("Started")
def test_training(self): """Test training""" profile_name = "en" with tempfile.TemporaryDirectory(prefix="rhasspy_") as temp_dir: profiles_dirs = [temp_dir, "profiles"] core = RhasspyCore(profile_name, profiles_dirs, do_logging=True) core.profile.set("rhasspy.listen_on_start", False) core.profile.set("rhasspy.preload_profile", False) core.start() sentences_path = core.profile.write_path( core.profile.get("speech_to_text.sentences_ini") ) with open(sentences_path, "w") as sentences_file: print("[Foo]", file=sentences_file) print("foo bar", file=sentences_file) print("foo bar baz", file=sentences_file) core.train() with open("etc/test/what_time_is_it.wav", "rb") as wav_file: text = core.transcribe_wav(wav_file.read()).text assert text != "what time is it" # Add some more sentences with open(sentences_path, "a") as sentences_file: print("", file=sentences_file) print("[GetTime]", file=sentences_file) print("what time is it", file=sentences_file) core.train() with open("etc/test/what_time_is_it.wav", "rb") as wav_file: text = core.transcribe_wav(wav_file.read()).text assert text == "what time is it"
def start_rhasspy() -> None: global core default_settings = Profile.load_defaults(profiles_dirs) # Get name of profile profile_name = args.profile \ or os.environ.get('RHASSPY_PROFILE', None) \ or pydash.get(default_settings, 'rhasspy.default_profile', 'en') # Load core core = RhasspyCore(profile_name, profiles_dirs) # Set environment variables os.environ['RHASSPY_BASE_DIR'] = os.getcwd() os.environ['RHASSPY_PROFILE'] = core.profile.name os.environ['RHASSPY_PROFILE_DIR'] = core.profile.write_dir() # Add profile settings from the command line extra_settings = {} for key, value in args.set: try: value = json.loads(value) except: pass logger.debug('Profile: {0}={1}'.format(key, value)) extra_settings[key] = value core.profile.set(key, value) core.start()
class RhasspyTestCase(unittest.TestCase): def setUp(self): profile_name = "en" profiles_dirs = ["profiles"] self.core = RhasspyCore(profile_name, profiles_dirs, do_logging=False) self.core.profile.set("wake.system", "dummy") self.core.start() def tearDown(self): self.core.shutdown() # ------------------------------------------------------------------------- def test_transcribe(self): """speech -> text""" with open("etc/test/turn_on_living_room_lamp.wav", "rb") as wav_file: text = self.core.transcribe_wav(wav_file.read()).text assert text == "turn on the living room lamp" # ------------------------------------------------------------------------- def test_recognize(self): """text -> intent""" intent = self.core.recognize_intent("turn on the living room lamp").intent assert intent["intent"]["name"] == "ChangeLightState" entities = {e["entity"]: e["value"] for e in intent["entities"]} assert entities["name"] == "living room lamp" assert entities["state"] == "on" # ------------------------------------------------------------------------- def test_training(self): """Test training""" profile_name = "en" with tempfile.TemporaryDirectory(prefix="rhasspy_") as temp_dir: profiles_dirs = [temp_dir, "profiles"] core = RhasspyCore(profile_name, profiles_dirs, do_logging=True) core.profile.set("rhasspy.listen_on_start", False) core.profile.set("rhasspy.preload_profile", False) core.start() sentences_path = core.profile.write_path( core.profile.get("speech_to_text.sentences_ini") ) with open(sentences_path, "w") as sentences_file: print("[Foo]", file=sentences_file) print("foo bar", file=sentences_file) print("foo bar baz", file=sentences_file) core.train() with open("etc/test/what_time_is_it.wav", "rb") as wav_file: text = core.transcribe_wav(wav_file.read()).text assert text != "what time is it" # Add some more sentences with open(sentences_path, "a") as sentences_file: print("", file=sentences_file) print("[GetTime]", file=sentences_file) print("what time is it", file=sentences_file) core.train() with open("etc/test/what_time_is_it.wav", "rb") as wav_file: text = core.transcribe_wav(wav_file.read()).text assert text == "what time is it" # ------------------------------------------------------------------------- def test_pronounce(self): # Known word pronunciations = self.core.get_word_pronunciations(["test"], n=1).pronunciations assert pronunciations["test"]["pronunciations"][0] == "T EH S T" # Unknown word pronunciations = self.core.get_word_pronunciations( ["raxacoricofallipatorius"], n=1 ).pronunciations assert ( "R AE K S AH K AO R IH K AO F AE L AH P AH T AO R IY IH S" in pronunciations["raxacoricofallipatorius"]["pronunciations"] )