diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller.py b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller.py index 043b058baf83..f57ff95e0927 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/azure/ai/documenttranslation/_poller.py @@ -62,6 +62,11 @@ def from_continuation_token(cls, polling_method, continuation_token, **kwargs): pass # returns the batch ID + @classmethod + def from_batch_id(batch_id): + # creates a poller from a batch id + pass + class DocTranslationLROPolling(PollingMethod): # pylint: disable=too-many-instance-attributes diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_batch_translation.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_batch_translation.py new file mode 100644 index 000000000000..5ac92c2574ae --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_batch_translation.py @@ -0,0 +1,69 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def sample_batch_translation(): + # import libraries + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + BatchDocumentInput, + StorageTarget, + ) + + # get service secrets + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_container_url_en = os.environ["AZURE_SOURCE_CONTAINER_URL_EN"] + target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] + + # create translation client + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + # submit documents for translation + operation = client.begin_translation( + [ + BatchDocumentInput( + source_url=source_container_url_en, + targets=[ + StorageTarget(target_url=target_container_url_es, language="es"), + StorageTarget(target_url=target_container_url_fr, language="fr"), + ], + ) + ] + ) # type: DocumentTranslationPoller[ItemPaged[DocumentStatusDetail]] + + # get final status + doc_statuses = operation.result() # type: ItemPaged[DocumentStatusDetail] + translation_details = operation.details # type: TranslationStatusDetail + + # print status + print("Translation status: {}".format(translation_details.status)) + print("Translation created on: {}".format(translation_details.created_on)) + print("Translation last updated on: {}".format(translation_details.last_updated_on)) + print("Total number of translations on documents: {}".format(translation_details.documents_total_count)) + + print("Of total documents...") + print("{} failed".format(translation_details.documents_failed_count)) + print("{} succeeded".format(translation_details.documents_succeeded_count)) + print("{} in progress".format(translation_details.documents_in_progress_count)) + print("{} not yet started".format(translation_details.documents_not_yet_started_count)) + print("{} cancelled".format(translation_details.documents_cancelled_count)) + + for document in doc_statuses: + print("Document with ID: {} has status: {}".format(document.id, document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.url)) + print("Translated to langauge: {}".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}".format(document.error.code, document.error.message)) + + + +if __name__ == '__main__': + sample_batch_translation() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_list_all_submitted_translations.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_list_all_submitted_translations.py new file mode 100644 index 000000000000..5a39e7b38ce7 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_list_all_submitted_translations.py @@ -0,0 +1,47 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def sample_list_all_submitted_jobs(): + # import libraries + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + ) + + # get service secrets + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + + # create translation client + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + # list submitted translations + translation_operations = client.list_submitted_translations() # type: ItemPaged[TranslationStatusDetail] + + for translation_operation in translation_operations: + if translation_operation.status in ["NotStarted", "Running"]: + poller = client.begin_translation(None, continuation_token=translation_operation.id) + translation_operation = poller.result() + + print("Translation ID: {}".format(translation_operation.id)) + print("Translation status: {}".format(translation_operation.status)) + print("Translation created on: {}".format(translation_operation.created_on)) + print("Translation last updated on: {}".format(translation_operation.last_updated_on)) + print("Total number of translations on documents: {}".format(translation_operation.documents_total_count)) + print("Total number of characters charged: {}".format(translation_operation.total_characters_charged)) + + print("Of total documents...") + print("{} failed".format(translation_operation.documents_failed_count)) + print("{} succeeded".format(translation_operation.documents_succeeded_count)) + print("{} in progress".format(translation_operation.documents_in_progress_count)) + print("{} not yet started".format(translation_operation.documents_not_yet_started_count)) + print("{} cancelled".format(translation_operation.documents_cancelled_count)) + + +if __name__ == '__main__': + sample_list_all_submitted_jobs() \ No newline at end of file diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_translation_status_checks.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_translation_status_checks.py new file mode 100644 index 000000000000..e5604c037586 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/arch-board/sample_translation_status_checks.py @@ -0,0 +1,56 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + + +def sample_translation_status_checks(): + # import libraries + import os + import time + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + BatchDocumentInput, + StorageTarget + ) + + # get service secrets + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + source_container_url_en = os.environ["AZURE_SOURCE_CONTAINER_URL_EN"] + target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] + + # create translation client + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + # submit docs for translation + operation = client.begin_translation( + [ + BatchDocumentInput( + source_url=source_container_url_en, + targets=[ + StorageTarget(target_url=target_container_url_es, language="es"), + StorageTarget(target_url=target_container_url_fr, language="fr") + ] + ) + ] + ) # type: DocumentTranslationPoller[ItemPaged[DocumentStatusDetail]] + + # check result + completed_docs = [] + running_state = ["NotStarted", "Running"] + + while operation.details.status in running_state: + time.sleep(30) + for doc in client.list_documents_statuses(operation.batch_id): + if doc.id not in completed_docs and doc.status not in running_state: + completed_docs.append(doc.id) + print("Document at {} completed with status: {}".format(doc.url, doc.status)) + + print("Translation batch completed.") + +if __name__ == '__main__': + sample_translation_status_checks() \ No newline at end of file diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_async.py deleted file mode 100644 index 5e9897646319..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_async.py +++ /dev/null @@ -1,113 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import os -import asyncio - - -class BatchTranslationSampleAsync(object): - - async def batch_translation_async(self): - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient - from azure.ai.documenttranslation import ( - BatchDocumentInput, - StorageTarget - ) - - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url_en = os.environ["AZURE_SOURCE_CONTAINER_URL_EN"] - source_container_url_de = os.environ["AZURE_SOURCE_CONTAINER_URL_DE"] - target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] - target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] - - # create service client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - # prepare translation job input - batch = [ - BatchDocumentInput( - source_url=source_container_url_en, - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ), - StorageTarget( - target_url=target_container_url_fr, - language="fr" - ) - ] - ), - BatchDocumentInput( - source_url=source_container_url_de, - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ), - StorageTarget( - target_url=target_container_url_fr, - language="fr" - ) - ] - ) - ] - - # run translation job - async with client: - job_detail = await client.create_translation_job(batch) # type: JobStatusDetail - - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) - - # get job result - job_result = await client.wait_until_done(job_detail.id) # type: JobStatusDetail - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - await self.check_documents(client, job_result.id) - - elif job_result.status in ["Failed", "ValidationFailed"]: - if job_result.error: - print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) - await self.check_documents(client, job_result.id) - exit(1) - - - async def check_documents(self, client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[DocumentStatusDetail] - except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container due to insufficient permissions.") - raise err - - docs_to_retry = [] - async for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - - -async def main(): - sample = BatchTranslationSampleAsync() - await sample.batch_translation_async() - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_with_storage_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_with_storage_async.py deleted file mode 100644 index fa8af0255101..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_with_storage_async.py +++ /dev/null @@ -1,142 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import os -import asyncio - - -class BatchTranslationWithStorageSampleAsync(object): - - async def batch_translation_with_storage_async(self): - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient - from azure.ai.documenttranslation import ( - BatchDocumentInput, - StorageTarget - ) - from azure.storage.blob.aio import ContainerClient - from azure.storage.blob import ( - generate_container_sas, - ContainerSasPermissions - ) - - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_storage_endpoint = os.environ["AZURE_STORAGE_SOURCE_ENDPOINT"] - source_storage_account_name = os.environ["AZURE_STORAGE_SOURCE_ACCOUNT_NAME"] - source_storage_container_name = os.environ["AZURE_STORAGE_SOURCE_CONTAINER_NAME"] - source_storage_key = os.environ["AZURE_STORAGE_SOURCE_KEY"] - target_storage_endpoint = os.environ["AZURE_STORAGE_TARGET_ENDPOINT"] - target_storage_account_name = os.environ["AZURE_STORAGE_TARGET_ACCOUNT_NAME"] - target_storage_container_name = os.environ["AZURE_STORAGE_TARGET_CONTAINER_NAME"] - target_storage_key = os.environ["AZURE_STORAGE_TARGET_KEY"] - - # create service clients - translation_client = DocumentTranslationClient( - endpoint, AzureKeyCredential(key) - ) - - container_client = ContainerClient( - source_storage_endpoint, - container_name=source_storage_container_name, - credential=source_storage_key - ) - - # upload some document for translation - with open("document.txt", "rb") as doc: - await container_client.upload_blob(name="document.txt", data=doc) - - # prepare translation job input - source_container_sas = generate_container_sas( - account_name=source_storage_account_name, - container_name=source_storage_container_name, - account_key=source_storage_key, - permission=ContainerSasPermissions.from_string("rl") - ) - - target_container_sas = generate_container_sas( - account_name=target_storage_account_name, - container_name=target_storage_container_name, - account_key=target_storage_key, - permission=ContainerSasPermissions.from_string("rlwd") - ) - - source_container_url = source_storage_endpoint + "/" + source_storage_container_name + "?" + source_container_sas - target_container_url = target_storage_endpoint + "/" + target_storage_container_name + "?" + target_container_sas - - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url, - language="es" - ) - ], - prefix="document" - ) - ] - - # run job - async with translation_client: - job_detail = await translation_client.create_translation_job(batch) - job_result = await translation_client.wait_until_done(job_detail.id) - - # poll status result - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - await self.check_documents(translation_client, job_result.id) - - elif job_result.status in ["Failed", "ValidationFailed"]: - if job_result.error: - print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) - await self.check_documents(translation_client, job_result.id) - exit(1) - - # store result documents - container_client = ContainerClient( - target_storage_endpoint, - container_name=target_storage_container_name, - credential=target_storage_key - ) - - with open("translated.txt", "wb") as my_blob: - download_stream = await container_client.download_blob("document.txt") - my_blob.write(await download_stream.readall()) - - - async def check_documents(self, client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[DocumentStatusDetail] - except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container due to insufficient permissions.") - raise err - - docs_to_retry = [] - async for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - -async def main(): - sample = BatchTranslationWithStorageSampleAsync() - await sample.batch_translation_with_storage_async() - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_cancel_translation_job_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_cancel_translation_job_async.py deleted file mode 100644 index 2cb61c61e33d..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_cancel_translation_job_async.py +++ /dev/null @@ -1,67 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import os -import asyncio - -class CancelTranslationJobSampleAsync(object): - - async def cancel_translation_job_async(self): - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient - from azure.ai.documenttranslation import ( - BatchDocumentInput, - StorageTarget - ) - - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] - target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] - - # prepare translation job input - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ) - ], - storage_type="file" - ) - ] - - # create translation client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - # run job - async with client: - job_detail = await client.create_translation_job(batch) - - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) - - await client.cancel_job(job_detail.id) - job_detail = await client.get_job_status(job_detail.id) # type: JobStatusDetail - - if job_detail.status in ["Cancelled", "Cancelling"]: - print("We cancelled job with ID: {}".format(job_detail.id)) - - -async def main(): - sample = CancelTranslationJobSampleAsync() - await sample.cancel_translation_job_async() - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) - - diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_custom_translation_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_custom_translation_async.py deleted file mode 100644 index 6d0ec8d2d815..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_custom_translation_async.py +++ /dev/null @@ -1,96 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import os -import asyncio - -class CustomTranslationSampleAsync(object): - - async def custom_translation_async(self): - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient - from azure.ai.documenttranslation import ( - BatchDocumentInput, - StorageTarget - ) - - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] - target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] - category_id = os.environ["AZURE_DOCUMENT_TRANSLATION_MODEL_ID"] - - # prepare translation job input - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url_fr, - language="fr", - category_id=category_id, - glossaries=["https://exampleglossary"] - ) - ], - prefix="document_2021" - ) - ] - - # create translation client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - # run translation job - async with client: - job_detail = await client.create_translation_job(batch) - - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) - - job_result = await client.wait_until_done(job_detail.id) # type: JobStatusDetail - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - await self.check_documents(client, job_result.id) - - elif job_result.status in ["Failed", "ValidationFailed"]: - if job_result.error: - print("Translation job failed: {}: {}".format(job_result.error.code, job_result.error.message)) - await self.check_documents(client, job_result.id) - exit(1) - - - async def check_documents(self, client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[DocumentStatusDetail] - except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container due to insufficient permissions.") - raise err - - docs_to_retry = [] - async for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - - -async def main(): - sample = CustomTranslationSampleAsync() - await sample.custom_translation_async() - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_list_all_submitted_jobs_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_list_all_submitted_jobs_async.py deleted file mode 100644 index ecb983cfe869..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_list_all_submitted_jobs_async.py +++ /dev/null @@ -1,54 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import os -import asyncio - -class ListAllSubmittedJobsSampleAsync(object): - - def list_all_submitted_jobs(self): - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient - - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - - # create translation client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - # list submitted jobs - jobs = client.list_submitted_jobs() # type: AsyncItemPaged[JobStatusDetail] - - async for job in jobs: - # wait for job to finish - 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)) - print("{} succeeded".format(job.documents_succeeded_count)) - 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)) - - -async def main(): - sample = ListAllSubmittedJobsSampleAsync() - await sample.list_all_submitted_jobs() - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_status_checks_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_status_checks_async.py deleted file mode 100644 index 3e0dcef5b8bb..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_status_checks_async.py +++ /dev/null @@ -1,103 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -import os -import asyncio -import time - -class TranslationStatusChecksSampleAsync(object): - - async def translation_status_checks_async(self): - - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient - from azure.ai.documenttranslation import ( - BatchDocumentInput, - StorageTarget - ) - - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] - target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] - target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] - - # prepare translation input - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ), - StorageTarget( - target_url=target_container_url_fr, - language="fr" - ) - ], - storage_type="folder", - prefix="document_2021" - ) - ] - - # create translation client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - # run translation job - async with client: - job_detail = await client.create_translation_job(batch) - while True: - job_detail = await client.get_job_status(job_detail.id) # type: JobStatusDetail - if job_detail.status in ["NotStarted", "Running"]: - await asyncio.sleep(30) - continue - - elif job_detail.status in ["Failed", "ValidationFailed"]: - if job_detail.error: - print("Translation job failed: {}: {}".format(job_detail.error.code, job_detail.error.message)) - await self.check_documents(client, job_detail.id) - exit(1) - - elif job_detail.status == "Succeeded": - print("We translated our documents!") - if job_detail.documents_failed_count > 0: - await self.check_documents(client, job_detail.id) - break - - - async def check_documents(self, client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[DocumentStatusDetail] - except ResourceNotFoundError as err: - print("Failed to process any documents in source/target container due to insufficient permissions.") - raise err - - docs_to_retry = [] - async for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - - -async def main(): - sample = TranslationStatusChecksSampleAsync() - await sample.translation_status_checks_async() - - -if __name__ == '__main__': - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py index 14bdd62b2d5b..bd235cb7c3e4 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py @@ -6,6 +6,7 @@ def sample_batch_translation(): + # import libraries import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( @@ -14,6 +15,7 @@ def sample_batch_translation(): StorageTarget ) + # get service secrets endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] source_container_url_en = os.environ["AZURE_SOURCE_CONTAINER_URL_EN"] @@ -21,8 +23,10 @@ def sample_batch_translation(): target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] + # create translation client client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + # prepare translation input batch = [ BatchDocumentInput( source_url=source_container_url_en, @@ -52,33 +56,32 @@ def sample_batch_translation(): ) ] - job_detail = client.create_translation_job(batch) # type: JobStatusDetail + # submit documents for translation + poller = client.begin_translation(batch) # type: DocumentTranslationPoller[ItemPaged[DocumentStatusDetail]] + + # initial status + translation_details = poller.details # type: TranslationStatusDetail + print("Translation initial status: {}".format(translation_details.status)) + print("Number of translations on documents: {}".format(translation_details.documents_total_count)) - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) - - job_result = client.wait_until_done(job_detail.id) # type: JobStatusDetail - if job_result.status == "Succeeded": + # get final status + doc_statuses = poller.result() # type: ItemPaged[DocumentStatusDetail] + translation_details = poller.details # type: TranslationStatusDetail + if translation_details.status == "Succeeded": print("We translated our documents!") - if job_result.documents_failed_count > 0: - check_documents(client, job_result.id) + if translation_details.documents_failed_count > 0: + docs_to_retry = check_documents(doc_statuses) + # do something with failed docs - 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(client, job_result.id) + elif translation_details.status in ["Failed", "ValidationFailed"]: + if translation_details.error: + print("Translation job failed: {}: {}".format(translation_details.error.code, translation_details.error.message)) + docs_to_retry = check_documents(doc_statuses) + # do something with failed docs exit(1) -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - 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 due to insufficient permissions.") - raise err - +def check_documents(doc_statuses): docs_to_retry = [] for document in doc_statuses: if document.status == "Failed": @@ -90,6 +93,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + return docs_to_retry if __name__ == '__main__': diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation_with_storage.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation_with_storage.py index d0968c411378..b6bf7df6b254 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation_with_storage.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation_with_storage.py @@ -6,6 +6,7 @@ def sample_batch_translation_with_storage(): + # import libraries import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( @@ -15,6 +16,7 @@ def sample_batch_translation_with_storage(): ) from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions + # get service secrets endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] source_storage_endpoint = os.environ["AZURE_STORAGE_SOURCE_ENDPOINT"] @@ -26,10 +28,12 @@ def sample_batch_translation_with_storage(): target_storage_container_name = os.environ["AZURE_STORAGE_TARGET_CONTAINER_NAME"] target_storage_key = os.environ["AZURE_STORAGE_TARGET_KEY"] + # create translation client translation_client = DocumentTranslationClient( endpoint, AzureKeyCredential(key) ) + # upload some document to source container container_client = ContainerClient( source_storage_endpoint, container_name=source_storage_container_name, @@ -39,6 +43,7 @@ def sample_batch_translation_with_storage(): with open("document.txt", "rb") as doc: container_client.upload_blob("document.txt", doc) + # prepare translation input source_container_sas = generate_container_sas( account_name=source_storage_account_name, container_name=source_storage_container_name, @@ -69,20 +74,32 @@ def sample_batch_translation_with_storage(): ) ] - job_detail = translation_client.create_translation_job(batch) - job_result = translation_client.wait_until_done(job_detail.id) + # submit docs for translation + poller = translation_client.begin_translation(batch) # type: DocumentTranslationPoller[ItemPaged[DocumentStatusDetail]] - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - check_documents(translation_client, job_result.id) + # initial status + translation_details = poller.details # type: TranslationStatusDetail + print("Translation initial status: {}".format(translation_details.status)) + print("Number of translations on documents: {}".format(translation_details.documents_total_count)) - 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) + # get final status + doc_statuses = poller.result() # type: ItemPaged[DocumentStatusDetail] + translation_result = poller.details # type: TranslationStatusDetail + if translation_result.status == "Succeeded": + print("We translated our documents!") + if translation_result.documents_failed_count > 0: + docs_to_retry = check_documents(doc_statuses) + # do something with failed docs + + elif translation_result.status in ["Failed", "ValidationFailed"]: + if translation_result.error: + print("Translation job failed: {}: {}".format(translation_result.error.code, translation_result.error.message)) + docs_to_retry = check_documents(doc_statuses) + # do something with failed docs exit(1) + + # write translated document to desired storage container_client = ContainerClient( target_storage_endpoint, container_name=target_storage_container_name, @@ -96,15 +113,7 @@ def sample_batch_translation_with_storage(): my_blob.write(download_stream.readall()) -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - 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 due to insufficient permissions.") - raise err - +def check_documents(doc_statuses): docs_to_retry = [] for document in doc_statuses: if document.status == "Failed": @@ -116,6 +125,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + return docs_to_retry if __name__ == '__main__': diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_translation_job.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_translation_job.py index 4ed96a711b31..54d3ab90c152 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_translation_job.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_translation_job.py @@ -6,6 +6,7 @@ def sample_cancel_translation_job(): + # import libraries import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( @@ -14,13 +15,16 @@ def sample_cancel_translation_job(): StorageTarget ) + # get service secrets endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] + # create translation client client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + # prepare translation inout batch = [ BatchDocumentInput( source_url=source_container_url, @@ -34,16 +38,22 @@ def sample_cancel_translation_job(): ) ] - job_detail = client.create_translation_job(batch) # type: JobStatusDetail + # submit documents for translation + poller = client.begin_translation(batch) # type: DocumentTranslationPoller[ItemPaged[DocumentStatusDetail]] - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) + # initial status + translation_details = poller.details # type: TranslationStatusDetail + print("Translation initial status: {}".format(translation_details.status)) + print("Number of translations on documents: {}".format(translation_details.documents_total_count)) - client.cancel_job(job_detail.id) - job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail + # cancel job + client.cancel_job(poller.batch_id) - if job_detail.status in ["Cancelled", "Cancelling"]: - print("We cancelled job with ID: {}".format(job_detail.id)) + # get result + translation_details = poller.details # type: TranslationStatusDetail + + if translation_details.status in ["Cancelled", "Cancelling"]: + print("We cancelled job with ID: {}".format(translation_details.id)) if __name__ == '__main__': diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py index 30321928eaca..7f55365d7981 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py @@ -6,6 +6,7 @@ def sample_custom_translation(): + # import libraries import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( @@ -14,14 +15,17 @@ def sample_custom_translation(): StorageTarget ) + # get service secrets endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] category_id = os.environ["AZURE_DOCUMENT_TRANSLATION_MODEL_ID"] + # create translation client client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + # prepare translation input batch = [ BatchDocumentInput( source_url=source_container_url, @@ -37,33 +41,32 @@ def sample_custom_translation(): ) ] - job_detail = client.create_translation_job(batch) # type: JobStatusDetail + # submit documents for translation + poller = client.begin_translation(batch) # type: DocumentTranslationPoller[ItemPaged[DocumentStatusDetail]] + + # initial status + translation_details = poller.details # type: TranslationStatusDetail + print("Translation initial status: {}".format(translation_details.status)) + print("Number of translations on documents: {}".format(translation_details.documents_total_count)) - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) - - job_result = client.wait_until_done(job_detail.id) # type: JobStatusDetail - if job_result.status == "Succeeded": + # get final status + doc_statuses = poller.result() # type: ItemPaged[DocumentStatusDetail] + translation_details = poller.details # type: TranslationStatusDetail + if translation_details.status == "Succeeded": print("We translated our documents!") - if job_result.documents_failed_count > 0: - check_documents(client, job_result.id) + if translation_details.documents_failed_count > 0: + docs_to_retry = check_documents(doc_statuses) + # do something with failed docs - 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(client, job_result.id) + elif translation_details.status in ["Failed", "ValidationFailed"]: + if translation_details.error: + print("Translation job failed: {}: {}".format(translation_details.error.code, translation_details.error.message)) + docs_to_retry = check_documents(doc_statuses) + # do something with failed docs exit(1) -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - 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 due to insufficient permissions.") - raise err - +def check_documents(doc_statuses): docs_to_retry = [] for document in doc_statuses: if document.status == "Failed": @@ -75,6 +78,7 @@ def check_documents(client, job_id): )) if document.url not in docs_to_retry: docs_to_retry.append(document.url) + return docs_to_retry if __name__ == '__main__': diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_all_submitted_jobs.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_all_submitted_jobs.py deleted file mode 100644 index d80d3de8a52d..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_list_all_submitted_jobs.py +++ /dev/null @@ -1,41 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_list_all_submitted_jobs(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - ) - - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - 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)) - print("{} succeeded".format(job.documents_succeeded_count)) - 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_submitted_jobs() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_status_checks.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_status_checks.py deleted file mode 100644 index 596b373db915..000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_status_checks.py +++ /dev/null @@ -1,88 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_translation_status_checks(): - import os - import time - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - BatchDocumentInput, - StorageTarget - ) - - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] - source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] - target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"] - target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - batch = [ - BatchDocumentInput( - source_url=source_container_url, - targets=[ - StorageTarget( - target_url=target_container_url_es, - language="es" - ), - StorageTarget( - target_url=target_container_url_fr, - language="fr" - ) - ], - storage_type="folder", - prefix="document_2021" - ) - ] - - job_detail = client.create_translation_job(batch) - - while True: - job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail - if job_detail.status in ["NotStarted", "Running"]: - time.sleep(30) - continue - - 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) - - elif job_detail.status == "Succeeded": - print("We translated our documents!") - if job_detail.documents_failed_count > 0: - check_documents(client, job_detail.id) - break - - -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - 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 due to insufficient permissions.") - raise err - - docs_to_retry = [] - for document in doc_statuses: - if document.status == "Failed": - print("Document at {} failed to be translated to {} language".format( - document.url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.url not in docs_to_retry: - docs_to_retry.append(document.url) - - -if __name__ == '__main__': - sample_translation_status_checks()