我正在调用 Azure 文本到语音 REST API 以从我的 Flask API 获取音频响应。 当我从 postman 调用 Azure REST API 时,我得到音频文件形式的输出,并且可以播放我以文本形式给出的内容,但是当我从 flask 调用 API 时,我得到空视频文件而不是音频文件
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")
我遇到以下错误
<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'
下面是postman直接向Azure API发出请求及其在postman中的响应的屏幕截图
当我调用我的 API 来调用 azure API 时,Postman 图像
我不确定我错过了什么
最佳答案
我认为您应该使用 response.content
而不是 Response
对象本身来获取字节形式的响应正文。
同时,我建议您使用 Azure Speech SDK用于语音合成,它具有易于使用的界面。引用Python Quickstart for TTS了解更多详情。
关于python - Azure 认知服务文本转语音 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74609190/