Skip to content

Commit

Permalink
renaming to use job terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapratico authored and MIDDLEEAST\v-moshaban committed Mar 10, 2021
1 parent d14fcac commit 48edd9e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
__all__ = [
"DocumentTranslationClient",
"DocumentTranslationVersion",
"BatchTranslationInput",
"BatchDocumentInput",
"TranslationGlossary",
"StorageInputType",
"FileFormat",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(
self.storage_source = kwargs.get("storage_source", None)


class BatchTranslationInput(object):
class BatchDocumentInput(object):
"""Definition for the input batch translation request.
:param source_url: Required. Location of the folder / container or single file with your
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def batch_translation_with_storage():
from azure.core.credentials import AzureKeyCredential
from azure.ai.documenttranslation import (
DocumentTranslationClient,
BatchTranslationInput,
BatchDocumentInput,
StorageTarget
)
from azure.storage.blob import ContainerClient, generate_container_sas, ContainerSasPermissions
Expand All @@ -26,7 +26,7 @@ def batch_translation_with_storage():
target_storage_container_name = os.environ["AZURE_STORAGE_TARGET_CONTAINER_NAME"]
target_storage_key = os.environ["AZURE_STORAGE_TARGET_KEY"]

batch_client = DocumentTranslationClient(
translation_client = DocumentTranslationClient(
endpoint, AzureKeyCredential(key)
)

Expand Down Expand Up @@ -57,7 +57,7 @@ def batch_translation_with_storage():
target_container_url = target_storage_endpoint + "/" + target_storage_container_name + "?" + target_container_sas

batch = [
BatchTranslationInput(
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
Expand All @@ -70,18 +70,18 @@ def batch_translation_with_storage():
)
]

batch_detail = batch_client.create_batch(batch)
batch_result = batch_client.wait_until_done(batch_detail.id)
job_detail = translation_client.create_translation_job(batch)
job_result = translation_client.wait_until_done(job_detail.id)

if batch_result.status == "Succeeded":
if job_result.status == "Succeeded":
print("We translated our documents!")
if batch_result.documents_failed_count > 0:
check_documents(batch_client, batch_result.id)
if job_result.documents_failed_count > 0:
check_documents(translation_client, job_result.id)

if batch_result.status in ["Failed", "ValidationFailed"]:
if batch_result.error:
print("Batch failed: {}: {}".format(batch_result.error.code, batch_result.error.message))
check_documents(batch_client, batch_result.id)
if 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(
Expand All @@ -97,11 +97,11 @@ def batch_translation_with_storage():
my_blob.write(download_stream.readall())


def check_documents(client, batch_id):
def check_documents(client, job_id):
from azure.core.exceptions import ResourceNotFoundError

try:
doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail]
doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail]
except ResourceNotFoundError as err:
print("Failed to process any documents in source/target container.")
raise err
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def sample_batch_translation():
from azure.core.credentials import AzureKeyCredential
from azure.ai.documenttranslation import (
DocumentTranslationClient,
BatchTranslationInput,
BatchDocumentInput,
StorageTarget
)

Expand All @@ -24,7 +24,7 @@ def sample_batch_translation():
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

batch = [
BatchTranslationInput(
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
Expand All @@ -42,32 +42,32 @@ def sample_batch_translation():
)
]

batch_detail = client.create_batch(batch)
job_detail = client.create_translation_job(batch)

while True:
batch_detail = client.get_batch_status(batch_detail.id) # type: BatchStatusDetail
if batch_detail.status in ["NotStarted", "Running"]:
time.sleep(5)
job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail
if job_detail.status in ["NotStarted", "Running"]:
time.sleep(10)
continue

if batch_detail.status in ["Failed", "ValidationFailed"]:
if batch_detail.error:
print("Batch failed: {}: {}".format(batch_detail.error.code, batch_detail.error.message))
check_documents(client, batch_detail.id)
if 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)

if batch_detail.status == "Succeeded":
if job_detail.status == "Succeeded":
print("We translated our documents!")
if batch_detail.documents_failed_count > 0:
check_documents(client, batch_detail.id)
if job_detail.documents_failed_count > 0:
check_documents(client, job_detail.id)
break


def check_documents(client, batch_id):
def check_documents(client, job_id):
from azure.core.exceptions import ResourceNotFoundError

try:
doc_statuses = client.list_documents_statuses(batch_id) # type: ItemPaged[DocumentStatusDetail]
doc_statuses = client.list_documents_statuses(job_id) # type: ItemPaged[DocumentStatusDetail]
except ResourceNotFoundError as err:
print("Failed to process any documents in source/target container.")
raise err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ------------------------------------


def sample_list_all_batches():
def sample_list_all_jobs():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documenttranslation import (
Expand All @@ -16,19 +16,18 @@ def sample_list_all_batches():
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
batches = client.list_batches_statuses()
jobs = client.list_submitted_jobs()

print("Batches summary")
for batch in batches:
print("Batch ID: {}".format(batch.id))
print("Batch status: {}".format(batch.status))
print("Batch created on: {}".format(batch.created_on))
print("Batch last updated on: {}".format(batch.last_updated_on))
print("Batch number of translations on documents: {}".format(batch.documents_total_count))
for job in jobs:
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("Of total documents...")
print("{} failed".format(batch.documents_failed_count))
print("{} succeeded".format(batch.documents_succeeded_count))
print("{} in progress".format(batch.documents_in_progress_count))
print("{} not yet started".format(batch.documents_not_yet_started_count))
print("{} cancelled".format(batch.documents_cancelled_count))
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))

0 comments on commit 48edd9e

Please sign in to comment.