Skip to content

Commit

Permalink
update samples - optional src language
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 48edd9e commit f418339
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def batch_translation_with_storage():
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
StorageTarget(
target_url=target_container_url,
Expand All @@ -78,7 +77,7 @@ def batch_translation_with_storage():
if job_result.documents_failed_count > 0:
check_documents(translation_client, job_result.id)

if job_result.status in ["Failed", "ValidationFailed"]:
elif 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)
Expand Down Expand Up @@ -117,3 +116,7 @@ def check_documents(client, job_id):
))
if document.url not in docs_to_retry:
docs_to_retry.append(document.url)


if __name__ == '__main__':
batch_translation_with_storage()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ------------------------------------


def sample_batch_translation():
def sample_translation_status_checks():
import os
import time
from azure.core.credentials import AzureKeyCredential
Expand All @@ -26,7 +26,6 @@ def sample_batch_translation():
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
StorageTarget(
target_url=target_container_url_es,
Expand All @@ -47,16 +46,16 @@ def sample_batch_translation():
while True:
job_detail = client.get_job_status(job_detail.id) # type: JobStatusDetail
if job_detail.status in ["NotStarted", "Running"]:
time.sleep(10)
time.sleep(30)
continue

if job_detail.status in ["Failed", "ValidationFailed"]:
elif 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 job_detail.status == "Succeeded":
elif job_detail.status == "Succeeded":
print("We translated our documents!")
if job_detail.documents_failed_count > 0:
check_documents(client, job_detail.id)
Expand All @@ -69,7 +68,7 @@ def check_documents(client, job_id):
try:
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.")
print("Failed to process any documents in source/target container due to insufficient permissions.")
raise err

docs_to_retry = []
Expand All @@ -83,3 +82,7 @@ def check_documents(client, job_id):
))
if document.url not in docs_to_retry:
docs_to_retry.append(document.url)


if __name__ == '__main__':
sample_translation_status_checks()
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ def sample_list_all_jobs():
key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"]

client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))
jobs = client.list_submitted_jobs()
jobs = client.list_submitted_jobs() # type: ItemPaged[JobStatusDetail]

for job in jobs:
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))


if __name__ == '__main__':
sample_list_all_jobs()

0 comments on commit f418339

Please sign in to comment.