class TestTextToSpeechOperator(unittest.TestCase):
    def setUp(self):
        with patch(
            "airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook.__init__",
            new=mock_base_gcp_hook_default_project_id,
        ):
            self.gcp_speech_to_text_hook = GCPSpeechToTextHook(gcp_conn_id="test")

    @patch("airflow.contrib.hooks.gcp_speech_to_text_hook.GCPSpeechToTextHook.get_conn")
    def test_synthesize_speech(self, get_conn):
        recognize_method = get_conn.return_value.recognize
        recognize_method.return_value = None
        self.gcp_speech_to_text_hook.recognize_speech(config=CONFIG, audio=AUDIO)
        recognize_method.assert_called_once_with(config=CONFIG, audio=AUDIO, retry=None, timeout=None)
class TestTextToSpeechOperator(unittest.TestCase):
    def setUp(self):
        with mock.patch(
                "airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook.__init__",
                new=mock_base_gcp_hook_default_project_id,
        ):
            self.gcp_speech_to_text_hook = GCPSpeechToTextHook(
                gcp_conn_id="test")

    @mock.patch(
        "airflow.contrib.hooks.gcp_speech_to_text_hook.GCPSpeechToTextHook.get_conn"
    )
    def test_synthesize_speech(self, get_conn):
        recognize_method = get_conn.return_value.recognize
        recognize_method.return_value = None
        self.gcp_speech_to_text_hook.recognize_speech(config=CONFIG,
                                                      audio=AUDIO)
        recognize_method.assert_called_once_with(config=CONFIG,
                                                 audio=AUDIO,
                                                 retry=None,
                                                 timeout=None)
Esempio n. 3
0
    def execute(self, context):
        _speech_to_text_hook = GCPSpeechToTextHook(
            gcp_conn_id=self.gcp_conn_id)
        _translate_hook = CloudTranslateHook(gcp_conn_id=self.gcp_conn_id)

        recognize_result = _speech_to_text_hook.recognize_speech(
            config=self.config, audio=self.audio)
        recognize_dict = MessageToDict(recognize_result)

        self.log.info("Recognition operation finished")

        if len(recognize_dict['results']) == 0:
            self.log.info("No recognition results")
            return {}
        self.log.debug("recognition result: %s", recognize_dict)

        try:
            transcript = recognize_dict['results'][0]['alternatives'][0][
                'transcript']
        except KeyError as key:
            raise AirflowException(
                "Wrong response '{}' returned - it should contain {} field".
                format(recognize_dict, key))

        try:
            translation = _translate_hook.translate(
                values=transcript,
                target_language=self.target_language,
                format_=self.format_,
                source_language=self.source_language,
                model=self.model)
            self.log.info('translated output: %s', translation)
            return translation
        except ValueError as e:
            self.log.error(
                'An error has been thrown from translate speech method:')
            self.log.error(e)
            raise AirflowException(e)
 def execute(self, context):
     _hook = GCPSpeechToTextHook(gcp_conn_id=self.gcp_conn_id)
     return _hook.recognize_speech(config=self.config,
                                   audio=self.audio,
                                   retry=self.retry,
                                   timeout=self.timeout)
 def execute(self, context):
     _hook = GCPSpeechToTextHook(gcp_conn_id=self.gcp_conn_id)
     return _hook.recognize_speech(
         config=self.config, audio=self.audio, retry=self.retry, timeout=self.timeout
     )