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

Add code samples for document translator sdk #21214

Merged
merged 8 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<WarningLevel>5</WarningLevel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.Translation.Document" Version="1.0.0-beta.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.8.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// <copyright file="SampleCheckDocumentStatuses.cs" company="Microsoft Corporation">
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// </copyright>

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace DocumentTranslatorSamples
{
public class SamplesMain
{
public async static Task Main(string[] args)
{
await SampleListAllSubmittedJobs.RunSampleAsync().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// <copyright file="SampleAuthentication.cs" company="Microsoft Corporation">
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// </copyright>
using Azure;
using Azure.AI.Translation.Document;
using System;
using System.Threading.Tasks;

/**
FILE: SampleAuthentication.cs
DESCRIPTION:
This sample demonstrates how to authenticate to the Document Translation service.
1) Use a Document Translation API key with AzureKeyCredential from azure.core.credentials
Note: the endpoint must be formatted to use the custom domain name for your resource:
https://<NAME-OF-YOUR-RESOURCE>.cognitiveservices.azure.com/
USAGE:
Set the environment variables with your own values before running the sample:
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key
**/

namespace DocumentTranslatorSamples
{
public class SampleAuthentication
FadyEssam marked this conversation as resolved.
Show resolved Hide resolved
{
public async static Task RunSampleAsync()
{
var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_KEY");

var documentTranslationClient = new DocumentTranslationClient(endpoint, new AzureKeyCredential(key));

// Make calls with authenticated client
var result = await documentTranslationClient.GetDocumentFormatsAsync().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// <copyright file="SampleCheckDocumentStatuses.cs" company="Microsoft Corporation">
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// </copyright>

using Azure;
using Azure.AI.Translation.Document;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

/**
FILE: SampleCheckDocumentStatuses.cs
DESCRIPTION:
This sample demonstrates how to create a translation job and then monitor each document's status
and progress within the job.
USAGE:
Set the environment variables with your own values before running the sample:
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents
to be translated.
4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents
will be written.
**/

namespace DocumentTranslatorSamples
{
class SampleCheckDocumentStatuses
FadyEssam marked this conversation as resolved.
Show resolved Hide resolved
{
public async static Task RunSampleAsync()
{
var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_KEY");
var sourceContainerUrl = new Uri(Environment.GetEnvironmentVariable("AZURE_SOURCE_CONTAINER_URL"));
var targetContainerUrl = new Uri(Environment.GetEnvironmentVariable("AZURE_TARGET_CONTAINER_URL"));

var documentTranslationClient = new DocumentTranslationClient(endpoint, new AzureKeyCredential(key));

var jobRequest = new List<DocumentTranslationInput>()
{
new DocumentTranslationInput(
source: new TranslationSource(sourceContainerUrl),
targets: new List<TranslationTarget>()
{
new TranslationTarget(targetUri: targetContainerUrl, languageCode: "es")
}
)
};

// Submit the translation job request
var jobResult = await documentTranslationClient.StartTranslationAsync(jobRequest).ConfigureAwait(false);

HashSet<string> completedDocuments = new HashSet<string>();

while (!jobResult.HasCompleted)
{
await Task.Delay(TimeSpan.FromSeconds(1));
await jobResult.UpdateStatusAsync().ConfigureAwait(false);
await foreach (var document in jobResult.GetAllDocumentStatusesAsync())
{
if (!completedDocuments.Contains(document.DocumentId))
{
if (document.Status == TranslationStatus.Succeeded)
{
Console.WriteLine($"Document at {document.SourceDocumentUri} was translated to {document.TranslateTo} language.You can find translated document at {document.TranslatedDocumentUri}");
completedDocuments.Add(document.DocumentId);
}
else if (document.Status == TranslationStatus.Failed)
{
Console.WriteLine($"Document ID: {document.DocumentId}, Error Code: {document.Error.ErrorCode}, Message: {document.Error.Message}");
completedDocuments.Add(document.DocumentId);
}
else if (document.Status == TranslationStatus.Running)
{
Console.WriteLine($"Document ID: {document.DocumentId}, translation progress is {document.TranslationProgressPercentage} percent");
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// <copyright file="SampleCreateTranslationJob.cs" company="Microsoft Corporation">
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// </copyright>

using Azure;
using Azure.AI.Translation.Document;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

/**
FILE: SampleCreateTranslationJob.cs
DESCRIPTION:
This sample demonstrates how to create a translation job for documents in your Azure Blob
Storage container and wait until the job is completed.
USAGE:
Set the environment variables with your own values before running the sample:
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
3) AZURE_SOURCE_CONTAINER_URL - the container SAS URL to your source container which has the documents
to be translated.
4) AZURE_TARGET_CONTAINER_URL - the container SAS URL to your target container where the translated documents
will be written.
**/

namespace DocumentTranslatorSamples
{
class SampleCreateTranslationJob
FadyEssam marked this conversation as resolved.
Show resolved Hide resolved
{
public async static Task RunSampleAsync()
{
var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_KEY");
var sourceContainerUrl = new Uri(Environment.GetEnvironmentVariable("AZURE_SOURCE_CONTAINER_URL"));
var targetContainerUrl = new Uri(Environment.GetEnvironmentVariable("AZURE_TARGET_CONTAINER_URL"));

var documentTranslationClient = new DocumentTranslationClient(endpoint, new AzureKeyCredential(key));

var jobRequest = new List<DocumentTranslationInput>()
{
new DocumentTranslationInput(
source: new TranslationSource(sourceContainerUrl),
targets: new List<TranslationTarget>()
{
new TranslationTarget(targetUri: targetContainerUrl, languageCode: "es")
}
)
};

// Submit the translation job and wait for it to finish
var jobResult = await documentTranslationClient.StartTranslationAsync(jobRequest).ConfigureAwait(false);
await jobResult.WaitForCompletionAsync().ConfigureAwait(false);

Console.WriteLine($"Job status: {jobResult.Status}");
Console.WriteLine($"Job created on: {jobResult.CreatedOn}");
Console.WriteLine($"Job last updated on: {jobResult.LastModified}");
Console.WriteLine($"Total number of translations on documents: {jobResult.DocumentsTotal}");
Console.WriteLine("\nOf total documents...");
Console.WriteLine($"{jobResult.DocumentsFailed} failed");
Console.WriteLine($"{jobResult.DocumentsSucceeded} succeeded");

await foreach (var document in jobResult.GetAllDocumentStatusesAsync())
{
if (document.Status == TranslationStatus.Succeeded)
{
Console.WriteLine($"Document at {document.SourceDocumentUri} was translated to {document.TranslateTo} language.You can find translated document at {document.TranslatedDocumentUri}");
}
else
{
Console.WriteLine($"Document ID: {document.DocumentId}, Error Code: {document.Error.ErrorCode}, Message: {document.Error.Message}");
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <copyright file="SampleListAllSubmittedJobs.cs" company="Microsoft Corporation">
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// </copyright>

using Azure;
using Azure.AI.Translation.Document;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

/**
FILE: SampleListAllSubmittedJobs.cs
DESCRIPTION:
This sample demonstrates how to list all the submitted translation jobs for the resource and
wait until done on any jobs that are still running.
USAGE:
Set the environment variables with your own values before running the sample:
1) AZURE_DOCUMENT_TRANSLATION_ENDPOINT - the endpoint to your Document Translation resource.
2) AZURE_DOCUMENT_TRANSLATION_KEY - your Document Translation API key.
**/

namespace DocumentTranslatorSamples
{
class SampleListAllSubmittedJobs
{
public async static Task RunSampleAsync()
FadyEssam marked this conversation as resolved.
Show resolved Hide resolved
{
var endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("AZURE_DOCUMENT_TRANSLATION_KEY");

var documentTranslationClient = new DocumentTranslationClient(endpoint, new AzureKeyCredential(key));

await foreach (var jobStatus in documentTranslationClient.GetTranslationsAsync())
{
if (jobStatus.Status == TranslationStatus.Running)
{
DocumentTranslationOperation operation = new DocumentTranslationOperation(jobStatus.TranslationId, documentTranslationClient);
await operation.WaitForCompletionAsync();
}

Console.WriteLine($"Job ID: {jobStatus.TranslationId}");
Console.WriteLine($"Job status: {jobStatus.Status}");
Console.WriteLine($"Job created on: {jobStatus.CreatedOn}");
Console.WriteLine($"Job last updated on: {jobStatus.LastModified}");
Console.WriteLine($"Total number of translations on documents: {jobStatus.DocumentsTotal}");
Console.WriteLine($"Total number of characters charged: {jobStatus.TotalCharactersCharged}");
Console.WriteLine("\nOf total documents...");
Console.WriteLine($"{jobStatus.DocumentsFailed} failed");
Console.WriteLine($"{jobStatus.DocumentsSucceeded} succeeded");
Console.WriteLine($"{jobStatus.DocumentsCancelled} cancelled");
}
}
}
}
Loading