diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/README.md b/sdk/documenttranslation/azure-ai-documenttranslation/README.md index dbb0b7f1653ef..dee698827fa25 100644 --- a/sdk/documenttranslation/azure-ai-documenttranslation/README.md +++ b/sdk/documenttranslation/azure-ai-documenttranslation/README.md @@ -287,7 +287,7 @@ for job in jobs: ``` To see how to use the Document Translation client library with Azure Storage Blob to upload documents, create SAS tokens -for your containers, and download the finished translated documents, see this [sample][sample_batch_translation_with_storage.py]. +for your containers, and download the finished translated documents, see this [sample][sample_translation_with_azure_blob]. Note that you will need to install the [azure-storage-blob][azure_storage_blob] library to run this sample. ## Troubleshooting @@ -320,10 +320,12 @@ The following section provides several code snippets illustrating common pattern These code samples show common scenario operations with the Azure Document Translation client library. -* Client authentication: TODO -* Create a translation job: TODO -* Check the status of documents: TODO -* List all submitted translation jobs: TODO +* Client authentication: [sample_authentication.py][sample_authentication] +* Create a translation job: [sample_create_translation_job.py][sample_create_translation_job] +* Check the status of documents: [sample_check_document_statuses.py][sample_check_document_statuses] +* List all submitted translation jobs: [sample_list_all_submitted_jobs.py][sample_list_all_submitted_jobs] +* Apply a custom glossary to translation: [sample_translation_with_glossaries.py][sample_translation_with_glossaries] +* Use Azure Blob Storage to set up translation resources: [sample_translation_with_azure_blob.py][sample_translation_with_azure_blob] ### Async samples @@ -331,10 +333,12 @@ This library also includes a complete async API supported on Python 3.6+. To use first install an async transport, such as [aiohttp](https://pypi.org/project/aiohttp/). Async clients are found under the `azure.ai.documenttranslation.aio` namespace. -* Client authentication: TODO -* Create a translation job: TODO -* Check the status of documents: TODO -* List all submitted translation jobs: TODO +* Client authentication: [sample_authentication_async.py][sample_authentication_async] +* Create a translation job: [sample_create_translation_job_async.py][sample_create_translation_job_async] +* Check the status of documents: [sample_check_document_statuses_async.py][sample_check_document_statuses_async] +* List all submitted translation jobs: [sample_list_all_submitted_jobs_async.py][sample_list_all_submitted_jobs_async] +* Apply a custom glossary to translation: [sample_translation_with_glossaries_async.py][sample_translation_with_glossaries_async] +* Use Azure Blob Storage to set up translation resources: [sample_translation_with_azure_blob_async.py][sample_translation_with_azure_blob_async] ### Additional documentation @@ -379,9 +383,20 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [sdk_logging_docs]: https://docs.microsoft.com/azure/developer/python/azure-sdk-logging - -[sample_batch_translation_with_storage.py]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation_with_storage.py - + [cla]: https://cla.microsoft.com [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_authentication_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_authentication_async.py new file mode 100644 index 0000000000000..3ce90e19c2d86 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_authentication_async.py @@ -0,0 +1,52 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +""" +FILE: sample_authentication_async.py + +DESCRIPTION: + This sample demonstrates how to authenticate to the Document Translation service. + + There is currently only one supported method of authentication: + 1) Use a Document Translation API key with AzureKeyCredential from azure.core.credentials + + Note: the endpoint must be formatted to use the custom domain name for your resource: + https://.cognitiveservices.azure.com/ + +USAGE: + python sample_authentication_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key +""" + +import os +import asyncio + + +async def sample_authentication_api_key_async(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation.aio import DocumentTranslationClient + + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + + document_translation_client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + # make calls with authenticated client + async with document_translation_client: + result = await document_translation_client.get_document_formats() + + +async def main(): + await sample_authentication_api_key_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_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_batch_translation_async.py deleted file mode 100644 index 5b8ad96df0e34..0000000000000 --- 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 ( - DocumentTranslationInput, - TranslationTarget - ) - - # 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 - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url_en, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ), - TranslationTarget( - target_url=target_container_url_fr, - language_code="fr" - ) - ] - ), - DocumentTranslationInput( - source_url=source_container_url_de, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ), - TranslationTarget( - target_url=target_container_url_fr, - language_code="fr" - ) - ] - ) - ] - - # run translation job - async with client: - job_detail = await client.create_translation_job(translation_inputs) # type: JobStatusResult - - 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: JobStatusResult - 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_all_document_statuses(job_id) # type: AsyncItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_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 61cbf9aa26277..0000000000000 --- 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 ( - DocumentTranslationInput, - TranslationTarget - ) - 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 - - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url, - language_code="es" - ) - ], - prefix="document" - ) - ] - - # run job - async with translation_client: - job_detail = await translation_client.create_translation_job(translation_inputs) - 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_all_document_statuses(job_id) # type: AsyncItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_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 de9a61c951a91..0000000000000 --- 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 ( - DocumentTranslationInput, - TranslationTarget - ) - - # 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 - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ) - ], - storage_type="file" - ) - ] - - # create translation client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - # run job - async with client: - job_detail = await client.create_translation_job(translation_inputs) - - 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: JobStatusResult - - 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_check_document_statuses_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_check_document_statuses_async.py new file mode 100644 index 0000000000000..c2d6dc3726a32 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_check_document_statuses_async.py @@ -0,0 +1,93 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_check_document_statuses_async.py + +DESCRIPTION: + This sample demonstrates how to create a translation job and then monitor each document's status + and progress within the job. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_check_document_statuses_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents + will be written. +""" + +import asyncio + + +async def sample_document_status_checks_async(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation.aio import DocumentTranslationClient + from azure.ai.documenttranslation import ( + DocumentTranslationInput, + TranslationTarget + ) + + 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 = os.environ["AZURE_TARGET_CONTAINER_URL"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + async with client: + job_result = await client.create_translation_job(inputs=[ + DocumentTranslationInput( + source_url=source_container_url, + targets=[ + TranslationTarget( + target_url=target_container_url, + language_code="es" + ) + ] + ) + ] + ) # type: JobStatusResult + + completed_docs = [] + while not job_result.has_completed: + await asyncio.sleep(30) + + doc_statuses = client.list_all_document_statuses(job_result.id) + async for document in doc_statuses: + if document.id not in completed_docs: + if document.status == "Succeeded": + print("Document at {} was translated to {} language".format( + document.translated_document_url, document.translate_to + )) + completed_docs.append(document.id) + if document.status == "Failed": + print("Document ID: {}, Error Code: {}, Message: {}".format( + document.id, document.error.code, document.error.message + )) + completed_docs.append(document.id) + if document.status == "Running": + print("Document ID: {}, translation progress is {} percent".format( + document.id, document.translation_progress * 100 + )) + + job_result = await client.get_job_status(job_result.id) + + print("\nTranslation job completed.") + + +async def main(): + await sample_document_status_checks_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_create_translation_job_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_create_translation_job_async.py new file mode 100644 index 0000000000000..c61ca29145d64 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_create_translation_job_async.py @@ -0,0 +1,90 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_create_translation_job_async.py + +DESCRIPTION: + This sample demonstrates how to create a translation job for documents in your Azure Blob + Storage container and wait until the job is completed. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_create_translation_job_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents + will be written. +""" + + +import asyncio + + +async def sample_translation_async(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation.aio import DocumentTranslationClient + from azure.ai.documenttranslation import ( + DocumentTranslationInput, + TranslationTarget + ) + + 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 = os.environ["AZURE_TARGET_CONTAINER_URL"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + async with client: + job = await client.create_translation_job(inputs=[ + DocumentTranslationInput( + source_url=source_container_url, + targets=[ + TranslationTarget( + target_url=target_container_url, + language_code="es" + ) + ] + ) + ] + ) # type: JobStatusResult + + job_result = await client.wait_until_done(job.id) # type: JobStatusResult + + print("Job status: {}".format(job_result.status)) + print("Job created on: {}".format(job_result.created_on)) + print("Job last updated on: {}".format(job_result.last_updated_on)) + print("Total number of translations on documents: {}".format(job_result.documents_total_count)) + + print("\nOf total documents...") + print("{} failed".format(job_result.documents_failed_count)) + print("{} succeeded".format(job_result.documents_succeeded_count)) + + doc_results = client.list_all_document_statuses(job_result.id) # type: AsyncItemPaged[DocumentStatusResult] + async for document in doc_results: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + +async def main(): + await sample_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_custom_translation_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_custom_translation_async.py deleted file mode 100644 index af1da6b192a67..0000000000000 --- 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 ( - DocumentTranslationInput, - TranslationTarget - ) - - # 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 - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url_fr, - language_code="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(translation_inputs) - - 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: JobStatusResult - 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_all_document_statuses(job_id) # type: AsyncItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_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 index 2c195b8aed7da..dbb24c154629e 100644 --- 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 @@ -4,30 +4,42 @@ # Licensed under the MIT License. # ------------------------------------ -import os -import asyncio +""" +FILE: list_all_submitted_jobs_async.py + +DESCRIPTION: + This sample demonstrates how to list all the submitted translation jobs for the resource and + wait until done on any jobs that are still running. -class ListAllSubmittedJobsSampleAsync(object): + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. - def list_all_submitted_jobs(self): - # import libraries - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation.aio import DocumentTranslationClient +USAGE: + python list_all_submitted_jobs_async.py - # get service secrets - endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] - key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. +""" + +import asyncio - # create translation client - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - # list submitted jobs - jobs = client.list_submitted_jobs() # type: AsyncItemPaged[JobStatusResult] +async def sample_list_all_submitted_jobs_async(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation.aio import DocumentTranslationClient - async for job in jobs: - # wait for job to finish - if job.status in ["NotStarted", "Running"]: - job = client.wait_until_done(job.id) + endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + async with client: + translation_jobs = client.list_submitted_jobs() # type: AsyncItemPaged[JobStatusResult] + + async for job in translation_jobs: + if job.status == "Running": + job = await client.wait_until_done(job.id) print("Job ID: {}".format(job.id)) print("Job status: {}".format(job.status)) @@ -36,19 +48,15 @@ def list_all_submitted_jobs(self): 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("\nOf 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)) + print("{} cancelled\n".format(job.documents_cancelled_count)) async def main(): - sample = ListAllSubmittedJobsSampleAsync() - await sample.list_all_submitted_jobs() - + await sample_list_all_submitted_jobs_async() if __name__ == '__main__': loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file + loop.run_until_complete(main()) 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 4e0bc7469b824..0000000000000 --- 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 ( - DocumentTranslationInput, - TranslationTarget - ) - - # 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 - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ), - TranslationTarget( - target_url=target_container_url_fr, - language_code="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(translation_inputs) - while True: - job_detail = await client.get_job_status(job_detail.id) # type: JobStatusResult - 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_all_document_statuses(job_id) # type: AsyncItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_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/async_samples/sample_translation_with_azure_blob_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_with_azure_blob_async.py new file mode 100644 index 0000000000000..4c074063004cb --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_with_azure_blob_async.py @@ -0,0 +1,168 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_translation_with_azure_blob_async.py + +DESCRIPTION: + This sample demonstrates how to use Azure Blob Storage to set up the necessary resources to create a translation + job. Run the sample to create containers, upload documents, and generate SAS tokens for the source/target + containers. Once the job is completed, use the storage library to download your documents locally. + +PREREQUISITE: + This sample requires you install azure-storage-blob client library: + https://pypi.org/project/azure-storage-blob/ + +USAGE: + python sample_translation_with_azure_blob_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_STORAGE_SOURCE_ENDPOINT - the endpoint to your Storage account + 4) AZURE_STORAGE_ACCOUNT_NAME - the name of your storage account + 5) AZURE_STORAGE_SOURCE_KEY - the shared access key to your storage account + + Optional environment variables - if not set, they will be created for you + 6) AZURE_STORAGE_SOURCE_CONTAINER_NAME - the name of your source container + 7) AZURE_STORAGE_TARGET_CONTAINER_NAME - the name of your target container + 8) AZURE_DOCUMENT_NAME - the name and file extension of your document in this directory + e.g. "mydocument.txt" +""" + +import os +import datetime +import asyncio +from azure.core.credentials import AzureKeyCredential +from azure.core.exceptions import ResourceExistsError +from azure.ai.documenttranslation.aio import DocumentTranslationClient +from azure.ai.documenttranslation import ( + DocumentTranslationInput, + TranslationTarget +) +from azure.storage.blob.aio import BlobServiceClient, BlobClient +from azure.storage.blob import generate_container_sas + + +class SampleTranslationWithAzureBlobAsync(object): + + def __init__(self): + self.endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + self.key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + self.storage_endpoint = os.environ["AZURE_STORAGE_SOURCE_ENDPOINT"] + self.storage_account_name = os.environ["AZURE_STORAGE_ACCOUNT_NAME"] + self.storage_key = os.environ["AZURE_STORAGE_SOURCE_KEY"] + self.storage_source_container_name = os.getenv("AZURE_STORAGE_SOURCE_CONTAINER_NAME", None) # Optional + self.storage_target_container_name = os.getenv("AZURE_STORAGE_TARGET_CONTAINER_NAME", None) # Optional + self.document_name = os.getenv("AZURE_DOCUMENT_NAME", None) # Optional document in same directory as this sample + + async def sample_translation_with_azure_blob(self): + + translation_client = DocumentTranslationClient( + self.endpoint, AzureKeyCredential(self.key) + ) + + blob_service_client = BlobServiceClient( + self.storage_endpoint, + credential=self.storage_key + ) + + source_container = await self.create_container( + blob_service_client, + container_name=self.storage_source_container_name or "translation-source-container", + ) + target_container = await self.create_container( + blob_service_client, + container_name=self.storage_target_container_name or "translation-target-container" + ) + + if self.document_name: + with open(self.document_name, "rb") as doc: + await source_container.upload_blob(self.document_name, doc) + else: + self.document_name = "example_document.txt" + await source_container.upload_blob( + name=self.document_name, + data=b"This is an example translation with the document translation client library" + ) + print("Uploaded document {} to storage container {}".format(self.document_name, source_container.container_name)) + + source_container_sas_url = self.generate_sas_url(source_container, permissions="rl") + target_container_sas_url = self.generate_sas_url(target_container, permissions="wl") + + translation_inputs = [ + DocumentTranslationInput( + source_url=source_container_sas_url, + targets=[ + TranslationTarget( + target_url=target_container_sas_url, + language_code="fr" + ) + ] + ) + ] + + job = await translation_client.create_translation_job(translation_inputs) + print("Created translation job with ID: {}".format(job.id)) + print("Waiting until job completes...") + + job_result = await translation_client.wait_until_done(job.id) + print("Job status: {}".format(job_result.status)) + + doc_results = translation_client.list_all_document_statuses(job_result.id) + + print("\nDocument results:") + async for document in doc_results: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + + blob_client = BlobClient.from_blob_url(document.translated_document_url, credential=self.storage_key) + async with blob_client: + with open("translated_"+self.document_name, "wb") as my_blob: + download_stream = await blob_client.download_blob() + my_blob.write(await download_stream.readall()) + + print("Downloaded {} locally".format("translated_"+self.document_name)) + else: + print("\nThere was a problem translating your document.") + print("Document Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + await translation_client.close() + await blob_service_client.close() + + async def create_container(self, blob_service_client, container_name): + try: + container_client = await blob_service_client.create_container(container_name) + print("Creating container: {}".format(container_name)) + except ResourceExistsError: + print("The container with name {} already exists".format(container_name)) + container_client = blob_service_client.get_container_client(container=container_name) + return container_client + + def generate_sas_url(self, container, permissions): + sas_token = generate_container_sas( + account_name=self.storage_account_name, + container_name=container.container_name, + account_key=self.storage_key, + permission=permissions, + expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1) + ) + + container_sas_url = self.storage_endpoint + container.container_name + "?" + sas_token + print("Generating {} SAS URL".format(container.container_name)) + return container_sas_url + + +async def main(): + sample = SampleTranslationWithAzureBlobAsync() + await sample.sample_translation_with_azure_blob() + +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_translation_with_glossaries_async.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_with_glossaries_async.py new file mode 100644 index 0000000000000..1669a948a55a3 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/async_samples/sample_translation_with_glossaries_async.py @@ -0,0 +1,91 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_translation_with_glossaries_async.py + +DESCRIPTION: + This sample demonstrates how to create a translation job and apply custom glossaries to the translation. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_translation_with_glossaries_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents + will be written. + 5) AZURE_TRANSLATION_GLOSSARY_URL - the SAS URL to your glossary file +""" + +import asyncio + + +async def sample_translation_with_glossaries_async(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation.aio import DocumentTranslationClient + from azure.ai.documenttranslation import ( + DocumentTranslationInput, + TranslationTarget, + TranslationGlossary + ) + + 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 = os.environ["AZURE_TARGET_CONTAINER_URL"] + glossary_url = os.environ["AZURE_TRANSLATION_GLOSSARY_URL"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + inputs = DocumentTranslationInput( + source_url=source_container_url, + targets=[ + TranslationTarget( + target_url=target_container_url, + language_code="es", + glossaries=[TranslationGlossary(glossary_url=glossary_url, file_format="TSV")] + ) + ] + ) + + async with client: + job = await client.create_translation_job(inputs=[inputs]) # type: JobStatusResult + + job_result = await client.wait_until_done(job.id) # type: JobStatusResult + + print("Job status: {}".format(job_result.status)) + print("Job created on: {}".format(job_result.created_on)) + print("Job last updated on: {}".format(job_result.last_updated_on)) + print("Total number of translations on documents: {}".format(job_result.documents_total_count)) + + print("\nOf total documents...") + print("{} failed".format(job_result.documents_failed_count)) + print("{} succeeded".format(job_result.documents_succeeded_count)) + + doc_results = client.list_all_document_statuses(job_result.id) # type: AsyncItemPaged[DocumentStatusResult] + async for document in doc_results: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + +async def main(): + await sample_translation_with_glossaries_async() + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_authentication.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_authentication.py new file mode 100644 index 0000000000000..987f91886c1ba --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_authentication.py @@ -0,0 +1,46 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +""" +FILE: sample_authentication.py + +DESCRIPTION: + This sample demonstrates how to authenticate to the Document Translation service. + + There is currently only one supported method of authentication: + 1) Use a Document Translation API key with AzureKeyCredential from azure.core.credentials + + Note: the endpoint must be formatted to use the custom domain name for your resource: + https://.cognitiveservices.azure.com/ + +USAGE: + python sample_authentication.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key +""" + +import os + + +def sample_authentication_api_key(): + 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"] + + document_translation_client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + # make calls with authenticated client + result = document_translation_client.get_document_formats() + + +if __name__ == '__main__': + sample_authentication_api_key() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py deleted file mode 100644 index 2c2083e3ed838..0000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation.py +++ /dev/null @@ -1,96 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_batch_translation(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget - ) - - 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"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url_en, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ), - TranslationTarget( - target_url=target_container_url_fr, - language_code="fr" - ) - ] - ), - DocumentTranslationInput( - source_url=source_container_url_de, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ), - TranslationTarget( - target_url=target_container_url_fr, - language_code="fr" - ) - ] - ) - ] - - job_detail = client.create_translation_job(translation_inputs) # type: JobStatusResult - - 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: JobStatusResult - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - 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)) - check_documents(client, job_result.id) - exit(1) - - -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_all_document_statuses(job_id) # type: ItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_document_url) - - -if __name__ == '__main__': - sample_batch_translation() 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 deleted file mode 100644 index 73cc09accc9c0..0000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_batch_translation_with_storage.py +++ /dev/null @@ -1,122 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_batch_translation_with_storage(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget - ) - from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions - - 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"] - - translation_client = DocumentTranslationClient( - endpoint, AzureKeyCredential(key) - ) - - container_client = ContainerClient( - source_storage_endpoint, - container_name=source_storage_container_name, - credential=source_storage_key - ) - - with open("document.txt", "rb") as doc: - container_client.upload_blob("document.txt", doc) - - 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 - - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url, - language_code="es" - ) - ], - prefix="document" - ) - ] - - job_detail = translation_client.create_translation_job(translation_inputs) - job_result = translation_client.wait_until_done(job_detail.id) - - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - 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)) - check_documents(translation_client, job_result.id) - exit(1) - - container_client = ContainerClient( - target_storage_endpoint, - container_name=target_storage_container_name, - credential=target_storage_key - ) - - target_container_client = container_client.from_container_url(target_container_url) - - with open("translated.txt", "wb") as my_blob: - download_stream = target_container_client.download_blob("document.txt") - my_blob.write(download_stream.readall()) - - -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_all_document_statuses(job_id) # type: ItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_document_url) - - -if __name__ == '__main__': - sample_batch_translation_with_storage() 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 deleted file mode 100644 index 7e0eb3f3bac65..0000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_cancel_translation_job.py +++ /dev/null @@ -1,50 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_cancel_translation_job(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget - ) - - 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"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ) - ], - storage_type="file" - ) - ] - - job_detail = client.create_translation_job(translation_inputs) # type: JobStatusResult - - print("Job initial status: {}".format(job_detail.status)) - print("Number of translations on documents: {}".format(job_detail.documents_total_count)) - - client.cancel_job(job_detail.id) - job_detail = client.get_job_status(job_detail.id) # type: JobStatusResult - - if job_detail.status in ["Cancelled", "Cancelling"]: - print("We cancelled job with ID: {}".format(job_detail.id)) - - -if __name__ == '__main__': - sample_cancel_translation_job() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_document_statuses.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_document_statuses.py new file mode 100644 index 0000000000000..655d66491609e --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_check_document_statuses.py @@ -0,0 +1,88 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_check_document_statuses.py + +DESCRIPTION: + This sample demonstrates how to create a translation job and then monitor each document's status + and progress within the job. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_check_document_statuses.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents + will be written. +""" + + +def sample_document_status_checks(): + import os + import time + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + DocumentTranslationInput, + TranslationTarget + ) + + 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 = os.environ["AZURE_TARGET_CONTAINER_URL"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + job_result = client.create_translation_job(inputs=[ + DocumentTranslationInput( + source_url=source_container_url, + targets=[ + TranslationTarget( + target_url=target_container_url, + language_code="es" + ) + ] + ) + ] + ) # type: JobStatusResult + + completed_docs = [] + while not job_result.has_completed: + time.sleep(30) + + doc_statuses = client.list_all_document_statuses(job_result.id) + for document in doc_statuses: + if document.id not in completed_docs: + if document.status == "Succeeded": + print("Document at {} was translated to {} language".format( + document.translated_document_url, document.translate_to + )) + completed_docs.append(document.id) + if document.status == "Failed": + print("Document ID: {}, Error Code: {}, Message: {}".format( + document.id, document.error.code, document.error.message + )) + completed_docs.append(document.id) + if document.status == "Running": + print("Document ID: {}, translation progress is {} percent".format( + document.id, document.translation_progress * 100 + )) + + job_result = client.get_job_status(job_result.id) + + print("\nTranslation job completed.") + + +if __name__ == '__main__': + sample_document_status_checks() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_create_translation_job.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_create_translation_job.py new file mode 100644 index 0000000000000..ab24503a6d59c --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_create_translation_job.py @@ -0,0 +1,82 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_create_translation_job.py + +DESCRIPTION: + This sample demonstrates how to create a translation job for documents in your Azure Blob + Storage container and wait until the job is completed. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_create_translation_job.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents + will be written. +""" + + +def sample_translation(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + DocumentTranslationInput, + TranslationTarget + ) + + 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 = os.environ["AZURE_TARGET_CONTAINER_URL"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + job = client.create_translation_job(inputs=[ + DocumentTranslationInput( + source_url=source_container_url, + targets=[ + TranslationTarget( + target_url=target_container_url, + language_code="es" + ) + ] + ) + ] + ) # type: JobStatusResult + + job_result = client.wait_until_done(job.id) # type: JobStatusResult + + print("Job status: {}".format(job_result.status)) + print("Job created on: {}".format(job_result.created_on)) + print("Job last updated on: {}".format(job_result.last_updated_on)) + print("Total number of translations on documents: {}".format(job_result.documents_total_count)) + + print("\nOf total documents...") + print("{} failed".format(job_result.documents_failed_count)) + print("{} succeeded".format(job_result.documents_succeeded_count)) + + doc_results = client.list_all_document_statuses(job_result.id) # type: ItemPaged[DocumentStatusResult] + for document in doc_results: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + +if __name__ == '__main__': + sample_translation() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py deleted file mode 100644 index 22a80d5ee2f2a..0000000000000 --- a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_custom_translation.py +++ /dev/null @@ -1,81 +0,0 @@ -# coding=utf-8 -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - - -def sample_custom_translation(): - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.documenttranslation import ( - DocumentTranslationClient, - DocumentTranslationInput, - TranslationTarget - ) - - 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"] - - client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url_fr, - language_code="fr", - category_id=category_id, - glossaries=["https://exampleglossary"] - ) - ], - prefix="document_2021" - ) - ] - - job_detail = client.create_translation_job(translation_inputs) # type: JobStatusResult - - 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: JobStatusResult - if job_result.status == "Succeeded": - print("We translated our documents!") - if job_result.documents_failed_count > 0: - 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)) - check_documents(client, job_result.id) - exit(1) - - -def check_documents(client, job_id): - from azure.core.exceptions import ResourceNotFoundError - - try: - doc_statuses = client.list_all_document_statuses(job_id) # type: ItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_document_url) - - -if __name__ == '__main__': - sample_custom_translation() 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 index 0bbe485db847b..67e2e8858ee11 100644 --- 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 @@ -4,6 +4,24 @@ # Licensed under the MIT License. # ------------------------------------ +""" +FILE: list_all_submitted_jobs.py + +DESCRIPTION: + This sample demonstrates how to list all the submitted translation jobs for the resource and + wait until done on any jobs that are still running. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python list_all_submitted_jobs.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. +""" + def sample_list_all_submitted_jobs(): import os @@ -16,10 +34,10 @@ def sample_list_all_submitted_jobs(): key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) - jobs = client.list_submitted_jobs() # type: ItemPaged[JobStatusResult] + translation_jobs = client.list_submitted_jobs() # type: ItemPaged[JobStatusResult] - for job in jobs: - if job.status in ["NotStarted", "Running"]: + for job in translation_jobs: + if job.status == "Running": job = client.wait_until_done(job.id) print("Job ID: {}".format(job.id)) @@ -29,12 +47,10 @@ def sample_list_all_submitted_jobs(): 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("\nOf 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)) + print("{} cancelled\n".format(job.documents_cancelled_count)) if __name__ == '__main__': 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 ab94a04b906b0..0000000000000 --- 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, - DocumentTranslationInput, - TranslationTarget - ) - - 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)) - - translation_inputs = [ - DocumentTranslationInput( - source_url=source_container_url, - targets=[ - TranslationTarget( - target_url=target_container_url_es, - language_code="es" - ), - TranslationTarget( - target_url=target_container_url_fr, - language_code="fr" - ) - ], - storage_type="folder", - prefix="document_2021" - ) - ] - - job_detail = client.create_translation_job(translation_inputs) - - while True: - job_detail = client.get_job_status(job_detail.id) # type: JobStatusResult - 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_all_document_statuses(job_id) # type: ItemPaged[DocumentStatusResult] - 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.translated_document_url, document.translate_to - )) - print("Document ID: {}, Error Code: {}, Message: {}".format( - document.id, document.error.code, document.error.message - )) - if document.translated_document_url not in docs_to_retry: - docs_to_retry.append(document.translated_document_url) - - -if __name__ == '__main__': - sample_translation_status_checks() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_with_azure_blob.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_with_azure_blob.py new file mode 100644 index 0000000000000..57088ed1c6285 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_with_azure_blob.py @@ -0,0 +1,158 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_translation_with_azure_blob.py + +DESCRIPTION: + This sample demonstrates how to use Azure Blob Storage to set up the necessary resources to create a translation + job. Run the sample to create containers, upload documents, and generate SAS tokens for the source/target + containers. Once the job is completed, use the storage library to download your documents locally. + +PREREQUISITE: + This sample requires you install azure-storage-blob client library: + https://pypi.org/project/azure-storage-blob/ + +USAGE: + python sample_translation_with_azure_blob.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_STORAGE_SOURCE_ENDPOINT - the endpoint to your Storage account + 4) AZURE_STORAGE_ACCOUNT_NAME - the name of your storage account + 5) AZURE_STORAGE_SOURCE_KEY - the shared access key to your storage account + + Optional environment variables - if not set, they will be created for you + 6) AZURE_STORAGE_SOURCE_CONTAINER_NAME - the name of your source container + 7) AZURE_STORAGE_TARGET_CONTAINER_NAME - the name of your target container + 8) AZURE_DOCUMENT_NAME - the name and file extension of your document in this directory + e.g. "mydocument.txt" +""" + +import os +import datetime +from azure.core.credentials import AzureKeyCredential +from azure.core.exceptions import ResourceExistsError +from azure.ai.documenttranslation import ( + DocumentTranslationClient, + DocumentTranslationInput, + TranslationTarget +) +from azure.storage.blob import BlobServiceClient, BlobClient, generate_container_sas + + +class SampleTranslationWithAzureBlob(object): + + def __init__(self): + self.endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] + self.key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] + self.storage_endpoint = os.environ["AZURE_STORAGE_SOURCE_ENDPOINT"] + self.storage_account_name = os.environ["AZURE_STORAGE_ACCOUNT_NAME"] + self.storage_key = os.environ["AZURE_STORAGE_SOURCE_KEY"] + self.storage_source_container_name = os.getenv("AZURE_STORAGE_SOURCE_CONTAINER_NAME", None) # Optional + self.storage_target_container_name = os.getenv("AZURE_STORAGE_TARGET_CONTAINER_NAME", None) # Optional + self.document_name = os.getenv("AZURE_DOCUMENT_NAME", None) # Optional document in same directory as this sample + + def sample_translation_with_azure_blob(self): + + translation_client = DocumentTranslationClient( + self.endpoint, AzureKeyCredential(self.key) + ) + + blob_service_client = BlobServiceClient( + self.storage_endpoint, + credential=self.storage_key + ) + + source_container = self.create_container( + blob_service_client, + container_name=self.storage_source_container_name or "translation-source-container", + ) + target_container = self.create_container( + blob_service_client, + container_name=self.storage_target_container_name or "translation-target-container" + ) + + if self.document_name: + with open(self.document_name, "rb") as doc: + source_container.upload_blob(self.document_name, doc) + else: + self.document_name = "example_document.txt" + source_container.upload_blob( + name=self.document_name, + data=b"This is an example translation with the document translation client library" + ) + print("Uploaded document {} to storage container {}".format(self.document_name, source_container.container_name)) + + source_container_sas_url = self.generate_sas_url(source_container, permissions="rl") + target_container_sas_url = self.generate_sas_url(target_container, permissions="wl") + + translation_inputs = [ + DocumentTranslationInput( + source_url=source_container_sas_url, + targets=[ + TranslationTarget( + target_url=target_container_sas_url, + language_code="fr" + ) + ] + ) + ] + + job = translation_client.create_translation_job(translation_inputs) + print("Created translation job with ID: {}".format(job.id)) + print("Waiting until job completes...") + + job_result = translation_client.wait_until_done(job.id) + print("Job status: {}".format(job_result.status)) + + doc_results = translation_client.list_all_document_statuses(job_result.id) + + print("\nDocument results:") + for document in doc_results: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + + blob_client = BlobClient.from_blob_url(document.translated_document_url, credential=self.storage_key) + with open("translated_"+self.document_name, "wb") as my_blob: + download_stream = blob_client.download_blob() + my_blob.write(download_stream.readall()) + + print("Downloaded {} locally".format("translated_"+self.document_name)) + else: + print("\nThere was a problem translating your document.") + print("Document Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + def create_container(self, blob_service_client, container_name): + try: + container_client = blob_service_client.create_container(container_name) + print("Creating container: {}".format(container_name)) + except ResourceExistsError: + print("The container with name {} already exists".format(container_name)) + container_client = blob_service_client.get_container_client(container=container_name) + return container_client + + def generate_sas_url(self, container, permissions): + sas_token = generate_container_sas( + account_name=self.storage_account_name, + container_name=container.container_name, + account_key=self.storage_key, + permission=permissions, + expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1) + ) + + container_sas_url = self.storage_endpoint + container.container_name + "?" + sas_token + print("Generating {} SAS URL".format(container.container_name)) + return container_sas_url + + +if __name__ == '__main__': + sample = SampleTranslationWithAzureBlob() + sample.sample_translation_with_azure_blob() diff --git a/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_with_glossaries.py b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_with_glossaries.py new file mode 100644 index 0000000000000..cab02928fa8d1 --- /dev/null +++ b/sdk/documenttranslation/azure-ai-documenttranslation/samples/sample_translation_with_glossaries.py @@ -0,0 +1,84 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_translation_with_glossaries.py + +DESCRIPTION: + This sample demonstrates how to create a translation job and apply custom glossaries to the translation. + + To set up your containers for translation and generate SAS tokens to your containers (or files) + with the appropriate permissions, see the README. + +USAGE: + python sample_translation_with_glossaries.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource. + 2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key. + 3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents + to be translated. + 4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents + will be written. + 5) AZURE_TRANSLATION_GLOSSARY_URL - the SAS URL to your glossary file +""" + + +def sample_translation_with_glossaries(): + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.documenttranslation import ( + DocumentTranslationClient, + DocumentTranslationInput, + TranslationTarget, + TranslationGlossary + ) + + 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 = os.environ["AZURE_TARGET_CONTAINER_URL"] + glossary_url = os.environ["AZURE_TRANSLATION_GLOSSARY_URL"] + + client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) + + inputs = DocumentTranslationInput( + source_url=source_container_url, + targets=[ + TranslationTarget( + target_url=target_container_url, + language_code="es", + glossaries=[TranslationGlossary(glossary_url=glossary_url, file_format="TSV")] + ) + ] + ) + + job = client.create_translation_job(inputs=[inputs]) # type: JobStatusResult + + job_result = client.wait_until_done(job.id) # type: JobStatusResult + + print("Job status: {}".format(job_result.status)) + print("Job created on: {}".format(job_result.created_on)) + print("Job last updated on: {}".format(job_result.last_updated_on)) + print("Total number of translations on documents: {}".format(job_result.documents_total_count)) + + print("\nOf total documents...") + print("{} failed".format(job_result.documents_failed_count)) + print("{} succeeded".format(job_result.documents_succeeded_count)) + + doc_results = client.list_all_document_statuses(job_result.id) # type: ItemPaged[DocumentStatusResult] + for document in doc_results: + print("Document ID: {}".format(document.id)) + print("Document status: {}".format(document.status)) + if document.status == "Succeeded": + print("Document location: {}".format(document.translated_document_url)) + print("Translated to language: {}\n".format(document.translate_to)) + else: + print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message)) + + +if __name__ == '__main__': + sample_translation_with_glossaries()