def execute(self, context):
     _hook = CloudTranslateHook(gcp_conn_id=self.gcp_conn_id)
     try:
         translation = _hook.translate(
             values=self.values,
             target_language=self.target_language,
             format_=self.format_,
             source_language=self.source_language,
             model=self.model,
         )
         self.log.debug("Translation %s", translation)
         return translation
     except ValueError as e:
         self.log.error('An error has been thrown from translate method:')
         self.log.error(e)
         raise AirflowException(e)
 def execute(self, context):
     _hook = CloudTranslateHook(gcp_conn_id=self.gcp_conn_id)
     try:
         translation = _hook.translate(
             values=self.values,
             target_language=self.target_language,
             format_=self.format_,
             source_language=self.source_language,
             model=self.model,
         )
         self.log.debug("Translation %s", translation)
         return translation
     except ValueError as e:
         self.log.error('An error has been thrown from translate method:')
         self.log.error(e)
         raise AirflowException(e)
Exemple #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)
Exemple #4
0
class TestCloudTranslateHook(unittest.TestCase):
    def setUp(self):
        with mock.patch(
                'airflow.contrib.hooks.gcp_translate_hook.CloudTranslateHook.__init__',
                new=mock_base_gcp_hook_default_project_id,
        ):
            self.hook = CloudTranslateHook(gcp_conn_id='test')

    @mock.patch(
        'airflow.contrib.hooks.gcp_translate_hook.CloudTranslateHook.get_conn')
    def test_translate_called(self, get_conn):
        # Given
        translate_method = get_conn.return_value.translate
        translate_method.return_value = {
            'translatedText': 'Yellowing self Gęśle',
            'detectedSourceLanguage': 'pl',
            'model': 'base',
            'input': 'zażółć gęślą jaźń',
        }
        # When
        result = self.hook.translate(
            values=['zażółć gęślą jaźń'],
            target_language='en',
            format_='text',
            source_language=None,
            model='base',
        )
        # Then
        self.assertEqual(
            result,
            {
                'translatedText': 'Yellowing self Gęśle',
                'detectedSourceLanguage': 'pl',
                'model': 'base',
                'input': 'zażółć gęślą jaźń',
            },
        )
        translate_method.assert_called_once_with(
            values=['zażółć gęślą jaźń'],
            target_language='en',
            format_='text',
            source_language=None,
            model='base',
        )
class TestCloudTranslateHook(unittest.TestCase):
    def setUp(self):
        with mock.patch(
            'airflow.contrib.hooks.gcp_translate_hook.CloudTranslateHook.__init__',
            new=mock_base_gcp_hook_default_project_id,
        ):
            self.hook = CloudTranslateHook(gcp_conn_id='test')

    @mock.patch('airflow.contrib.hooks.gcp_translate_hook.CloudTranslateHook.get_conn')
    def test_translate_called(self, get_conn):
        # Given
        translate_method = get_conn.return_value.translate
        translate_method.return_value = {
            'translatedText': 'Yellowing self Gęśle',
            'detectedSourceLanguage': 'pl',
            'model': 'base',
            'input': 'zażółć gęślą jaźń',
        }
        # When
        result = self.hook.translate(
            values=['zażółć gęślą jaźń'],
            target_language='en',
            format_='text',
            source_language=None,
            model='base',
        )
        # Then
        self.assertEqual(
            result,
            {
                'translatedText': 'Yellowing self Gęśle',
                'detectedSourceLanguage': 'pl',
                'model': 'base',
                'input': 'zażółć gęślą jaźń',
            },
        )
        translate_method.assert_called_once_with(
            values=['zażółć gęślą jaźń'],
            target_language='en',
            format_='text',
            source_language=None,
            model='base',
        )
Exemple #6
0
 def setUp(self):
     with mock.patch(
             'airflow.contrib.hooks.gcp_translate_hook.CloudTranslateHook.__init__',
             new=mock_base_gcp_hook_default_project_id,
     ):
         self.hook = CloudTranslateHook(gcp_conn_id='test')
 def setUp(self):
     with mock.patch(
         'airflow.contrib.hooks.gcp_translate_hook.CloudTranslateHook.__init__',
         new=mock_base_gcp_hook_default_project_id,
     ):
         self.hook = CloudTranslateHook(gcp_conn_id='test')