Search code examples
pythonazureazure-speech

How to use Batch Transcription API through Python


I have create an Azure Batch Transcription Service which will take audio file from Azure blob storage as input and share the Speech to Text with me.

I am able to achieve the above using the https://eastus.cris.ai/swagger/ui/index#/ website, the curl command which ran successfully was:

curl -X POST "https://eastus.cris.ai/api/speechtotext/v2.0/transcriptions" -H "accept: application/json" -H "Ocp-Apim-Subscription-Key: <my subscription key>" -H "Authorization: <my access token>" -H "Content-Type: application/json" -d "{ \"recordingsUrl\": \"<my azure blob audio file url>\", \"models\": [], \"locale\": \"en-US\", \"name\": \"<Some Name>\", \"description\": \"<Some Description>\", \"properties\": { \"ProfanityFilterMode\": \"Masked\", \"PunctuationMode\": \"DictatedAndAutomatic\" }}"

However if I try to achieve the same programmatically through Python, I get the error as below:

Response [400] {"code":"InvalidPayload","message":"No valid object could be found."}

Here is my code:

import requests
url="https://eastus.cris.ai/api/speechtotext/v2.0/transcriptions"

headers={'accept' : 'application/json','Ocp-Apim-Subscription-Key  ':'<my subscription key', 'Authorization' : '<my access token>','Content-Type': 'application/json'}

data={'recordingsUrl' : '<my azure blob audio file url>' ,"models": [], 'locale' : 'en-US', 'name' : '<Some Name>','description' : '<Some Description>','properties' : {'ProfanityFilterMode': 'Masked','PunctuationMode': 'DictatedAndAutomatic'}}

r =requests.post(url,headers=headers,data=data, verify=False)
print(r)
print(r.text)

Let me know if there is some mistake I am making in requests.post


Solution

  • This API requires payloads on JSON Type.

    Your code is not sending serialized JSON type.

    So server response messages means mismatched in payloads type.

    It just sending payloads with python dictionary type, not JSON.

    It needed to convert data payloads to serialized JSON Format.

    1.Import json modules.

    import requests, json
    

    2.Change data payloads to json with json.dumps(data).

    r =requests.post(url,headers=headers,data=json.dumps(data), verify=False)