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 (BatchDocumentInput, StorageTarget) # 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 batch = [ BatchDocumentInput( source_url=source_container_url, targets=[ StorageTarget(target_url=target_container_url_es, language="es"), StorageTarget(target_url=target_container_url_fr, language="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(batch) while True: job_detail = await client.get_job_status( job_detail.id) # type: JobStatusDetail 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
def sample_batch_translation(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import (DocumentTranslationClient, BatchDocumentInput, StorageTarget) 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)) batch = [ BatchDocumentInput( source_url=source_container_url_en, targets=[ StorageTarget(target_url=target_container_url_es, language="es"), StorageTarget(target_url=target_container_url_fr, language="fr") ]), BatchDocumentInput( source_url=source_container_url_de, targets=[ StorageTarget(target_url=target_container_url_es, language="es"), StorageTarget(target_url=target_container_url_fr, language="fr") ]) ] job_detail = client.create_translation_job(batch) # type: JobStatusDetail 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: JobStatusDetail 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 sample_translation_status_checks(): import os import time from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import (DocumentTranslationClient, BatchDocumentInput, StorageTarget) 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)) batch = [ BatchDocumentInput( source_url=source_container_url, targets=[ StorageTarget(target_url=target_container_url_es, language="es"), StorageTarget(target_url=target_container_url_fr, language="fr") ], storage_type="folder", prefix="document_2021") ] job_detail = client.create_translation_job(batch) while True: job_detail = client.get_job_status( job_detail.id) # type: JobStatusDetail 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
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 (BatchDocumentInput, StorageTarget) # 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 batch = [ BatchDocumentInput(source_url=source_container_url, targets=[ StorageTarget( target_url=target_container_url_fr, language="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(batch) 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: JobStatusDetail 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 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 (BatchDocumentInput, StorageTarget) # 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 batch = [ BatchDocumentInput(source_url=source_container_url, targets=[ StorageTarget( target_url=target_container_url_es, language="es") ], storage_type="file") ] # create translation client client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) # run job async with client: job_detail = await client.create_translation_job(batch) 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: JobStatusDetail if job_detail.status in ["Cancelled", "Cancelling"]: print("We cancelled job with ID: {}".format(job_detail.id))
def sample_cancel_translation_job(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, BatchDocumentInput, StorageTarget ) 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)) batch = [ BatchDocumentInput( source_url=source_container_url, targets=[ StorageTarget( target_url=target_container_url_es, language="es" ) ], storage_type="file" ) ] job_detail = client.create_translation_job(batch) # type: JobStatusDetail 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: JobStatusDetail if job_detail.status in ["Cancelled", "Cancelling"]: print("We cancelled job with ID: {}".format(job_detail.id))
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 (BatchDocumentInput, StorageTarget) 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 batch = [ BatchDocumentInput(source_url=source_container_url, targets=[ StorageTarget( target_url=target_container_url, language="es") ], prefix="document") ] # run job async with translation_client: job_detail = await translation_client.create_translation_job(batch) 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())
def sample_batch_translation_with_storage(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.documenttranslation import ( DocumentTranslationClient, BatchDocumentInput, StorageTarget ) 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 batch = [ BatchDocumentInput( source_url=source_container_url, targets=[ StorageTarget( target_url=target_container_url, language="es" ) ], prefix="document" ) ] job_detail = translation_client.create_translation_job(batch) 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())