示例#1
0
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))
示例#2
0
async def sample_list_all_submitted_jobs_async():
    import os
    # [START list_all_jobs_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
    async with client:
        translation_jobs = client.list_submitted_jobs()  # type: AsyncItemPaged[JobStatusResult]

        async for job in translation_jobs:
            if job.status == "Running":
                job = await client.wait_until_done(job.id)

            print("Job ID: {}".format(job.id))
            print("Job status: {}".format(job.status))
            print("Job created on: {}".format(job.created_on))
            print("Job last updated on: {}".format(job.last_updated_on))
            print("Total number of translations on documents: {}".format(job.documents_total_count))
            print("Total number of characters charged: {}".format(job.total_characters_charged))

            print("\nOf total documents...")
            print("{} failed".format(job.documents_failed_count))
            print("{} succeeded".format(job.documents_succeeded_count))
            print("{} cancelled\n".format(job.documents_cancelled_count))
async def sample_list_translations_async():
    import os
    # [START list_translations_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
    async with client:
        operations = client.list_translation_statuses(
        )  # type: AsyncItemPaged[TranslationStatus]

        async for operation in operations:
            print(f"ID: {operation.id}")
            print(f"Status: {operation.status}")
            print(f"Created on: {operation.created_on}")
            print(f"Last updated on: {operation.last_updated_on}")
            print(
                f"Total number of operations on documents: {operation.documents_total_count}"
            )
            print(
                f"Total number of characters charged: {operation.total_characters_charged}"
            )

            print("\nOf total documents...")
            print(f"{operation.documents_failed_count} failed")
            print(f"{operation.documents_succeeded_count} succeeded")
            print(f"{operation.documents_canceled_count} canceled\n")
示例#4
0
async def sample_document_status_checks_async():
    import os
    # [START create_translation_job_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient
    from azure.ai.translation.document import (DocumentTranslationInput,
                                               TranslationTarget)

    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))
    async with client:
        job_result = await client.create_translation_job(
            inputs=[
                DocumentTranslationInput(
                    source_url=source_container_url,
                    targets=[
                        TranslationTarget(target_url=target_container_url,
                                          language_code="es")
                    ])
            ])  # type: JobStatusResult
        # [END create_translation_job_async]

        completed_docs = []
        while not job_result.has_completed:
            await asyncio.sleep(30)

            doc_statuses = client.list_all_document_statuses(job_result.id)
            async 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.translate_to,
                                    document.translated_document_url))
                        completed_docs.append(document.id)
                    if document.status == "Failed":
                        print("Document ID: {}, Error Code: {}, Message: {}".
                              format(document.id, 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))

            job_result = await client.get_job_status(job_result.id)

        print("\nTranslation job completed.")
示例#5
0
    async def test_active_directory_auth_async(self, **kwargs):
        translation_document_test_endpoint = kwargs.pop("translation_document_test_endpoint")
        token = self.get_credential(DocumentTranslationClient, is_async=True)
        kwargs = {}
        if os.getenv("AZURE_COGNITIVE_SCOPE"):
            kwargs["credential_scopes"] = [os.getenv("AZURE_COGNITIVE_SCOPE")]
        client = DocumentTranslationClient(translation_document_test_endpoint, token, **kwargs)
        # prepare containers and test data
        blob_data = b'This is some text'
        source_container_sas_url = self.create_source_container(data=Document(data=blob_data))
        target_container_sas_url = self.create_target_container()

        # prepare translation inputs
        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_sas_url,
                targets=[
                    TranslationTarget(
                        target_url=target_container_sas_url,
                        language_code="fr"
                    )
                ]
            )
        ]

        # submit translation and test
        await self._begin_and_validate_translation_async(client, translation_inputs, 1, "fr")
示例#6
0
async def sample_translation_under_folder_async():
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio 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"]  # do not include the folder name in source url
    target_container_url = os.environ["AZURE_TARGET_CONTAINER_URL"]

    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
    async with client:
        poller = await client.begin_translation(source_container_url,
                                                target_container_url,
                                                "fr",
                                                prefix="myfoldername")
        result = await poller.result()

        async 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"
                )
示例#7
0
async def sample_translation_specific_document_async():
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
    source_blob_url = os.environ["AZURE_SOURCE_BLOB_URL"]
    target_blob_url = os.environ["AZURE_TARGET_BLOB_URL"]

    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
    async with client:
        poller = await client.begin_translation(source_blob_url,
                                                target_blob_url,
                                                "fr",
                                                storage_type="File")
        result = await poller.result()

        async 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"
                )
