Skip to content

Commit

Permalink
[translation] add sample that shows filtered translation (#22791)
Browse files Browse the repository at this point in the history
* add samples that show filtered translation

* update readme

* add to ignored samples

* fix for async

* use fstrings
  • Loading branch information
kristapratico authored Feb 1, 2022
1 parent 5b17097 commit 15dedd8
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 3 deletions.
2 changes: 2 additions & 0 deletions scripts/devops_tasks/test_run_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@
"sample_list_translations_with_filters.py",
"sample_translation_with_custom_model.py",
"sample_translation_with_custom_model_async.py",
"sample_begin_translation_with_filters.py",
"sample_begin_translation_with_filters_async.py"
],
"azure-ai-formrecognizer": [
"sample_manage_custom_models.py",
Expand Down
5 changes: 4 additions & 1 deletion sdk/translation/azure-ai-translation-document/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ The following section provides several code snippets covering some of the most c
* [List translation operations](#list-translation-operations "List Translation Operations")

### Translate your documents
Translate the documents in your source container to the target container.
Translate all the documents in your source container to the target container. To translate documents under a folder, or only translate certain documents, see [sample_begin_translation_with_filters.py][sample_begin_translation_with_filters].

```python
from azure.core.credentials import AzureKeyCredential
Expand Down Expand Up @@ -385,6 +385,7 @@ describes available configurations for retries, logging, transport protocols, an
## Next steps

The following section provides several code snippets illustrating common patterns used in the Document Translation Python client library.
More samples can be found under the [samples][samples] directory.

### More sample code

Expand Down Expand Up @@ -459,6 +460,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
[managed_identity]: https://aka.ms/azsdk/documenttranslation/managed-identity
[sdk_logging_docs]: https://docs.microsoft.com/azure/developer/python/azure-sdk-logging

[samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples
[sample_authentication]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/sample_authentication.py
[sample_authentication_async]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_authentication_async.py
[sample_begin_translation]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation.py
Expand All @@ -475,6 +477,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
[sample_translation_with_azure_blob_async]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_azure_blob_async.py
[sample_translation_with_custom_model]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_custom_model.py
[sample_translation_with_custom_model_async]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_custom_model_async.py
[sample_begin_translation_with_filters]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation_with_filters.py

[supported_glossary_formats]: https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/overview#supported-glossary-formats
[custom_translation_article]: https://docs.microsoft.com/azure/cognitive-services/translator/custom-translator/quickstart-build-deploy-custom-model
Expand Down
12 changes: 12 additions & 0 deletions sdk/translation/azure-ai-translation-document/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ what you can do with the Azure Document Translation client library.
|**Advanced Sample File Name**|**Description**|
|----------------|-------------|
|[sample_translation_with_azure_blob.py][begin_translation_with_azure_blob] and [sample_translation_with_azure_blob_async.py][begin_translation_with_azure_blob_async]|Translate documents with upload/download help using Azure Blob Storage|
|[sample_translation_with_custom_model.py][sample_translation_with_custom_model] and [sample_translation_with_custom_model_async.py][sample_translation_with_custom_model_async]|Translate documents using a custom model|
|[sample_begin_translation_with_filters.py][sample_begin_translation_with_filters] and [sample_begin_translation_with_filters_async.py][sample_begin_translation_with_filters_async]|Translate documents under a folder or translate only a specific document|
|[sample_list_document_statuses_with_filters.py][sample_list_document_statuses_with_filters] and [sample_list_document_statuses_with_filters_async.py][sample_list_document_statuses_with_filters_async]|List document statuses using filters|
|[sample_list_translations_with_filters.py][sample_list_translations_with_filters] and [sample_list_translations_with_filters_async.py][sample_list_translations_with_filters_async]|List translations operations using filters|


[versioning_story_readme]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/translation/azure-ai-translation-document#install-the-package
Expand All @@ -76,6 +80,14 @@ what you can do with the Azure Document Translation client library.
[check_document_statuses_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_check_document_statuses_async.py
[list_translations]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/sample_list_translations.py
[list_translations_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_list_translations_async.py
[sample_translation_with_custom_model]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/sample_translation_with_custom_model.py
[sample_translation_with_custom_model_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_translation_with_custom_model_async.py
[sample_begin_translation_with_filters]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/sample_begin_translation_with_filters.py
[sample_begin_translation_with_filters_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_begin_translation_with_filters_async.py
[sample_list_document_statuses_with_filters]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/sample_list_document_statuses_with_filters.py
[sample_list_document_statuses_with_filters_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_list_document_statuses_with_filters_async.py
[sample_list_translations_with_filters]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/sample_list_translations_with_filters.py
[sample_list_translations_with_filters_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/translation/azure-ai-translation-document/samples/async_samples/sample_list_translations_with_filters_async.py
[pip]: https://pypi.org/project/pip/
[azure_subscription]: https://azure.microsoft.com/free/
[azure_document_translation_account]: https://docs.microsoft.com/azure/cognitive-services/translator/document-translation/get-started-with-document-translation?tabs=python
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
FILE: sample_begin_translation_async.py
DESCRIPTION:
This sample demonstrates how to translate documents in your Azure Blob Storage container.
This sample demonstrates how to translate documents in your Azure Blob Storage container. To translate a
specific document only, or to translate documents under a folder, see sample_begin_translation_with_filters_async.py.
To set up your containers for translation and generate SAS tokens to your containers (or files)
with the appropriate permissions, see the README.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
FILE: sample_begin_translation_with_filters_async.py
DESCRIPTION:
This sample demonstrates how to translate documents under a particular folder in your container, or how
to translate only a specific document in a container. To see how to translate all documents in your container,
see sample_begin_translation_async.py.
USAGE:
python sample_begin_translation_with_filters_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. For this sample, the source container should contain a document within a folder.
4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents
will be written.
5) AZURE_SOURCE_BLOB_URL - the blob SAS URL to the specific document to translate.
e.g. https://mystorage.blob.core.windows.net/srccontainer/document.pdf?<sas-token>
6) AZURE_TARGET_BLOB_URL - the target container SAS URL, including the document name for the translated file
(which does not exist yet). E.g. https://mystorage.blob.core.windows.net/targetcontainer/document_french.pdf?<sas-token>
"""

import asyncio


async def sample_translation_under_folder_async():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document.aio import DocumentTranslationClient

endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] # do not include the folder name in source url
target_container_url = os.environ["AZURE_TARGET_CONTAINER_URL"]

client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
async with client:
poller = await client.begin_translation(source_container_url, target_container_url, "fr", prefix="myfoldername")
result = await poller.result()

async for document in result:
print(f"Document ID: {document.id}")
print(f"Document status: {document.status}")
if document.status == "Succeeded":
print(f"Source document location: {document.source_document_url}")
print(f"Translated document location: {document.translated_document_url}")
print(f"Translated to language: {document.translated_to}\n")
else:
print(f"Error Code: {document.error.code}, Message: {document.error.message}\n")


async def sample_translation_specific_document_async():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document.aio import DocumentTranslationClient

endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
source_blob_url = os.environ["AZURE_SOURCE_BLOB_URL"]
target_blob_url = os.environ["AZURE_TARGET_BLOB_URL"]

client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
async with client:
poller = await client.begin_translation(source_blob_url, target_blob_url, "fr", storage_type="File")
result = await poller.result()

async for document in result:
print(f"Document ID: {document.id}")
print(f"Document status: {document.status}")
if document.status == "Succeeded":
print(f"Source document location: {document.source_document_url}")
print(f"Translated document location: {document.translated_document_url}")
print(f"Translated to language: {document.translated_to}\n")
else:
print(f"Error Code: {document.error.code}, Message: {document.error.message}\n")


async def main():
await sample_translation_under_folder_async()
await sample_translation_specific_document_async()

if __name__ == '__main__':
asyncio.run(main())
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
FILE: sample_begin_translation.py
DESCRIPTION:
This sample demonstrates how to translate documents in your Azure Blob Storage container.
This sample demonstrates how to translate documents in your Azure Blob Storage container. To translate a
specific document only, or to translate documents under a folder, see sample_begin_translation_with_filters.py.
To set up your containers for translation and generate SAS tokens to your containers (or files)
with the appropriate permissions, see the README.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------

"""
FILE: sample_begin_translation_with_filters.py
DESCRIPTION:
This sample demonstrates how to translate documents under a particular folder in your container, or how
to translate only a specific document in a container. To see how to translate all documents in your container,
see sample_begin_translation.py.
USAGE:
python sample_begin_translation_with_filters.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. For this sample, the source container should contain a document within a folder.
4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents
will be written.
5) AZURE_SOURCE_BLOB_URL - the blob SAS URL to the specific document to translate.
e.g. https://mystorage.blob.core.windows.net/srccontainer/document.pdf?<sas-token>
6) AZURE_TARGET_BLOB_URL - the target container SAS URL, including the document name for the translated file
(which does not exist yet). E.g. https://mystorage.blob.core.windows.net/targetcontainer/document_french.pdf?<sas-token>
"""


def sample_translation_under_folder():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient

endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
source_container_url = os.environ["AZURE_SOURCE_CONTAINER_URL"] # do not include the folder name in source url
target_container_url = os.environ["AZURE_TARGET_CONTAINER_URL"]

client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

poller = client.begin_translation(source_container_url, target_container_url, "fr", prefix="myfoldername")
result = poller.result()

for document in result:
print(f"Document ID: {document.id}")
print(f"Document status: {document.status}")
if document.status == "Succeeded":
print(f"Source document location: {document.source_document_url}")
print(f"Translated document location: {document.translated_document_url}")
print(f"Translated to language: {document.translated_to}\n")
else:
print(f"Error Code: {document.error.code}, Message: {document.error.message}\n")


def sample_translation_specific_document():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient

endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
source_blob_url = os.environ["AZURE_SOURCE_BLOB_URL"]
target_blob_url = os.environ["AZURE_TARGET_BLOB_URL"]

client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

poller = client.begin_translation(source_blob_url, target_blob_url, "fr", storage_type="File")
result = poller.result()

for document in result:
print(f"Document ID: {document.id}")
print(f"Document status: {document.status}")
if document.status == "Succeeded":
print(f"Source document location: {document.source_document_url}")
print(f"Translated document location: {document.translated_document_url}")
print(f"Translated to language: {document.translated_to}\n")
else:
print(f"Error Code: {document.error.code}, Message: {document.error.message}\n")


if __name__ == '__main__':
sample_translation_under_folder()
sample_translation_specific_document()

0 comments on commit 15dedd8

Please sign in to comment.