def test_run(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) @pipeline.event def on_step(context): pipeline.stop() pipeline.run()
def create(spokestack_id: str, spokestack_secret: str, sample_rate: int = 16000, frame_width: int = 20, **kwargs) -> SpeechPipeline: """ Args: spokestack_id (str): spokestack API id. spokestack_secret (str): Spokestack API secret. sample_rate (int): sample rate of the audio (Hz). frame_width (int): width of the audio frame: 10, 20, or 30 (ms). Returns: SpeechPipeline instance with profile configuration. """ pipeline = SpeechPipeline( input_source=PyAudioInput(sample_rate=sample_rate, frame_width=frame_width, **kwargs), stages=[ VoiceActivityDetector(sample_rate=sample_rate, frame_width=frame_width, **kwargs), VoiceActivityTrigger(), ActivationTimeout(frame_width=frame_width, **kwargs), CloudSpeechRecognizer(spokestack_id=spokestack_id, spokestack_secret=spokestack_secret, sample_rate=sample_rate, frame_width=frame_width, **kwargs), ], ) return pipeline
def test_events(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) @pipeline.event def on_speech(context): context.transcript = "event triggered" # test empty event pipeline.event(name="dummy_event") pipeline.context.event("speech") assert pipeline.context.transcript == "event triggered"
def test_start_stop(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) pipeline.start() assert pipeline.is_running # test second call to start, which ignores if the pipeline is running pipeline.start() pipeline.step() pipeline.close() assert not pipeline.is_running
def test_dispatch(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) pipeline.start() pipeline._dispatch() pipeline.is_managed = True pipeline._dispatch()
def test_activate_deactivate(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) pipeline.start() pipeline.activate() assert pipeline.context.is_active pipeline.deactivate() assert not pipeline.context.is_active
def test_cleanup(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) pipeline.start() assert pipeline.is_running pipeline.stop() assert not pipeline.is_running pipeline.cleanup() assert not pipeline._stages assert not pipeline._input_source
def test_cleanup(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) pipeline.start() assert pipeline.is_running pipeline.stop() assert not pipeline.is_running # run after stopped will trigger clean up pipeline.run() assert not pipeline._stages assert not pipeline._input_source
def create( spokestack_id: str, spokestack_secret: str, sample_rate: int = 16000, frame_width: int = 20, model_dir: str = "", **kwargs: Any, ) -> SpeechPipeline: """Creates a speech pipeline instance from profile Args: spokestack_id (str): spokestack API id. spokestack_secret (str): Spokestack API secret. sample_rate (int): sample rate of the audio (Hz). frame_width (int): width of the audio frame: 10, 20, or 30 (ms). model_dir (str): Directory containing the tflite wakeword models. Returns: """ pipeline = SpeechPipeline( input_source=PyAudioInput(frame_width=frame_width, sample_rate=sample_rate, **kwargs), stages=[ AutomaticGainControl(sample_rate=sample_rate, frame_width=frame_width), AutomaticNoiseSuppression(sample_rate=sample_rate), VoiceActivityDetector( frame_width=frame_width, sample_rate=sample_rate, **kwargs, ), WakewordTrigger(model_dir=model_dir, **kwargs), ActivationTimeout(frame_width=frame_width, **kwargs), CloudSpeechRecognizer( spokestack_secret=spokestack_secret, spokestack_id=spokestack_id, **kwargs, ), ], ) return pipeline
def create(classes: List[str], model_dir: str, sample_rate: int = 16000, frame_width: int = 20, **kwargs: Any) -> SpeechPipeline: """Create a speech pipeline instance from profile. Args: model_dir (str): Directory containing the tflite keyword models. classes: (List(str)): Classes for the keyword model to recognize sample_rate (int): sample rate of the audio (Hz). frame_width (int): width of the audio frame: 10, 20, or 30 (ms). """ pipeline = SpeechPipeline( input_source=PyAudioInput(frame_width=frame_width, sample_rate=sample_rate, **kwargs), stages=[ AutomaticGainControl(sample_rate=sample_rate, frame_width=frame_width), AutomaticNoiseSuppression(sample_rate=sample_rate), VoiceActivityDetector(sample_rate=sample_rate, frame_width=frame_width, **kwargs), VoiceActivityTrigger(), KeywordRecognizer( classes=classes, model_dir=model_dir, sample_rate=sample_rate, **kwargs, ), ActivationTimeout(frame_width=frame_width, **kwargs), ], ) return pipeline
def main(): pipeline = SpeechPipeline( PyAudioInput(frame_width=20, sample_rate=16000, exception_on_overflow=False), [ VoiceActivityDetector(), WakewordTrigger(pre_emphasis=0.97, model_dir="tflite"), GoogleSpeechRecognizer(GOOGLE_CREDS), ActivationTimeout(), ], ) dialogue_manager = DialogueManager( "tflite", "distilbert-base-cased-distilled-squad") manager = TextToSpeechManager( TextToSpeechClient(KEY_ID, KEY_SECRET), PyAudioOutput(), ) @pipeline.event def on_activate(context): print(context.is_active) @pipeline.event def on_recognize(context): pipeline.pause() answer = dialogue_manager(context.transcript) manager.synthesize(answer, "text", "demo-male") pipeline.resume() @pipeline.event def on_deactivate(context): print(context.is_active) manager.synthesize(dialogue_manager.greet(), "text", "demo-male") pipeline.start() pipeline.run()
def main(): pipeline = SpeechPipeline( PyAudioInput(frame_width=20, sample_rate=16000, exception_on_overflow=False), [ VoiceActivityDetector(), WakewordTrigger(pre_emphasis=0.97, model_dir="tflite"), CloudSpeechRecognizer(spokestack_id=KEY_ID, spokestack_secret=KEY_SECRET), ActivationTimeout(), ], ) nlu = TFLiteNLU("tflite") dialogue_manager = DialogueManager() manager = TextToSpeechManager( TextToSpeechClient(KEY_ID, KEY_SECRET), PyAudioOutput(), ) @pipeline.event def on_activate(context): print("active") @pipeline.event def on_recognize(context): pipeline.pause() results = nlu(context.transcript) response = dialogue_manager(results) if response: manager.synthesize(response, "text", "demo-male") pipeline.resume() if results.intent == "AMAZON.StopIntent": pipeline.stop() manager.synthesize(Response.WELCOME.value, "text", "demo-male") pipeline.start() pipeline.run()
def test_pause_resume(): stages = [ mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ] pipeline = SpeechPipeline(mock.MagicMock(), stages=stages) pipeline.start() assert pipeline.is_running pipeline.step() pipeline.pause() pipeline._input_source.stop.assert_called() # verify it does nothing pipeline.step() pipeline.resume() pipeline._input_source.start.assert_called() pipeline.close()