def test_post_http_error():
    client = TextToSpeechClient("", "", "")

    with mock.patch("spokestack.tts.clients.spokestack.requests") as patched:
        patched.post.return_value = MockResponse(status_code=201)
        with pytest.raises(Exception):
            _ = client.synthesize("utterance")
def test_synthesize_invalid_mode():
    client = TextToSpeechClient("", "", "")

    test = np.ones(160).tobytes()
    with mock.patch("spokestack.tts.clients.spokestack.requests") as patched:
        patched.get.return_value = mock.Mock(content=test)
        with pytest.raises(ValueError):
            _ = client.synthesize("test utterance", mode="python")
def test_synthesize_url():
    client = TextToSpeechClient("", "", "")

    with mock.patch("spokestack.tts.clients.spokestack.requests") as patched:
        mock_url = "https://test"
        patched.post.return_value = MockResponse(
            status_code=200,
            return_value={"data": {"synthesizeText": {"url": mock_url}}},
        )
        response = client.synthesize_url("# test utterance", mode="text")
        assert response == mock_url
Ejemplo n.º 4
0
def test_synthesize_markdown():
    client = TextToSpeechClient("", "", "")

    test = np.ones(160).tobytes()
    with mock.patch("spokestack.tts.clients.spokestack.requests") as patched:
        mock_iterable = mock.MagicMock(spec=Response().iter_content(),
                                       return_value=test)
        patched.post.return_value = MockResponse(status_code=200)
        patched.get.return_value = mock.Mock(iter_content=mock_iterable,
                                             status_code=200)
        response = client.synthesize("# test utterance", mode="markdown")
        assert response == test
Ejemplo n.º 5
0
def test_graphql():
    client = TextToSpeechClient("", "", "")
    voice = "voice"
    profile = "test"

    for mode in ["text", "ssml", "markdown"]:
        method = f"synthesize{mode[0].upper()}{mode[1:]}"
        body = client._build_body("test",
                                  mode=mode,
                                  voice=voice,
                                  profile=profile)
        assert f"{method}(" in body
        assert profile.upper() in body
def test_error_response():
    client = TextToSpeechClient("", "", "")

    with mock.patch("spokestack.tts.clients.spokestack.requests") as patched:
        patched.post.return_value = MockResponse(
            status_code=200,
            return_value={
                "data": {"synthesizeSSML": None},
                "errors": [
                    {
                        "locations": [{"column": 0, "line": 3}],
                        "message": "synthesis_failed",
                        "path": ["synthesizeSSML"],
                    }
                ],
            },
        )

        with pytest.raises(TTSError):
            _ = client.synthesize("utterance")
Ejemplo n.º 7
0
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()
Ejemplo n.º 8
0
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()