Search code examples
azurerestazure-openai

Azure Translate API is working in postman but not in VS Code


I'm testing out the azure translate REST API and trying to use the functionality to translate just a single document, instead of the entire container contents.

When I make the call via Postman, using the following headers and JSON body, I get the status 202, and it sucessfully creates the document in the desired output container:

enter image description here enter image description here

I then tried to perform the same operation using the python in Visual Studio Code to make the call, using the template code from the Microsoft Docs:

import requests

endpoint = 'https://<my-endpoint>.cognitiveservices.azure.com/'
key =  '<my-key>'
path = 'translator/text/batch/v1.1/batches'
constructed_url = endpoint + path

sourceSASUrl = "https://<my-blob>.blob.core.windows.net/input/christmasemailcopy.docx?sp=rwl&st=2023-12-11T11:39:20Z&se=2023-12-11T19:39:20Z&sv=2022-11-02&sr=c&sig=<sas-token-here>"

targetSASUrl = "https://<my-blob>.blob.core.windows.net/output/christmasemailcopyes3.docx?sp=rcwl&st=2023-12-11T11:40:52Z&se=2023-12-11T19:40:52Z&sv=2022-11-02&sr=c&sig=<sas-token-here>"

body= {
    "inputs": [
        {
            "source": {
                "sourceUrl": sourceSASUrl,
                "language": "en"
            },
            "targets": [
                {
                    "targetUrl": targetSASUrl,
                    "language": "es"
                }
            ]
        }
    ]
}
headers = {
  'Ocp-Apim-Subscription-Key': key,
  'Content-Type': 'application/json',
  'ocp-Apim-subscription-reigon' : 'northeurope'
}

response = requests.post(constructed_url, headers=headers, json=body)
response_headers = response.headers

print(f'response status code: {response.status_code}\nresponse status: {response.reason}\n\nresponse headers:\n')

for key, value in response_headers.items():
    print(key, ":", value)

This returns the same message as using Postman, status 202, accepted.

In theory, this should mean that the translated file is in the output blob container, which is it using the postman call, but there's no file when I run this in VS Code. Am I missing something really simple in terms of how I call it in python? Or is this a weird bug of the translate API that someone else has come across? Or is it just to do with making a POST call in Python?

Any guidance gratefully appreciated!


Solution

  • For anyone else facing this problem, it's that there's an error in the example code on the microsoft documents at time of writing (14/12/23) I have reported it.

    In the python version, the value "source" is in the wrong place. It should look like this.

    body = {
        "inputs": [
            {
                "storageType": "File",
                "source": {
                    "sourceUrl": sourceUrl,
                    "storageSource": "AzureBlob",
                    "language": "en"
                },
                "targets": [
                    {
                        "targetUrl": targetUrl,
                        "storageSource": "AzureBlob",
                        "category": "general",
                        "language": language
                    }
                ]
            }
        ]
    }