-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[translation] add sample that shows filtered translation (#22791)
* add samples that show filtered translation * update readme * add to ignored samples * fix for async * use fstrings
- Loading branch information
1 parent
5b17097
commit 15dedd8
Showing
7 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...translation-document/samples/async_samples/sample_begin_translation_with_filters_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ranslation/azure-ai-translation-document/samples/sample_begin_translation_with_filters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |