gopidesupavan commented on code in PR #40287: URL: https://github.com/apache/airflow/pull/40287#discussion_r1645011338
########## tests/system/providers/amazon/aws/example_comprehend_document_classifier.py: ########## @@ -0,0 +1,234 @@ +# 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. +from __future__ import annotations + +import os +import tempfile +from datetime import datetime +from urllib.request import urlretrieve + +from airflow import DAG +from airflow.decorators import task, task_group +from airflow.models.baseoperator import chain +from airflow.providers.amazon.aws.hooks.comprehend import ComprehendHook +from airflow.providers.amazon.aws.hooks.s3 import S3Hook +from airflow.providers.amazon.aws.operators.comprehend import ( + ComprehendCreateDocumentClassifierOperator, +) +from airflow.providers.amazon.aws.operators.s3 import ( + S3CreateBucketOperator, + S3CreateObjectOperator, + S3DeleteBucketOperator, +) +from airflow.providers.amazon.aws.sensors.comprehend import ( + ComprehendCreateDocumentClassifierCompletedSensor, +) +from airflow.utils.trigger_rule import TriggerRule +from tests.system.providers.amazon.aws.utils import SystemTestContextBuilder + +ROLE_ARN_KEY = "ROLE_ARN" +sys_test_context_task = SystemTestContextBuilder().add_variable(ROLE_ARN_KEY).build() + +DAG_ID = "example_comprehend_document_classifier" +ANNOTATION_BUCKET_KEY = "training-labels/label.csv" +TRAINING_DATA_PREFIX = "training-docs" + +# To create a custom document classifier, we need a minimum of 10 documents for each label. +# for testing purpose, we will generate 10 copies of each document referenced below. +PUBLIC_DATA_SOURCES = [ + { + "fileName": "discharge-summary.pdf", + "url": "https://github.com/aws-samples/amazon-comprehend-examples/blob/master/building-custom-classifier/sample-docs/discharge-summary.pdf?raw=true", + }, + { + "fileName": "doctors-notes.pdf", + "url": "https://github.com/aws-samples/amazon-comprehend-examples/blob/master/building-custom-classifier/sample-docs/doctors-notes.pdf?raw=true", + }, +] + +# Annotations file won't allow headers +# label,document name,page number + +ANNOTATIONS = """DISCHARGE_SUMMARY,discharge-summary-0.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-1.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-2.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-3.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-4.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-5.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-6.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-7.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-8.pdf,1 +DISCHARGE_SUMMARY,discharge-summary-9.pdf,1 +DOCTOR_NOTES,doctors-notes-0.pdf,1 +DOCTOR_NOTES,doctors-notes-1.pdf,1 +DOCTOR_NOTES,doctors-notes-2.pdf,1 +DOCTOR_NOTES,doctors-notes-3.pdf,1 +DOCTOR_NOTES,doctors-notes-4.pdf,1 +DOCTOR_NOTES,doctors-notes-5.pdf,1 +DOCTOR_NOTES,doctors-notes-6.pdf,1 +DOCTOR_NOTES,doctors-notes-7.pdf,1 +DOCTOR_NOTES,doctors-notes-8.pdf,1 +DOCTOR_NOTES,doctors-notes-9.pdf,1""" + + +@task_group +def document_classifier_workflow(): + # [START howto_operator_create_document_classifier] + create_document_classifier = ComprehendCreateDocumentClassifierOperator( + task_id="create_document_classifier", + document_classifier_name=classifier_name, + input_data_config=input_data_configurations, + output_data_config=output_data_configurations, + mode="MULTI_CLASS", + data_access_role_arn=test_context[ROLE_ARN_KEY], + language_code="en", + document_classifier_kwargs=document_classifier_kwargs, + ) + # [END howto_operator_create_document_classifier] + create_document_classifier.wait_for_completion = False + + # [START howto_sensor_create_document_classifier] + await_create_document_classifier = ComprehendCreateDocumentClassifierCompletedSensor( + task_id="await_create_document_classifier", document_classifier_arn=create_document_classifier.output + ) + # [END howto_sensor_create_document_classifier] + + @task + def delete_classifier(document_classifier_arn: str): + ComprehendHook().conn.delete_document_classifier(DocumentClassifierArn=document_classifier_arn) + + chain( + create_document_classifier, + await_create_document_classifier, + delete_classifier(create_document_classifier.output), + ) + + +@task +def copy_data_to_s3(bucket: str, sources: list[dict], prefix: str, number_of_copies=1): Review Comment: These are actually useful here, somehow i forgot. I have updated now, copy data to use HttpToS3Operator. Thank you @vincbeck. 😄 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org