async def sample_list_document_statuses_with_filters_async():
    # import libraries
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import (
        DocumentTranslationClient,
    )
    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 = ["Cancelled", "Failed"]
    order_by = ["createdDateTimeUtc desc"]
    results_per_page = 2
    skip = 3

    async with client:
        filtered_docs = client.list_all_document_statuses(
            translation_id,
            # filters
            statuses=statuses,
            translated_after=start,
            translated_before=end,
            # ordering
            order_by=order_by,
            # paging
            skip=skip,
            results_per_page=results_per_page
        ).by_page()

        # check statuses
        async for page in filtered_docs:
            async for doc in page:
                display_doc_info(doc)
示例#9
0
async def sample_multiple_translation_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)

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]
    source_container_url_1 = os.environ["AZURE_SOURCE_CONTAINER_URL_1"]
    source_container_url_2 = os.environ["AZURE_SOURCE_CONTAINER_URL_2"]
    target_container_url_fr = os.environ["AZURE_TARGET_CONTAINER_URL_FR"]
    target_container_url_ar = os.environ["AZURE_TARGET_CONTAINER_URL_AR"]
    target_container_url_es = os.environ["AZURE_TARGET_CONTAINER_URL_ES"]

    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

    async with client:
        poller = await client.begin_translation(inputs=[
            DocumentTranslationInput(
                source_url=source_container_url_1,
                targets=[
                    TranslationTarget(target_url=target_container_url_fr,
                                      language_code="fr"),
                    TranslationTarget(target_url=target_container_url_ar,
                                      language_code="ar")
                ]),
            DocumentTranslationInput(
                source_url=source_container_url_2,
                targets=[
                    TranslationTarget(target_url=target_container_url_es,
                                      language_code="es")
                ])
        ])
        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))
示例#10
0
async def sample_document_status_checks_async():
    # [START list_document_statuses_async]
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio 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))

    async with client:
        poller = await client.begin_translation(source_container_url,
                                                target_container_url, "es")

        completed_docs = []
        while poller.status() in ["Running", "NotStarted"]:
            await asyncio.sleep(30)

            doc_statuses = client.list_document_statuses(poller.id)
            async 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.")
async def sample_list_translations_with_filters_async():
    # import libraries
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    from datetime import datetime

    # obtain client secrets
    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

    # authorize client
    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
    async with client:
        # 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

        # list translation operations
        submitted_translations = client.list_translation_statuses(
            # 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
        async for page in submitted_translations:
            async for translation in page:
                display_translation_info(translation)
async def sample_translation_with_custom_model_async():
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio 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))

    async with client:
        poller = await client.begin_translation(source_container_url,
                                                target_container_url,
                                                "es",
                                                category_id=custom_model_id)
        result = await 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)
        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))
示例#13
0
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 (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))

    async with client:
        poller = await client.begin_translation(
            source_container_url,
            target_container_url,
            "es",
            glossaries=[
                TranslationGlossary(glossary_url=glossary_url,
                                    file_format="TSV")
            ])

        result = await 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")

        async 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"
                )
async def sample_authentication_api_key_async():
    # [START create_dt_client_with_key_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

    document_translation_client = DocumentTranslationClient(
        endpoint, AzureKeyCredential(key))
    # [END create_dt_client_with_key_async]

    # make calls with authenticated client
    async with document_translation_client:
        result = await document_translation_client.get_document_formats()
    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 sample_authentication_with_azure_active_directory_async():
    # [START create_dt_client_with_aad_async]
    """DefaultAzureCredential will use the values from these environment
    variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
    """
    from azure.identity.aio import DefaultAzureCredential
    from azure.ai.translation.document.aio import DocumentTranslationClient

    endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
    credential = DefaultAzureCredential()

    document_translation_client = DocumentTranslationClient(
        endpoint, credential)
    # [END create_dt_client_with_aad_async]

    # make calls with authenticated client
    async with document_translation_client:
        result = await document_translation_client.get_supported_document_formats(
        )
示例#17
0
async def sample_translation_async():
    # [START begin_translation_async]
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document.aio 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))

    async with client:
        poller = await client.begin_translation(source_container_url,
                                                target_container_url, "fr")
        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))
