async def sample_list_all_submitted_jobs_async():
    import os
    # [START list_all_jobs_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documenttranslation.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))
Beispiel #2
0
    def list_all_submitted_jobs(self):
        # import libraries
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.documenttranslation.aio import DocumentTranslationClient

        # get service secrets
        endpoint = os.environ["AZURE_DOCUMENT_TRANSLATION_ENDPOINT"]
        key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

        # create translation client
        client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

        # list submitted jobs
        jobs = client.list_submitted_jobs() # type: AsyncItemPaged[JobStatusResult]

        async for job in jobs:
            # wait for job to finish
            if job.status in ["NotStarted", "Running"]:
                job = 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("Of total documents...")
            print("{} failed".format(job.documents_failed_count))
            print("{} succeeded".format(job.documents_succeeded_count))
            print("{} in progress".format(job.documents_in_progress_count))
            print("{} not yet started".format(job.documents_not_yet_started_count))
            print("{} cancelled".format(job.documents_cancelled_count))
Beispiel #3
0
async def sample_translation_with_glossaries_async():
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documenttranslation.aio import DocumentTranslationClient
    from azure.ai.documenttranslation 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("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_document_status_checks_async():
    import os
    # [START create_translation_job_async]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documenttranslation.aio import DocumentTranslationClient
    from azure.ai.documenttranslation 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".
                              format(document.translated_document_url,
                                     document.translate_to))
                        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.")
Beispiel #5
0
    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 (DocumentTranslationInput,
                                                  TranslationTarget)

        # 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
        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_url,
                targets=[
                    TranslationTarget(target_url=target_container_url_es,
                                      language_code="es"),
                    TranslationTarget(target_url=target_container_url_fr,
                                      language_code="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(translation_inputs
                                                             )
            while True:
                job_detail = await client.get_job_status(
                    job_detail.id)  # type: JobStatusResult
                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
Beispiel #6
0
async def sample_authentication_api_key_async():
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.documenttranslation.aio import DocumentTranslationClient

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

    document_translation_client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

    # make calls with authenticated client
    async with document_translation_client:
        result = await document_translation_client.get_document_formats()
    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 (DocumentTranslationInput,
                                                  TranslationTarget)

        # 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
        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_url,
                targets=[
                    TranslationTarget(target_url=target_container_url_fr,
                                      language_code="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(translation_inputs
                                                             )

            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: JobStatusResult
            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 (DocumentTranslationInput,
                                                  TranslationTarget)

        # 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
        translation_inputs = [
            DocumentTranslationInput(
                source_url=source_container_url,
                targets=[
                    TranslationTarget(target_url=target_container_url_es,
                                      language_code="es")
                ],
                storage_type="file")
        ]

        # create translation client
        client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

        # run job
        async with client:
            job_detail = await client.create_translation_job(translation_inputs
                                                             )

            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: JobStatusResult

            if job_detail.status in ["Cancelled", "Cancelling"]:
                print("We cancelled job with ID: {}".format(job_detail.id))
Beispiel #9
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("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()
    async def batch_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_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"]

        # create service client
        client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

        # prepare translation job input
        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")
                ])
        ]

        # run translation job
        async with client:
            job_detail = await 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))

            # get job result
            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 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 (DocumentTranslationInput,
                                                  TranslationTarget)
        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

        translation_inputs = [
            DocumentTranslationInput(source_url=source_container_url,
                                     targets=[
                                         TranslationTarget(
                                             target_url=target_container_url,
                                             language_code="es")
                                     ],
                                     prefix="document")
        ]

        # run job
        async with translation_client:
            job_detail = await translation_client.create_translation_job(
                translation_inputs)
            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())