def sample_list_document_statuses_with_filters(): # import libraries from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document import ( DocumentTranslationClient, ) import os from datetime import datetime # obtain client secrets endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"] key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] translation_id = os.environ["TRANSLATION_ID"] # this should be the id for the translation operation you'd like to list docs for! # authorize client client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) # set your filters ''' Note: these are just sample values for the filters! please comment/uncomment/change what you are interested in using. ''' start = datetime(2021, 4, 12) end = datetime(2021, 4, 14) statuses = ["Canceled", "Failed"] order_by = ["created_on desc"] results_per_page = 2 skip = 3 filtered_docs = client.list_document_statuses( translation_id, # filters statuses=statuses, created_after=start, created_before=end, # ordering order_by=order_by, # paging skip=skip, results_per_page=results_per_page ).by_page() # check statuses for page in filtered_docs: for doc in page: display_doc_info(doc)
def sample_document_status_checks(): # [START list_document_statuses] import os import time 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"] target_container_url = os.environ["AZURE_TARGET_CONTAINER_URL"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) poller = client.begin_translation(source_container_url, target_container_url, "es") completed_docs = [] while not poller.done(): time.sleep(30) doc_statuses = client.list_document_statuses(poller.id) for document in doc_statuses: if document.id not in completed_docs: if document.status == "Succeeded": print( "Document at {} was translated to {} language. You can find translated document at {}" .format(document.source_document_url, document.translated_to, document.translated_document_url)) completed_docs.append(document.id) if document.status == "Failed": print( "Document at {} failed translation. Error Code: {}, Message: {}" .format(document.source_document_url, document.error.code, document.error.message)) completed_docs.append(document.id) if document.status == "Running": print( "Document ID: {}, translation progress is {} percent". format(document.id, document.translation_progress * 100)) print("\nTranslation completed.")
def sample_translation_with_custom_model(): 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"] target_container_url = os.environ["AZURE_TARGET_CONTAINER_URL"] custom_model_id = os.environ["AZURE_CUSTOM_MODEL_ID"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) poller = client.begin_translation(source_container_url, target_container_url, "es", category_id=custom_model_id) result = poller.result() print("Operation status: {}".format(result.status)) print("Operation created on: {}".format(result.created_on)) print("Operation last updated on: {}".format(result.last_updated_on)) print("Total number of translations on documents: {}".format( result.documents_total_count)) print("\nOf total documents...") print("{} failed".format(result.documents_failed_count)) print("{} succeeded".format(result.documents_succeeded_count)) doc_results = client.list_document_statuses(result.id) for document in doc_results: 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.translate_to)) else: print("Error Code: {}, Message: {}\n".format( document.error.code, document.error.message))
class TranslationPerfStressTest(PerfStressTest): def __init__(self, arguments): super().__init__(arguments) # test related env vars endpoint = os.environ["TRANSLATION_DOCUMENT_TEST_ENDPOINT"] key = os.environ["TRANSLATION_DOCUMENT_TEST_API_KEY"] self.storage_name = os.environ["TRANSLATION_DOCUMENT_STORAGE_NAME"] self.storage_key = os.environ["TRANSLATION_DOCUMENT_STORAGE_KEY"] self.storage_endpoint = "https://" + self.storage_name + ".blob.core.windows.net/" self.source_container_name = "source-perf-" + str(uuid.uuid4()) self.target_container_name = "target-perf-" + str(uuid.uuid4()) self.service_client = DocumentTranslationClient( endpoint, AzureKeyCredential(key), **self._client_kwargs) self.async_service_client = AsyncDocumentTranslationClient( endpoint, AzureKeyCredential(key), **self._client_kwargs) async def create_source_container(self): container_client = ContainerClient(self.storage_endpoint, self.source_container_name, self.storage_key) async with container_client: await container_client.create_container() docs = Document.create_docs(10) for blob in docs: await container_client.upload_blob(name=blob.prefix + blob.name + blob.suffix, data=blob.data) return self.generate_sas_url(self.source_container_name, "rl") async def create_target_container(self): container_client = ContainerClient(self.storage_endpoint, self.target_container_name, self.storage_key) async with container_client: await container_client.create_container() return self.generate_sas_url(self.target_container_name, "wl") def generate_sas_url(self, container_name, permission): sas_token = generate_container_sas(account_name=self.storage_name, container_name=container_name, account_key=self.storage_key, permission=permission, expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=2)) container_sas_url = self.storage_endpoint + container_name + "?" + sas_token return container_sas_url async def global_setup(self): """The global setup is run only once.""" self.source_container_sas_url = await self.create_source_container() self.target_container_sas_url = await self.create_target_container() poller = await self.async_service_client.begin_translation( self.source_container_sas_url, self.target_container_sas_url, "fr") self.translation_id = poller.id async def global_cleanup(self): """The global cleanup is run only once.""" blob_service_client = BlobServiceClient(self.storage_endpoint, self.storage_key) async with blob_service_client: await blob_service_client.delete_container( self.source_container_name) await blob_service_client.delete_container( self.target_container_name) async def close(self): """This is run after cleanup.""" await self.async_service_client.close() self.service_client.close() await super().close() def run_sync(self): """The synchronous perf test.""" statuses = self.service_client.list_document_statuses( self.translation_id) for doc in statuses: pass async def run_async(self): """The asynchronous perf test.""" statuses = self.async_service_client.list_document_statuses( self.translation_id) async for doc in statuses: pass