Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples hero scenarios #16936

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[samples] updated async samples to comply with new changes
  • Loading branch information
MIDDLEEAST\v-moshaban committed Mar 3, 2021
commit 26d3d50565176eef30c7f559fd1a0f6f0f15928f
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@ async def batch_translation_async(self):
# 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"]
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"]

@@ -32,8 +33,7 @@ async def batch_translation_async(self):
# prepare translation job input
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
source_url=source_container_url_en,
targets=[
StorageTarget(
target_url=target_container_url_es,
@@ -43,8 +43,20 @@ async def batch_translation_async(self):
target_url=target_container_url_fr,
language="fr"
)
],
storage_type="file"
]
),
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"
)
]
)
]

@@ -62,7 +74,7 @@ async def batch_translation_async(self):
if job_result.documents_failed_count > 0:
await self.check_documents(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))
await self.check_documents(client, job_result.id)
@@ -75,7 +87,7 @@ async def check_documents(self, client, job_id):
try:
doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[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 = []
Original file line number Diff line number Diff line change
@@ -72,7 +72,6 @@ async def batch_translation_with_storage_async(self):
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
StorageTarget(
target_url=target_container_url,
@@ -94,7 +93,7 @@ async def batch_translation_with_storage_async(self):
if job_result.documents_failed_count > 0:
await self.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))
await self.check_documents(translation_client, job_result.id)
@@ -118,7 +117,7 @@ async def check_documents(self, client, job_id):
try:
doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[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 = []
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@ async def cancel_translation_job_async(self):
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
StorageTarget(
target_url=target_container_url_es,
Original file line number Diff line number Diff line change
@@ -29,12 +29,12 @@ async def custom_translation_async(self):
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
StorageTarget(
target_url=target_container_url_fr,
language="fr",
category_id=category_id
category_id=category_id,
glossaries=["https://exampleglossary"]
)
],
prefix="document_2021"
@@ -57,11 +57,11 @@ async def custom_translation_async(self):
if job_result.documents_failed_count > 0:
await self.check_documents(client, job_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))
await self.check_documents(client, job_result.id)
exit(1)
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 check_documents(self, client, job_id):
@@ -70,7 +70,7 @@ async def check_documents(self, client, job_id):
try:
doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[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 = []
Original file line number Diff line number Diff line change
@@ -22,14 +22,19 @@ def list_all_submitted_jobs(self):
client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

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

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))
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
import asyncio
import time

class CheckStatusesSampleAsync(object):
class TranslationStatusChecksSampleAsync(object):

async def check_statuses_async(self):
async def translation_status_checks_async(self):

# import libraries
from azure.core.credentials import AzureKeyCredential
@@ -31,7 +31,6 @@ async def check_statuses_async(self):
batch = [
BatchDocumentInput(
source_url=source_container_url,
source_language="en",
targets=[
StorageTarget(
target_url=target_container_url_es,
@@ -59,13 +58,13 @@ async def check_statuses_async(self):
await asyncio.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))
await self.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:
await self.check_documents(client, job_detail.id)
@@ -78,7 +77,7 @@ async def check_documents(self, client, job_id):
try:
doc_statuses = client.list_documents_statuses(job_id) # type: AsyncItemPaged[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 = []
@@ -95,8 +94,8 @@ async def check_documents(self, client, job_id):


async def main():
sample = CheckStatusesSampleAsync()
await sample.check_statuses_async()
sample = TranslationStatusChecksSampleAsync()
await sample.translation_status_checks_async()


if __name__ == '__main__':
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
# ------------------------------------


def batch_translation_with_storage():
def sample_batch_translation_with_storage():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documenttranslation import (
@@ -102,7 +102,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 = []
@@ -119,4 +119,4 @@ def check_documents(client, job_id):


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


def sample_cancel_job():
def sample_cancel_translation_job():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documenttranslation import (
@@ -47,4 +47,4 @@ def sample_cancel_job():


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


def sample_list_all_jobs():
def sample_list_all_submitted_jobs():
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documenttranslation import (
@@ -38,4 +38,4 @@ def sample_list_all_jobs():


if __name__ == '__main__':
sample_list_all_jobs()
sample_list_all_submitted_jobs()