I am trying to use Azure speech to text api, through python. Below is the code.
import http.client, urllib.request, urllib.parse, urllib.error, base64
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '<my_subscription_key>',
}
params = urllib.parse.urlencode({
"contentUrls": [
"<URI_TO_MY_AUDIO_FILE>",
],
"properties": {
},
"locale": "en-US",
"displayName": "Transcription"
})
try:
conn = http.client.HTTPSConnection('eastus.api.cognitive.microsoft.com')
conn.request("POST", "/speechtotext/v3.0/transcriptions?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
The error I am getting is:
b'{\r\n "code": "InvalidPayload",\r\n "message": "Invalid JavaScript property identifier character: }. Path \'\', line 1, position 5."\r\n}'
I believe I am setting my parameters wrong. Unable to find what's wrong.
Python version 3.7
Strangely the same HTTP request is successful when done via https://eastus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/CreateTranscription
CodePudding user response:
Your POST request body is not supplied in the right format.
Please see the corrected code below:
import http.client
import json
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '<my_subscription_key>',
}
body = {
"contentUrls": [
"<URI_TO_MY_AUDIO_FILE>",
],
"properties": {
},
"locale": "en-US",
"displayName": "Transcription"
}
try:
conn = http.client.HTTPSConnection('eastus.api.cognitive.microsoft.com')
conn.request("POST", "/speechtotext/v3.0/transcriptions", body=json.dumps(body), headers=headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
Thanks,
Sen
CodePudding user response:
I'd recommend switching to using the Python API rather than the REST API; https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-speech-to-text?tabs=windowsinstall&pivots=programming-language-python
That looks like a weird error that could even be being caused by something in your audio file, so swapping to the Python API would probably give you a more useful error message.