Document translate dosen't translate

Eduardo Gomez 3,416 Reputation points
2022-10-01T18:21:44.413+00:00

I have two containers, and I am setting up the SAAS as per documentation (https://learn.microsoft.com/en-us/azure/cognitive-services/translator/document-translation/client-sdks?tabs=csharp#translate-a-document-or-batch-files)

When I checked y containers, everything uploaded normally without any problem and to their respective containers (Source and Target)

My document is in English.

The problem is, that it doesn't translate my documents.

Transcription code

public static async Task TranslatorAsync(Uri sourceUrl, Uri TargetUrl, string language = "es")   
{  
        DocumentTranslationClient client = new(new Uri(Constants.ENDPOINT), new AzureKeyCredential(Constants.KEY));  

        var input = new DocumentTranslationInput(sourceUrl, TargetUrl, language);  

        DocumentTranslationOperation operation = await client.StartTranslationAsync(input);  

        await operation.WaitForCompletionAsync();  

        MessageBox.Show($"  Status: {operation.Status}");  
        MessageBox.Show($"  Created on: {operation.CreatedOn}");  
        MessageBox.Show($"  Last modified: {operation.LastModified}");  
        MessageBox.Show($"  Total documents: {operation.DocumentsTotal}");  
        MessageBox.Show($"    Succeeded: {operation.DocumentsSucceeded}");  
        MessageBox.Show($"    Failed: {operation.DocumentsFailed}"); // this is how I know that it failed  
        MessageBox.Show($"    In Progress: {operation.DocumentsInProgress}");  
        MessageBox.Show($"Not started: {operation.DocumentsNotStarted}");  
}  

Azure code for uploading to containers

public class AzureStorageService   
{  
    readonly string ConectionString = Constants.AZURE_STORAGE_CONNECTIONSTRING;  
    BlobContainerClient? ContainerClient;  

    public async Task<Uri> UploadToAzureBlobStorage(string FilePath)   
    {  
        ContainerClient = new BlobContainerClient(ConectionString, Constants.AZURE_CONTAINER_ORIGINAL_DOCUMENT);  

        Azure.Storage.Sas.BlobSasBuilder blobSasBuilder = new() {  
            BlobContainerName = Constants.AZURE_CONTAINER_ORIGINAL_DOCUMENT,  
            ExpiresOn = DateTime.MaxValue,//Let SAS token expire never.  
        };  

        blobSasBuilder.SetPermissions(Azure.Storage.Sas.BlobSasPermissions.Read | Azure.Storage.Sas.BlobSasPermissions.List);  
        var sasToken = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(Constants.AZUTE_STORAGE_ACCOUNT_NAME, Constants.AZUTE_STORAGE_ACCOUNT_KEY)).ToString();  

        var blob = ContainerClient.GetBlobClient(Path.GetFileName(FilePath));  
        await blob.UploadAsync(FilePath, true);  

        return new Uri($"{Constants.AZURE_UPLOAD_DOUMMENRTS}?{sasToken}");  
    }  

    public async Task<Uri> GetfFromdAzureBlobStorage(string FilePath)   
    {  
        ContainerClient = new BlobContainerClient(ConectionString, Constants.AZURE_CONTAINER_TRANSLATED_DOCUMENT);  

        Azure.Storage.Sas.BlobSasBuilder blobSasBuilder = new() {  
            BlobContainerName = Constants.AZURE_CONTAINER_TRANSLATED_DOCUMENT,  
            ExpiresOn = DateTime.MaxValue,//Let SAS token expire never.  
        };  

        blobSasBuilder.SetPermissions(Azure.Storage.Sas.BlobSasPermissions.Read | Azure.Storage.Sas.BlobSasPermissions.List);  
        var sasToken = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(Constants.AZUTE_STORAGE_ACCOUNT_NAME, Constants.AZUTE_STORAGE_ACCOUNT_KEY)).ToString();  

        var blob = ContainerClient.GetBlobClient(Path.GetFileName(FilePath));  
        await blob.UploadAsync(FilePath, true);  

        return new Uri($"{Constants.AZURE_DOWNLOAD_DOCUMENTS}?{sasToken}");  
    }  
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,415 questions
{count} votes

Accepted answer
  1. romungi-MSFT 42,311 Reputation points Microsoft Employee
    2022-10-06T07:48:51.497+00:00

    @Eduardo Gomez Apologies, I wasn't working yesterday. I got a chance to run your code today and found the issue for the error. From debugging the code, I found that you are setting the targetURI by calling SaveFromdAzureBlobStorage() but this method is actually uploading the original file to the translated container even before the translation request is called. Hence, the error "File already exists".

    All I had to do was comment out these lines from the method SaveFromdAzureBlobStorage() to translate a document successfully.

                //var blob = ContainerClient.GetBlobClient(Path.GetFileName(FilePath));  
                //await blob.UploadAsync(FilePath, true);  
    

    Once this is done the file I uploaded was successfully translated and uploaded. You can check the file Invoice_1.pdf in both your containers for reference.

    247870-image.png

    Since you have some additional files in the translated container the requests for these documents will still error out since I have not deleted these documents from your previous tests in the translated container. Please try to cleanup your containers and try again. I also think you can factor your code to only pass the uploaded file instead of translating all the documents in a container again whenever a user provides a document. If this is not changed every request will try to translate each and every document from original container and the documents that are already present in translated container will again error out.

    Content of my translated document in spanish.

    247970-image.png

    P.S: Please refresh your resource keys to ensure the same is not misused. Thanks!!

    If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.


0 additional answers

Sort by: Most helpful