From f418339253e64a1ebacf95b9560c9b4dbf4da403 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Tue, 2 Mar 2021 07:58:52 -0800 Subject: [PATCH] update samples - optional src language --- .../samples/sample_azure_storage.py | 7 +++++-- .../samples/sample_check_statuses.py | 15 +++++++++------ .../samples/sample_list_batches.py | 10 +++++++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py index 6501fa7b4d06..dcbb9ec80298 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_azure_storage.py @@ -59,7 +59,6 @@ def batch_translation_with_storage(): batch = [ BatchDocumentInput( source_url=source_container_url, - source_language="en", targets=[ StorageTarget( target_url=target_container_url, @@ -78,7 +77,7 @@ def batch_translation_with_storage(): if job_result.documents_failed_count > 0: check_documents(translation_client, job_result.id) - if job_result.status in ["Failed", "ValidationFailed"]: + elif job_result.status in ["Failed", "ValidationFailed"]: if job_result.error: print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) check_documents(translation_client, job_result.id) @@ -117,3 +116,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + + +if __name__ == '__main__': + batch_translation_with_storage() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py index 4d47f3ca8821..596b373db915 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_statuses.py @@ -5,7 +5,7 @@ # ------------------------------------ -def sample_batch_translation(): +def sample_translation_status_checks(): import os import time from azure.core.credentials import AzureKeyCredential @@ -26,7 +26,6 @@ def sample_batch_translation(): batch = [ BatchDocumentInput( source_url=source_container_url, - source_language="en", targets=[ StorageTarget( target_url=target_container_url_es, @@ -47,16 +46,16 @@ def sample_batch_translation(): while True: job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail if job_detail.status in ["NotStarted", "Running"]: - time.sleep(10) + time.sleep(30) continue - if job_detail.status in ["Failed", "ValidationFailed"]: + elif job_detail.status in ["Failed", "ValidationFailed"]: if job_detail.error: print("Translation job failed: {}: {}".format(job_detail.error.code, job_detail.error.message)) check_documents(client, job_detail.id) exit(1) - if job_detail.status == "Succeeded": + elif job_detail.status == "Succeeded": print("We translated our documents!") if job_detail.documents_failed_count > 0: check_documents(client, job_detail.id) @@ -69,7 +68,7 @@ def check_documents(client, job_id): try: doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail] except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container.") + print("Failed to process any documents in source/target container due to insufficient permissions.") raise err docs_to_retry = [] @@ -83,3 +82,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + + +if __name__ == '__main__': + sample_translation_status_checks() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py index cc2495de8e9e..2961b9516301 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_batches.py @@ -16,14 +16,18 @@ def sample_list_all_jobs(): key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - jobs = client.list_submitted_jobs() + jobs = client.list_submitted_jobs() # type: ItemPaged[JobStatusDetail] for job in jobs: + if job.status in ["NotStarted", "Running"]: + job = client.wait_until_done(job.id) + print("Job ID: {}".format(job.id)) print("Job status: {}".format(job.status)) print("Job created on: {}".format(job.created_on)) print("Job last updated on: {}".format(job.last_updated_on)) print("Total number of translations on documents: {}".format(job.documents_total_count)) + print("Total number of characters charged: {}".format(job.total_characters_charged)) print("Of total documents...") print("{} failed".format(job.documents_failed_count)) @@ -31,3 +35,7 @@ def sample_list_all_jobs(): print("{} in progress".format(job.documents_in_progress_count)) print("{} not yet started".format(job.documents_not_yet_started_count)) print("{} cancelled".format(job.documents_cancelled_count)) + + +if __name__ == '__main__': + sample_list_all_jobs()