I am calling a Azure Text to Speech REST API to get audio response from my flask API. When I call the Azure REST API from postman I get the output as audio file and can play what I have given as text but when I call the API from my flask I get empty video file instead of audio file
def call_azure_cognitive_api(text):
token = get_token()
cognitive_service_url = 'https://eastus.tts.speech.microsoft.com/cognitiveservices/v1'
headers = {
'Authorization': 'Bearer %s' %token,
'X-Microsoft-OutputFormat': 'audio-16khz-32kbitrate-mono-mp3',
'Content-Type':'application/ssml+xml'
}
data = """<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Male' name='en-US-ChristopherNeural'> Microsoft Speech Service Text-to-Speech API </voice></speak>"""
response = requests.post(cognitive_service_url,data=data,headers=headers)
print(response)
return send_file(response, mimetype="audio/mpeg", download_name="ajinkya.mp3")
I am getting below error
<Response [200]>
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/werkzeug/wsgi.py", line 576, in __next__
data = self.file.read(self.buffer_size)
AttributeError: 'Response' object has no attribute 'read'
Below is the screenshot of postman request to Azure API directly and its response in postman
Postman image when I call my API to call azure API
I am not sure what I am missing
I think you should use response.content
instead of the Response
object itself to get the response body as bytes.
Meanwhile, I recommened you to use the Azure Speech SDK for speech synthesis, which has an easy-to-use interface. Refer to the Python Quickstart for TTS for more details.