示例#18
0
    async def test_active_directory_auth_async(self):
        token = self.generate_oauth_token()
        endpoint = self.get_oauth_endpoint()
        client = DocumentTranslationClient(endpoint, token)
        # prepare containers and test data
        blob_data = b'This is some text'
        source_container_sas_url = self.create_source_container(data=Document(
            data=blob_data))
        target_container_sas_url = self.create_target_container()

        # prepare translation inputs
        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_sas_url,
                targets=[
                    TranslationTarget(target_url=target_container_sas_url,
                                      language_code="fr")
                ])
        ]

        # submit translation and test
        await self._begin_and_validate_translation_async(
            client, translation_inputs, 1, "fr")
示例#19
0
    async def sample_translation_with_azure_blob(self):

        translation_client = DocumentTranslationClient(
            self.endpoint, AzureKeyCredential(self.key)
        )

        blob_service_client = BlobServiceClient(
            self.storage_endpoint,
            credential=self.storage_key
        )

        source_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_source_container_name or "translation-source-container-async",
        )
        target_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_target_container_name or "translation-target-container-async"
        )

        if self.document_name:
            with open(self.document_name, "rb") as doc:
                await source_container.upload_blob(self.document_name, doc)
        else:
            self.document_name = "example_document.txt"
            await source_container.upload_blob(
                name=self.document_name,
                data=b"This is an example translation with the document translation client library"
            )
        print(f"Uploaded document {self.document_name} to storage container {source_container.container_name}")

        source_container_sas_url = self.generate_sas_url(source_container, permissions="rl")
        target_container_sas_url = self.generate_sas_url(target_container, permissions="wl")

        poller = await translation_client.begin_translation(source_container_sas_url, target_container_sas_url, "fr")
        print(f"Created translation operation with ID: {poller.id}")
        print("Waiting until translation completes...")

        result = await poller.result()
        print(f"Status: {poller.status()}")

        print("\nDocument results:")
        async 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")

                blob_client = BlobClient.from_blob_url(document.translated_document_url, credential=self.storage_key)
                async with blob_client:
                    with open("translated_"+self.document_name, "wb") as my_blob:
                        download_stream = await blob_client.download_blob()
                        my_blob.write(await download_stream.readall())

                print("Downloaded {} locally".format("translated_"+self.document_name))
            else:
                print("\nThere was a problem translating your document.")
                print(f"Document Error Code: {document.error.code}, Message: {document.error.message}\n")

        await translation_client.close()
        await blob_service_client.close()
示例#20
0
    async def sample_translation_with_azure_blob(self):

        translation_client = DocumentTranslationClient(
            self.endpoint, AzureKeyCredential(self.key))

        blob_service_client = BlobServiceClient(self.storage_endpoint,
                                                credential=self.storage_key)

        source_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_source_container_name
            or "translation-source-container",
        )
        target_container = await self.create_container(
            blob_service_client,
            container_name=self.storage_target_container_name
            or "translation-target-container")

        if self.document_name:
            with open(self.document_name, "rb") as doc:
                await source_container.upload_blob(self.document_name, doc)
        else:
            self.document_name = "example_document.txt"
            await source_container.upload_blob(
                name=self.document_name,
                data=
                b"This is an example translation with the document translation client library"
            )
        print("Uploaded document {} to storage container {}".format(
            self.document_name, source_container.container_name))

        source_container_sas_url = self.generate_sas_url(source_container,
                                                         permissions="rl")
        target_container_sas_url = self.generate_sas_url(target_container,
                                                         permissions="wl")

        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_sas_url,
                targets=[
                    TranslationTarget(target_url=target_container_sas_url,
                                      language_code="fr")
                ])
        ]

        job = await translation_client.create_translation_job(
            translation_inputs)
        print("Created translation job with ID: {}".format(job.id))
        print("Waiting until job completes...")

        job_result = await translation_client.wait_until_done(job.id)
        print("Job status: {}".format(job_result.status))

        doc_results = translation_client.list_all_document_statuses(
            job_result.id)

        print("\nDocument results:")
        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))

                blob_client = BlobClient.from_blob_url(
                    document.translated_document_url,
                    credential=self.storage_key)
                async with blob_client:
                    with open("translated_" + self.document_name,
                              "wb") as my_blob:
                        download_stream = await blob_client.download_blob()
                        my_blob.write(await download_stream.readall())

                print("Downloaded {} locally".format("translated_" +
                                                     self.document_name))
            else:
                print("\nThere was a problem translating your document.")
                print("Document Error Code: {}, Message: {}\n".format(
                    document.error.code, document.error.message))

        await translation_client.close()
        await blob_service_client.close()
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