Source code for tests.system.providers.google.cloud.automl.example_automl_nl_text_extraction

#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
"""
Example Airflow DAG that uses Google AutoML services.
"""

from __future__ import annotations

import os
from datetime import datetime

from google.cloud.aiplatform import schema
from google.protobuf.struct_pb2 import Value

from airflow.models.dag import DAG
from airflow.providers.google.cloud.hooks.automl import CloudAutoMLHook
from airflow.providers.google.cloud.operators.gcs import (
    GCSCreateBucketOperator,
    GCSDeleteBucketOperator,
    GCSSynchronizeBucketsOperator,
)
from airflow.providers.google.cloud.operators.vertex_ai.auto_ml import (
    CreateAutoMLTextTrainingJobOperator,
    DeleteAutoMLTrainingJobOperator,
)
from airflow.providers.google.cloud.operators.vertex_ai.dataset import (
    CreateDatasetOperator,
    DeleteDatasetOperator,
    ImportDataOperator,
)
from airflow.utils.trigger_rule import TriggerRule

[docs]ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID", "default")
[docs]GCP_PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT", "default")
[docs]DAG_ID = "example_automl_text_extr"
[docs]GCP_AUTOML_LOCATION = "us-central1"
[docs]RESOURCE_DATA_BUCKET = "airflow-system-tests-resources"
[docs]DATA_SAMPLE_GCS_BUCKET_NAME = f"bucket_{DAG_ID}_{ENV_ID}".replace("_", "-")
[docs]TEXT_EXTR_DISPLAY_NAME = f"{DAG_ID}-{ENV_ID}".replace("_", "-")
[docs]AUTOML_DATASET_BUCKET = f"gs://{DATA_SAMPLE_GCS_BUCKET_NAME}/automl/extraction.jsonl"
[docs]MODEL_NAME = f"{DAG_ID}-{ENV_ID}".replace("_", "-")
[docs]DATASET_NAME = f"ds_clss_{ENV_ID}".replace("-", "_")
[docs]DATASET = { "display_name": DATASET_NAME, "metadata_schema_uri": schema.dataset.metadata.text, "metadata": Value(string_value="extr-dataset"), }
[docs]DATA_CONFIG = [ { "import_schema_uri": schema.dataset.ioformat.text.extraction, "gcs_source": {"uris": [AUTOML_DATASET_BUCKET]}, }, ]
[docs]extract_object_id = CloudAutoMLHook.extract_object_id
# Example DAG for AutoML Natural Language Entities Extraction with DAG( DAG_ID, schedule="@once", # Override to match your needs start_date=datetime(2021, 1, 1), catchup=False, user_defined_macros={"extract_object_id": extract_object_id}, tags=["example", "automl", "text-extraction"], ) as dag:
[docs] create_bucket = GCSCreateBucketOperator( task_id="create_bucket", bucket_name=DATA_SAMPLE_GCS_BUCKET_NAME, storage_class="REGIONAL", location=GCP_AUTOML_LOCATION, )
move_dataset_file = GCSSynchronizeBucketsOperator( task_id="move_dataset_to_bucket", source_bucket=RESOURCE_DATA_BUCKET, source_object="vertex-ai/automl/datasets/text", destination_bucket=DATA_SAMPLE_GCS_BUCKET_NAME, destination_object="automl", recursive=True, ) create_extr_dataset = CreateDatasetOperator( task_id="create_extr_dataset", dataset=DATASET, region=GCP_AUTOML_LOCATION, project_id=GCP_PROJECT_ID, ) extr_dataset_id = create_extr_dataset.output["dataset_id"] import_extr_dataset = ImportDataOperator( task_id="import_extr_data", dataset_id=extr_dataset_id, region=GCP_AUTOML_LOCATION, project_id=GCP_PROJECT_ID, import_configs=DATA_CONFIG, ) # [START howto_cloud_create_text_extraction_training_job_operator] create_extr_training_job = CreateAutoMLTextTrainingJobOperator( task_id="create_extr_training_job", display_name=TEXT_EXTR_DISPLAY_NAME, prediction_type="extraction", multi_label=False, dataset_id=extr_dataset_id, model_display_name=MODEL_NAME, training_fraction_split=0.8, validation_fraction_split=0.1, test_fraction_split=0.1, sync=True, region=GCP_AUTOML_LOCATION, project_id=GCP_PROJECT_ID, ) # [END howto_cloud_create_text_extraction_training_job_operator] delete_extr_training_job = DeleteAutoMLTrainingJobOperator( task_id="delete_extr_training_job", training_pipeline_id=create_extr_training_job.output["training_id"], region=GCP_AUTOML_LOCATION, project_id=GCP_PROJECT_ID, trigger_rule=TriggerRule.ALL_DONE, ) delete_extr_dataset = DeleteDatasetOperator( task_id="delete_extr_dataset", dataset_id=extr_dataset_id, region=GCP_AUTOML_LOCATION, project_id=GCP_PROJECT_ID, trigger_rule=TriggerRule.ALL_DONE, ) delete_bucket = GCSDeleteBucketOperator( task_id="delete_bucket", bucket_name=DATA_SAMPLE_GCS_BUCKET_NAME, trigger_rule=TriggerRule.ALL_DONE, ) ( # TEST SETUP [create_bucket >> move_dataset_file, create_extr_dataset] # TEST BODY >> import_extr_dataset >> create_extr_training_job # TEST TEARDOWN >> delete_extr_training_job >> delete_extr_dataset >> delete_bucket ) from tests.system.utils.watcher import watcher # This test needs watcher in order to properly mark success/failure # when "tearDown" task with trigger rule is part of the DAG list(dag.tasks) >> watcher() from tests.system.utils import get_test_run # noqa: E402 # Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)
[docs]test_run = get_test_run(dag)

Was this entry helpful?