def test_translation_with_glossary(self, client): doc = Document(data=b'testing') source_container_sas_url = self.create_source_container(data=[doc]) target_container_sas_url = self.create_target_container() container_client = ContainerClient(self.storage_endpoint, self.source_container_name, self.storage_key) with open(GLOSSARY_FILE_NAME, "rb") as fd: container_client.upload_blob(name=GLOSSARY_FILE_NAME, data=fd.read()) prefix, suffix = source_container_sas_url.split("?") glossary_file_sas_url = prefix + "/" + GLOSSARY_FILE_NAME + "?" + suffix poller = client.begin_translation( source_container_sas_url, target_container_sas_url, "es", glossaries=[TranslationGlossary(glossary_url=glossary_file_sas_url, file_format="csv")] ) result = poller.result() container_client = ContainerClient(self.storage_endpoint, self.target_container_name, self.storage_key) # download translated file and assert that translation reflects glossary changes document = doc.name + doc.suffix with open(document, "wb") as my_blob: download_stream = container_client.download_blob(document) my_blob.write(download_stream.readall()) with open(document, "rb") as fd: translated = fd.readline() assert b'essai' in translated # glossary worked os.remove(document)
async def sample_translation_with_glossaries_async(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document.aio import DocumentTranslationClient from azure.ai.translation.document import (DocumentTranslationInput, TranslationTarget, TranslationGlossary) 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"] glossary_url = os.environ["AZURE_TRANSLATION_GLOSSARY_URL"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) inputs = DocumentTranslationInput( source_url=source_container_url, targets=[ TranslationTarget(target_url=target_container_url, language_code="es", glossaries=[ TranslationGlossary( glossary_url=glossary_url, file_format="TSV") ]) ]) async with client: job = await client.create_translation_job(inputs=[inputs] ) # type: JobStatusResult job_result = await client.wait_until_done(job.id ) # type: JobStatusResult print("Job status: {}".format(job_result.status)) print("Job created on: {}".format(job_result.created_on)) print("Job last updated on: {}".format(job_result.last_updated_on)) print("Total number of translations on documents: {}".format( job_result.documents_total_count)) print("\nOf total documents...") print("{} failed".format(job_result.documents_failed_count)) print("{} succeeded".format(job_result.documents_succeeded_count)) doc_results = client.list_all_document_statuses( job_result.id) # type: AsyncItemPaged[DocumentStatusResult] async 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))
async def sample_translation_with_glossaries_async(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document.aio import DocumentTranslationClient from azure.ai.translation.document import (DocumentTranslationInput, TranslationTarget, TranslationGlossary) 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"] glossary_url = os.environ["AZURE_TRANSLATION_GLOSSARY_URL"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) inputs = DocumentTranslationInput( source_url=source_container_url, targets=[ TranslationTarget(target_url=target_container_url, language_code="es", glossaries=[ TranslationGlossary( glossary_url=glossary_url, file_format="TSV") ]) ]) async with client: poller = await client.begin_translation(inputs=[inputs]) result = await 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)) async 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))
def sample_translation_with_glossaries(): import os from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document import (DocumentTranslationClient, TranslationGlossary) 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"] glossary_url = os.environ["AZURE_TRANSLATION_GLOSSARY_URL"] client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) poller = client.begin_translation(source_container_url, target_container_url, "es", glossaries=[ TranslationGlossary( glossary_url=glossary_url, file_format="TSV") ]) 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" )
def test_single_input_with_kwargs(self, **kwargs): client = kwargs.pop("client") variables = kwargs.pop("variables", {}) # prepare containers and test data source_container_sas_url = self.create_source_container( data=Document(data=b'hello world'), variables=variables) target_container_sas_url = self.create_target_container( variables=variables) def callback(request): req = _StartTranslationDetails.deserialize( json.loads(request.http_request.body)) input = req.inputs[0] assert input.source.source_url == source_container_sas_url assert input.source.language == "en" assert input.source.filter.prefix == "" assert input.source.filter.suffix == ".txt" assert input.storage_type == "File" assert input.targets[0].category == "fake" assert input.targets[0].glossaries[0].format == "txt" assert input.targets[0].glossaries[ 0].glossary_url == "https://glossaryfile.txt" assert input.targets[0].language == "es" assert input.targets[0].target_url == target_container_sas_url try: poller = client.begin_translation( source_container_sas_url, target_container_sas_url, "es", storage_type="File", source_language_code="en", prefix="", suffix=".txt", category_id="fake", glossaries=[ TranslationGlossary( glossary_url="https://glossaryfile.txt", file_format="txt") ], raw_response_hook=callback) poller.result() except HttpResponseError as e: pass return variables