Last active
September 3, 2024 19:28
-
-
Save 7effrey89/9f5acda8974f5ef6ec26f069faf998cc to your computer and use it in GitHub Desktop.
Azure Document Translation using Access Token Authentication
This file contains hidden or 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
| # Pre-requisites: | |
| # 1. Create a service princple in Azure AD | |
| # 2. Create a new client secret for it (note down the client-id, tenant-id, and secret-value) | |
| # 3. Go to the Azure portal and find the Translator service among the Azure AI Services. | |
| # 4. In the Access Control (IAM) tab, assign the service principle the "Cognitive Services User" role | |
| # 5. In the Resource management tab, enable a System assigned Identity. | |
| # 6. under Azure Role Assignment, choose scope: Storage, Resource: your blob storage (where your documents reside), Role: "Storage Blob Data Contributor" | |
| # 7- Run the code below to get the service to translate all documents in one container to another container | |
| from azure.core.credentials import AccessToken, AzureKeyCredential, TokenCredential | |
| from azure.ai.translation.document import DocumentTranslationClient | |
| from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential | |
| import os | |
| AZURE_CLIENT_ID="<app-registration-client-id>" | |
| AZURE_TENANT_ID="<tenant-id>" | |
| AZURE_CLIENT_SECRET="<app-registration-client-secret-value" #Value | |
| os.environ["AZURE_CLIENT_ID"] = AZURE_CLIENT_ID | |
| os.environ["AZURE_CLIENT_SECRET"] = AZURE_CLIENT_SECRET | |
| os.environ["AZURE_TENANT_ID"] = AZURE_TENANT_ID | |
| # Language all documents will be translated to | |
| TRANSLATE_TO="da" | |
| # Custom credential class to use the token directly | |
| class CustomTokenCredential(TokenCredential): | |
| def __init__(self, token): | |
| self.token = token | |
| def get_token(self, *scopes, **kwargs): | |
| return AccessToken(self.token, float('inf')) | |
| # Add your endpoint | |
| endpoint = "https://<your-document-translator-single-resource>.cognitiveservices.azure.com/" | |
| credential = DefaultAzureCredential() | |
| #use token authentication | |
| token = credential.get_token("https://cognitiveservices.azure.com/.default").token | |
| custom_credential = CustomTokenCredential(token) | |
| # key_credential = AzureKeyCredential("api key") | |
| # Use the custom credential with the token | |
| document_translation_client = DocumentTranslationClient(endpoint, custom_credential) | |
| source_container_url_en = "https://<blob-storage-account>.blob.core.windows.net/<container>" | |
| target_container_url_da = "https://<blob-storage-account>.blob.core.windows.net/<container>" | |
| poller = document_translation_client.begin_translation(source_container_url_en, target_container_url_da, TRANSLATE_TO) | |
| result = poller.result() | |
| print(f"Status: {poller.status()}") | |
| print(f"Created on: {poller.details.created_on}") | |
| print(f"Last updated on: {poller.details.last_updated_on}") | |
| print(f"Total number of translations on documents: {poller.details.documents_total_count}") | |
| print("\nOf total documents...") | |
| print(f"{poller.details.documents_failed_count} failed") | |
| print(f"{poller.details.documents_succeeded_count} succeeded") | |
| 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment