python - Azure 认知服务文本转语音 REST API

标签 python azure flask azure-cognitive-services

我正在调用 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中的响应的屏幕截图 PostMan Directly to Azure

当我调用我的 API 来调用 azure API 时,Postman 图像 My API call which inturn calls Azure text to speech api

我不确定我错过了什么

最佳答案

我认为您应该使用 response.content 而不是 Response 对象本身来获取字节形式的响应正文。

同时,我建议您使用 Azure Speech SDK用于语音合成,它具有易于使用的界面。引用Python Quickstart for TTS了解更多详情。

关于python - Azure 认知服务文本转语音 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74609190/

相关文章:

Azure 应用程序仅访问 Key Vault key

python - 如何使用 Flask HTTP Auth 为 Web 服务实现 "Incorrect username/password"提示?

python - 按列对数据库行进行分组的有效方法?

sql-server - 从本地 SQL Server SSRS 连接到在线 SQL Azure DB

python - Matplotlib Gridspec "Cell"标签

azure - KQL 负载均衡器字节

python - 如何在 Flask 中传递 "WTF object"

python - virtualenv 重新启动时删除库( flask /蛋黄)

python - 在 Twisted 透视代理中调用客户端的远程方法

python - 为现有的 python 模块编写扩展