Esempio n. 1
0
    def test_missing_audio(self, mock_hook):
        mock_hook.return_value.recognize_speech.return_value = True

        with pytest.raises(AirflowException) as ctx:
            CloudSpeechToTextRecognizeSpeechOperator(  # pylint: disable=missing-kwoa
                project_id=PROJECT_ID, gcp_conn_id=GCP_CONN_ID, config=CONFIG, task_id="id"
            ).execute(context={"task_instance": Mock()})

        err = ctx.value
        assert "audio" in str(err)
        mock_hook.assert_not_called()
    def test_missing_config(self, mock_hook):
        mock_hook.return_value.recognize_speech.return_value = True

        with self.assertRaises(AirflowException) as e:
            CloudSpeechToTextRecognizeSpeechOperator(  # pylint: disable=missing-kwoa
                project_id=PROJECT_ID, gcp_conn_id=GCP_CONN_ID, audio=AUDIO, task_id="id"
            ).execute(context={"task_instance": Mock()})

        err = e.exception
        self.assertIn("config", str(err))
        mock_hook.assert_not_called()
Esempio n. 3
0
    def test_recognize_speech_green_path(self, mock_hook):
        mock_hook.return_value.recognize_speech.return_value = True

        CloudSpeechToTextRecognizeSpeechOperator(  # pylint: disable=no-value-for-parameter
            project_id=PROJECT_ID,
            gcp_conn_id=GCP_CONN_ID,
            config=CONFIG,
            audio=AUDIO,
            task_id="id").execute(context={"task_instance": Mock()})

        mock_hook.assert_called_once_with(gcp_conn_id=GCP_CONN_ID)
        mock_hook.return_value.recognize_speech.assert_called_once_with(
            config=CONFIG, audio=AUDIO, retry=None, timeout=None)
CONFIG = {"encoding": "LINEAR16", "language_code": "en_US"}
AUDIO = {"uri": "gs://{bucket}/{object}".format(bucket=BUCKET_NAME, object=FILENAME)}
# [END howto_operator_speech_to_text_api_arguments]

default_args = {"start_date": dates.days_ago(1)}

with models.DAG(
    "example_gcp_speech_to_text",
    default_args=default_args,
    schedule_interval=None,  # Override to match your needs
    tags=['example'],
) as dag:
    text_to_speech_synthesize_task = CloudTextToSpeechSynthesizeOperator(
        project_id=GCP_PROJECT_ID,
        input_data=INPUT,
        voice=VOICE,
        audio_config=AUDIO_CONFIG,
        target_bucket_name=BUCKET_NAME,
        target_filename=FILENAME,
        task_id="text_to_speech_synthesize_task",
    )
    # [START howto_operator_speech_to_text_recognize]
    speech_to_text_recognize_task2 = CloudSpeechToTextRecognizeSpeechOperator(
        config=CONFIG,
        audio=AUDIO,
        task_id="speech_to_text_recognize_task"
    )
    # [END howto_operator_speech_to_text_recognize]

    text_to_speech_synthesize_task >> speech_to_text_recognize_task2
Esempio n. 5
0
    # [START howto_operator_text_to_speech_synthesize]
    text_to_speech_synthesize_task = CloudTextToSpeechSynthesizeOperator(
        project_id=GCP_PROJECT_ID,
        input_data=INPUT,
        voice=VOICE,
        audio_config=AUDIO_CONFIG,
        target_bucket_name=BUCKET_NAME,
        target_filename=FILENAME,
        task_id="text_to_speech_synthesize_task",
    )
    # [END howto_operator_text_to_speech_synthesize]

    # [START howto_operator_speech_to_text_recognize]
    speech_to_text_recognize_task = CloudSpeechToTextRecognizeSpeechOperator(
        project_id=GCP_PROJECT_ID,
        config=CONFIG,
        audio=AUDIO,
        task_id="speech_to_text_recognize_task")
    # [END howto_operator_speech_to_text_recognize]

    text_to_speech_synthesize_task >> speech_to_text_recognize_task

    # [START howto_operator_translate_speech]
    translate_speech_task = GcpTranslateSpeechOperator(
        project_id=GCP_PROJECT_ID,
        audio=AUDIO,
        config=CONFIG,
        target_language=TARGET_LANGUAGE,
        format_=FORMAT,
        source_language=SOURCE_LANGUAGE,
        model=MODEL,