Microsoft Azure Document Translation Client Library for Python
Project description
Azure Document Translation client library for Python
Azure Cognitive Services Document Translation is a cloud service that translates documents to and from 90 languages and dialects while preserving document structure and data format. Use the client library for Document Translation to:
- Translate numerous, large files from an Azure Blob Storage container to a target container in your language of choice.
- Check the translation status and progress of each document in the translation operation.
- Apply a custom translation model or glossaries to tailor translation to your specific case.
Source code | Package (PyPI) | API reference documentation | Product documentation | Samples
Getting started
Prerequisites
- Python 2.7, or 3.6 or later is required to use this package.
- You must have an Azure subscription and a Translator resource to use this package.
Install the package
Install the Azure Document Translation client library for Python with pip:
pip install azure-ai-translation-document --pre
Note: This version of the client library defaults to the v1.0 version of the service
Create a Translator resource
The Document Translation feature supports single-service access only. To access the service, create a Translator resource.
You can create the resource using
Option 1: Azure Portal
Option 2: Azure CLI. Below is an example of how you can create a Translator resource using the CLI:
# Create a new resource group to hold the Translator resource -
# if using an existing resource group, skip this step
az group create --name my-resource-group --location westus2
# Create document translation
az cognitiveservices account create \
--name document-translation-resource \
--custom-domain document-translation-resource \
--resource-group my-resource-group \
--kind TextTranslation \
--sku S1 \
--location westus2 \
--yes
Authenticate the client
In order to interact with the Document Translation feature service, you will need to create an instance of a client. An endpoint and credential are necessary to instantiate the client object.
Looking up the endpoint
You can find the endpoint for your Translator resource using the Azure Portal.
Note that the service requires a custom domain endpoint. Follow the instructions in the above link to format your endpoint: https://{NAME-OF-YOUR-RESOURCE}.cognitiveservices.azure.com/
Get the API key
The API key can be found in the Azure Portal or by running the following Azure CLI command:
az cognitiveservices account keys list --name "resource-name" --resource-group "resource-group-name"
Create the client with AzureKeyCredential
To use an API key as the credential
parameter,
pass the key as a string into an instance of AzureKeyCredential.
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient
endpoint = "https://<resource-name>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
document_translation_client = DocumentTranslationClient(endpoint, credential)
Create the client with an Azure Active Directory credential
AzureKeyCredential
authentication is used in the examples in this getting started guide, but you can also
authenticate with Azure Active Directory using the azure-identity library.
To use the DefaultAzureCredential type shown below, or other credential types provided
with the Azure SDK, please install the azure-identity
package:
pip install azure-identity
You will also need to register a new AAD application and grant access to your
Translator resource by assigning the "Cognitive Services User"
role to your service principal.
Once completed, set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
AZURE_CLIENT_ID
, AZURE_TENANT_ID
, AZURE_CLIENT_SECRET
.
from azure.identity import DefaultAzureCredential
from azure.ai.translation.document import DocumentTranslationClient
credential = DefaultAzureCredential()
document_translation_client = DocumentTranslationClient(
endpoint="https://<resource-name>.cognitiveservices.azure.com/",
credential=credential
)
Key concepts
The Document Translation service requires that you upload your files to an Azure Blob Storage source container and provide a target container where the translated documents can be written. SAS tokens to the containers (or files) are used to access the documents and create the translated documents in the target container. Additional information about setting this up can be found in the service documentation:
- Set up Azure Blob Storage containers with your documents
- Optionally apply glossaries or a custom model for translation
- Generate SAS tokens to your containers (or files) with the appropriate permissions
DocumentTranslationClient
Interaction with the Document Translation client library begins with an instance of the DocumentTranslationClient
.
The client provides operations for:
- Creating a translation operation to translate documents in your source container(s) and write results to you target container(s).
- Checking the status of individual documents in the translation operation and monitoring each document's progress.
- Enumerating all past and current translations operations.
- Identifying supported glossary and document formats.
Translation Input
Input to the begin_translation
client method can be provided in two different ways:
- A single source container with documents can be translated to a different language:
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient
document_translation_client = DocumentTranslationClient("<endpoint>", AzureKeyCredential("<api_key>"))
poller = document_translation_client.begin_translation("<sas_url_to_source>", "<sas_url_to_target>", "<target_language_code>")
- Or multiple different sources can be provided each with their own targets.
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient, DocumentTranslationInput, TranslationTarget
my_input = [
DocumentTranslationInput(
source_url="<sas_url_to_source_A>",
targets=[
TranslationTarget(target_url="<sas_url_to_target_fr>", language_code="fr"),
TranslationTarget(target_url="<sas_url_to_target_de>", language_code="de")
]
),
DocumentTranslationInput(
source_url="<sas_url_to_source_B>",
targets=[
TranslationTarget(target_url="<sas_url_to_target_fr>", language_code="fr"),
TranslationTarget(target_url="<sas_url_to_target_de>", language_code="de")
]
),
DocumentTranslationInput(
source_url="<sas_url_to_source_C>",
targets=[
TranslationTarget(target_url="<sas_url_to_target_fr>", language_code="fr"),
TranslationTarget(target_url="<sas_url_to_target_de>", language_code="de")
]
)
]
document_translation_client = DocumentTranslationClient("<endpoint>", AzureKeyCredential("<api_key>"))
poller = document_translation_client.begin_translation(my_input)
Note: the target_url for each target language must be unique.
See the service documentation for all supported languages.
Long-Running Operations
Long-running operations are operations which consist of an initial request sent to the service to start an operation, followed by polling the service at intervals to determine whether the operation has completed or failed, and if it has succeeded, to get the result.
Methods that translate documents are modeled as long-running operations.
The client exposes a begin_<method-name>
method that returns a DocumentTranslationLROPoller
or AsyncDocumentTranslationLROPoller
. Callers should wait
for the operation to complete by calling result()
on the poller object returned from the begin_<method-name>
method.
Sample code snippets are provided to illustrate using long-running operations below.
Examples
The following section provides several code snippets covering some of the most common Document Translation tasks, including:
Translate your documents
Translate the documents in your source container to the target container.
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient
endpoint = "https://<resource-name>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
source_container_sas_url_en = "<sas-url-en>"
target_container_sas_url_es = "<sas-url-es>"
document_translation_client = DocumentTranslationClient(endpoint, credential)
poller = document_translation_client.begin_translation(source_container_sas_url_en, target_container_sas_url_es, "es")
result = poller.result()
print("Status: {}".format(poller.status()))
print("Created on: {}".format(poller.details.created_on))
print("Last updated on: {}".format(poller.details.last_updated_on))
print("Total number of translations on documents: {}".format(poller.details.documents_total_count))
print("\nOf total documents...")
print("{} failed".format(poller.details.documents_failed_count))
print("{} succeeded".format(poller.details.documents_succeeded_count))
for document in result:
print("Document ID: {}".format(document.id))
print("Document status: {}".format(document.status))
if document.status == "Succeeded":
print("Source document location: {}".format(document.source_document_url))
print("Translated document location: {}".format(document.translated_document_url))
print("Translated to language: {}\n".format(document.translated_to))
else:
print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message))
Translate multiple inputs
Begin translating with documents in multiple source containers to multiple target containers in different languages.
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient, DocumentTranslationInput, TranslationTarget
endpoint = "https://<resource-name>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
source_container_sas_url_de = "<sas-url-de>"
source_container_sas_url_en = "<sas-url-en>"
target_container_sas_url_es = "<sas-url-es>"
target_container_sas_url_fr = "<sas-url-fr>"
target_container_sas_url_ar = "<sas-url-ar>"
document_translation_client = DocumentTranslationClient(endpoint, credential)
poller = document_translation_client.begin_translation(
[
DocumentTranslationInput(
source_url=source_container_sas_url_en,
targets=[
TranslationTarget(target_url=target_container_sas_url_es, language_code="es"),
TranslationTarget(target_url=target_container_sas_url_fr, language_code="fr"),
],
),
DocumentTranslationInput(
source_url=source_container_sas_url_de,
targets=[
TranslationTarget(target_url=target_container_sas_url_ar, language_code="ar"),
],
)
]
)
result = poller.result()
for document in result:
print("Document ID: {}".format(document.id))
print("Document status: {}".format(document.status))
if document.status == "Succeeded":
print("Source document location: {}".format(document.source_document_url))
print("Translated document location: {}".format(document.translated_document_url))
print("Translated to language: {}\n".format(document.translated_to))
else:
print("Error Code: {}, Message: {}\n".format(document.error.code, document.error.message))
List translation operations
Enumerate over the translation operations submitted for the resource.
from azure.core.credentials import AzureKeyCredential
from azure.ai.translation.document import DocumentTranslationClient
endpoint = "https://<resource-name>.cognitiveservices.azure.com/"
credential = AzureKeyCredential("<api_key>")
document_translation_client = DocumentTranslationClient(endpoint, credential)
operations = document_translation_client.list_translation_statuses() # type: ItemPaged[TranslationStatus]
for operation in operations:
print("\nID: {}".format(operation.id))
print("Status: {}".format(operation.status))
print("Created on: {}".format(operation.created_on))
print("Last updated on: {}".format(operation.last_updated_on))
print("Total number of translations on documents: {}".format(operation.documents_total_count))
print("Total number of characters charged: {}".format(operation.total_characters_charged))
print("Of total documents...")
print("{} failed".format(operation.documents_failed_count))
print("{} succeeded".format(operation.documents_succeeded_count))
print("{} canceled".format(operation.documents_canceled_count))
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. Note that you will need to install the azure-storage-blob library to run this sample.
Advanced Topics
The following section provides some insights for some advanced translation features such as glossaries and custom translation models.
Glossaries
Glossaries are domain-specific dictionaries. For example, if you want to translate some medical-related documents, you may need support for the many words, terminology, and idioms in the medical field which you can't find in the standard translation dictionary, or you simply need specific translation. This is why Document Translation provides support for glossaries.
How To Create Glossary File
Document Translation supports glossaries in the following formats:
File Type | Extension | Description | Samples |
---|---|---|---|
Tab-Separated Values/TAB | .tsv, .tab | Read more on wikipedia | glossary_sample.tsv |
Comma-Separated Values | .csv | Read more on wikipedia | glossary_sample.csv |
Localization Interchange File Format | .xlf, .xliff | Read more on wikipedia | glossary_sample.xlf |
View all supported formats here.
How Use Glossaries in Document Translation
In order to use glossaries with Document Translation, you first need to upload your glossary file to a blob container, and then provide the SAS URL to the file as in the code samples sample_translation_with_glossaries.py.
Custom Translation Models
Instead of using Document Translation's engine for translation, you can use your own custom Azure machine/deep learning model.
How To Create a Custom Translation Model
For more info on how to create, provision, and deploy your own custom Azure translation model, please follow the instructions here: Build, deploy, and use a custom model for translation
How To Use a Custom Translation Model With Document Translation
In order to use a custom translation model with Document Translation, you first need to create and deploy your model, then follow the code sample sample_translation_with_custom_model.py to use with Document Translation.
Troubleshooting
General
Document Translation client library will raise exceptions defined in Azure Core.
Logging
This library uses the standard logging library for logging.
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO
level.
Detailed DEBUG
level logging, including request/response bodies and unredacted
headers, can be enabled on the client or per-operation with the logging_enable
keyword argument.
See full SDK logging documentation with examples here.
Optional Configuration
Optional keyword arguments can be passed in at the client and per-operation level. The azure-core reference documentation describes available configurations for retries, logging, transport protocols, and more.
Next steps
The following section provides several code snippets illustrating common patterns used in the Document Translation Python client library.
More sample code
These code samples show common scenario operations with the Azure Document Translation client library.
- Client authentication: sample_authentication.py
- Begin translating documents: sample_begin_translation.py
- Translate with multiple inputs: sample_translate_multiple_inputs.py
- Check the status of documents: sample_check_document_statuses.py
- List all submitted translation operations: sample_list_translations.py
- Apply a custom glossary to translation: sample_translation_with_glossaries.py
- Use Azure Blob Storage to set up translation resources: sample_translation_with_azure_blob.py
Async samples
This library also includes a complete async API supported on Python 3.6+. To use it, you must
first install an async transport, such as aiohttp. Async clients
are found under the azure.ai.translation.document.aio
namespace.
- Client authentication: sample_authentication_async.py
- Begin translating documents: sample_begin_translation_async.py
- Translate with multiple inputs: sample_translate_multiple_inputs_async.py
- Check the status of documents: sample_check_document_statuses_async.py
- List all submitted translation operations: sample_list_translations_async.py
- Apply a custom glossary to translation: sample_translation_with_glossaries_async.py
- Use Azure Blob Storage to set up translation resources: sample_translation_with_azure_blob_async.py
Additional documentation
For more extensive documentation on Azure Cognitive Services Document Translation, see the Document Translation documentation on docs.microsoft.com.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Release History
1.0.0b5 (2021-09-08)
Breaking Changes
- Changed:
list_all_translation_statuses
has been renamed tolist_translation_statuses
- Changed:
list_all_document_statuses
has been renamed tolist_document_statuses
- Changed:
TranslationStatus
propertydocuments_cancelled_count
has been renamed todocuments_canceled_count
- Changed:
FileFormat
has been renamed toDocumentTranslationFileFormat
- Changed: Operation statuses
Cancelled
andCancelling
have been renamed toCanceled
andCanceling
, respectively.
Bugs Fixed
- The operation
id
underdetails
of the poller object now populates correctly.
1.0.0b4 (2021-08-10)
Features Added
- The single translation input version of
begin_translation(source, target, target_language_code)
now accepts keyword argumentsstorage_type
,glossaries
,category_id
,prefix
,suffix
, andsource_language_code
.
Breaking Changes
- Changed: renamed kwargs
translated_before
andtranslated_after
tocreated_before
andcreated_after
, respectively, forlist_all_document_statuses
. - Changed: renamed
order_by
sorting query optioncreatedDateTimeUtc
tocreated_on
forlist_all_translation_statuses
andlist_all_document_statuses
.
1.0.0b3 (2021-07-07)
Breaking changes
TranslationStatusResult
was renamed toTranslationStatus
.DocumentStatusResult
was renamed toDocumentStatus
.get_document_formats
was renamed toget_supported_document_formats
.get_glossary_formats
was renamed toget_supported_glossary_formats
.
1.0.0b2 (2021-06-08)
This version of the SDK defaults to the latest supported service version, which currently is v1.0
Breaking changes
create_translation_job
was removed and replaced withbegin_translation
which follows a long-running operation (LRO) approach. The client method now returns aDocumentTranslationLROPoller
(orAsyncDocumentTranslationLROPoller
) to begin the long-running operation. A call to.result()
can be made on the poller object to wait until the translation is complete. See the README for more information about LROs.- Upon completion of the LRO,
begin_translation
now returns a pageable ofDocumentStatusResult
. All job-level metadata can still be found onpoller.details
. has_completed
has been removed fromJobStatusResult
andDocumentStatusResult
. Usepoller.done()
to check if the translation has completed.- Client method
wait_until_done
has been removed. Usepoller.result()
to wait for the LRO to complete. - Client method
list_submitted_jobs
has been renamed tolist_all_translation_statuses
. - Client method
get_job_status
has been renamed toget_translation_status
. - Client method
cancel_job
has been renamed tocancel_translation
. - Parameter
job_id
was renamed totranslation_id
forget_translation_status
,cancel_translation
,list_all_document_statuses
, andget_document_status
. JobStatusResult
has been renamed toTranslationStatusResult
.DocumentStatusResult
propertytranslate_to
has been renamed totranslated_to
New features
- Authentication using
azure-identity
credentials now supported.- see the Azure Identity documentation for more information.
- Added paging and filtering options to
list_all_document_statuses
andlist_submitted_jobs
. - The input to
begin_translation
now accepts either the parameterinputs
as aList[DocumentTranslationInput]
to perform multiple translations, or the parameterssource_url
,target_url
, andtarget_language_code
to perform a single translation of your documents.
Dependency updates
- Package requires azure-core version 1.14.0 or greater.
1.0.0b1 (2021-04-06)
This is the first beta package of the azure-ai-translation-document client library that targets the Document Translation
service version 1.0-preview.1
. This package's documentation and samples demonstrate the new API.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file azure-ai-translation-document-1.0.0b5.zip
.
File metadata
- Download URL: azure-ai-translation-document-1.0.0b5.zip
- Upload date:
- Size: 140.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3828babf220f5618b68f0cce656e4cde7d632c7fa0791b6217f01c2c3b937b41 |
|
MD5 | d4358bb2c6b4d917086c2a75ff43b9b9 |
|
BLAKE2b-256 | 1eca83c4a7cae14869bd37bcc5cc448887e1a45c3c3488ee69a35d265cffced2 |
File details
Details for the file azure_ai_translation_document-1.0.0b5-py2.py3-none-any.whl
.
File metadata
- Download URL: azure_ai_translation_document-1.0.0b5-py2.py3-none-any.whl
- Upload date:
- Size: 62.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73146c42b741e6dfb1096cd2077f55478513cce269ee1c745557e7edef7f6a4c |
|
MD5 | 48f0ff31665a16cd41a370a855099fa1 |
|
BLAKE2b-256 | 706fb781e2f65044921430a55cdc04b8628423d1f59fcfa8a9e31d9d47f9bb69 |