Cannot translate a file with azure translator, try to upload on blob storage but "File not Found" error

Dipankar Bhattacharyya 0 Reputation points
2023-12-08T07:28:15.2666667+00:00
def sample_translation():
    import os
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.translation.document import DocumentTranslationClient

    endpoint = "https://api.cognitive.microsofttranslator.com"
    key =  "mykey"
    source_container_url = "https://dipblob2.blob.core.windows.net/conta?sp=rl&st=2023-12-08T05:34:40Z&se=2023-12-08T13:34:40Z&skoid=966b96e4-df8b-4ffd-ba38-0ebdd0ecb0b5&sktid=d3e8a7d7-a29e-4fb3-9348-84d3eb3899d3&skt=2023-12-08T05:34:40Z&ske=2023-12-08T13:34:40Z&sks=b&skv=2022-11-02&sv=2022-11-02&sr=c&sig=cj7DFR%2BamhjN2NnwuupxMo6TMXO3wt4Xwy1ZWsQvpSA%3D"
    target_container_url = "https://dipblob2.blob.core.windows.net/contb?sp=wl&st=2023-12-08T05:36:40Z&se=2023-12-08T13:36:40Z&skoid=966b96e4-df8b-4ffd-ba38-0ebdd0ecb0b5&sktid=d3e8a7d7-a29e-4fb3-9348-84d3eb3899d3&skt=2023-12-08T05:36:40Z&ske=2023-12-08T13:36:40Z&sks=b&skv=2022-11-02&sv=2022-11-02&sr=c&sig=sm7XESS4lGPqf%2BDl%2FajUXC8VijHyNsEzdwIy3Pgez4g%3D"
    client = DocumentTranslationClient(endpoint, AzureKeyCredential(key))

    poller = client.begin_translation(source_container_url, target_container_url, "fr")
    result = poller.result()

    print(f"Status: {poller.status()}")
    print(f"Created on: {poller.details.created_on}")
    print(f"Last updated on: {poller.details.last_updated_on}")
    print(f"Total number of translations on documents: {poller.details.documents_total_count}")

    print("\nOf total documents...")
    print(f"{poller.details.documents_failed_count} failed")
    print(f"{poller.details.documents_succeeded_count} succeeded")

    for document in result:
        print(f"Document ID: {document.id}")
        print(f"Document status: {document.status}")
        if document.status == "Succeeded":
            print(f"Source document location: {document.source_document_url}")
            print(f"Translated document location: {document.translated_document_url}")
            print(f"Translated to language: {document.translated_to}\n")
        elif document.error:
            print(f"Error Code: {document.error.code}, Message: {document.error.message}\n")
    # [END begin_translation]


if __name__ == '__main__':
    sample_translation()

failing at this line
poller = client.begin_translation(source_container_url, target_container_url, "fr")

but I can read the blob container files from my mac using BlobServiceClient.

Requirement: Translate a pdf/txt file and output a file, if without blob storage then even better. Thanks in advance
Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
363 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Dipankar Bhattacharyya 0 Reputation points
    2023-12-08T16:58:27.1466667+00:00

    It that case I will loose the pdf formatting, may be then uploading to blob container is my only option. Why I am getting this File not found error, any suggestion?

    0 comments No comments

  2. Dipankar Bhattacharyya 0 Reputation points
    2023-12-08T17:02:20.3766667+00:00

    getting this error

    Traceback (most recent call last):
      File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py", line 1496, in _exec
        pydev_imports.execfile(file, globals, locals)  # execute the script
      File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
        exec(compile(contents+"\n", file, 'exec'), glob, loc)
      File "/Users/dipankarbhattacharyya/myKognitos/cases/translate_language/sample_begin_translation.py", line 37, in <module>
        sample_translation()
      File "/Users/dipankarbhattacharyya/myKognitos/cases/translate_language/sample_begin_translation.py", line 12, in sample_translation
        poller = client.begin_translation(source_container_url, target_container_url, "fr")
      File "/Users/dipankarbhattacharyya/kognitos/brain/venv/lib/python3.9/site-packages/azure/core/tracing/decorator.py", line 76, in wrapper_use_tracer
        return func(*args, **kwargs)
      File "/Users/dipankarbhattacharyya/kognitos/brain/venv/lib/python3.9/site-packages/azure/ai/translation/document/_client.py", line 277, in begin_translation
        self._client.document_translation.begin_start_translation(
      File "/Users/dipankarbhattacharyya/kognitos/brain/venv/lib/python3.9/site-packages/azure/ai/translation/document/_generated/operations/_document_translation_operations.py", line 149, in begin_start_translation
        raw_result = self._start_translation_initial(
      File "/Users/dipankarbhattacharyya/kognitos/brain/venv/lib/python3.9/site-packages/azure/ai/translation/document/_generated/operations/_document_translation_operations.py", line 95, in _start_translation_initial
        map_error(status_code=response.status_code, response=response, error_map=error_map)
      File "/Users/dipankarbhattacharyya/kognitos/brain/venv/lib/python3.9/site-packages/azure/core/exceptions.py", line 109, in map_error
        raise error
    azure.core.exceptions.ResourceNotFoundError: Operation returned an invalid status 'Not Found'
    Content: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>404 - File or directory not found.</title>
    <style type="text/css">
    <!--
    body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
    fieldset{padding:0 15px 10px 15px;} 
    h1{font-size:2.4em;margin:0;color:#FFF;}
    h2{font-size:1.7em;margin:0;color:#CC0000;} 
    h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
    #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
    background-color:#555555;}
    #content{margin:0 0 0 2%;position:relative;}
    .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
    -->
    </style>
    </head>
    <body>
    <div id="header"><h1>Server Error</h1></div>
    <div id="content">
     <div class="content-container"><fieldset>
      <h2>404 - File or directory not found.</h2>
      <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
     </fieldset></div>
    </div>
    </body>
    </html>
    
    Process finished with exit code 1
    
    
    0 comments No comments