gpt4 book ai didi

python - Azure 认知服务 - 使用 python 和 websockets 自定义语音

转载 作者:行者123 更新时间:2023-12-02 07:28:11 27 4
gpt4 key购买 nike

我正在 Python 中使用 Microsoft 自定义语音服务。目前仅支持 HTTP 端点。根据文档,还支持 websocket。

有人有通过 websocket 发送数据的示例吗?到目前为止,我已经开始使用我的 token 打开 websocket 来提供服务。但是当我开始发送数据时,连接关闭并出现错误 104。

详细信息:-Python3- websocket 客户端- 带有 RIFF header 的 wav(适用于 HTTP)

谢谢!

代码示例:

# pip install websocket-client
def main_websocket_cris():
path_root = os.path.abspath(os.path.dirname(__file__))
filename = os.path.join(path_root, 'example_011.wav')
chunk_size = 8192

key = '<mykey>'
url = 'wss://<mydeployment>.api.cris.ai/ws/cris/speech/recognize'
token = auth_cris(key)

header = ['Authorization: Bearer %s' % token]
ws = websocket.create_connection(url, header=header)
try:
print('--- send ping')
ws.ping()
print('> ping done')

print('--- send pong')
ws.pong(b'')
print('> pong done')

print('--- status and headers')
print('> status:', ws.getstatus())
print('> headers:', ws.getheaders())
print('> status done')

headers = ['Path: audio',
'X-RequestId: %s' % str(uuid.uuid4()).replace('-', ''),
'X-Timestamp: %s' % datetime.datetime.now().isoformat(),
'Content-Type: audio/x-wav']
headers = {
'Path': 'audio',
'X-RequestId': str(uuid.uuid4()).replace('-', ''),
'X-Timestamp': str(datetime.datetime.now().isoformat()),
'Content-Type': 'audio/x-wav'
}
print(headers)
#ws.send(json.dumps(headers))

print('--- send binary data')
print('> read file in chunks of %s bytes' % chunk_size)
with open(filename, 'rb') as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
ws.send(json.dumps(headers))
ws.send_binary(chunk)
print("> Sent")
print('--- now receive answer')
print("> Receiving...")
result = ws.recv()
print("> Received '%s'" % result)
finally:
print('--- close')
ws.close()
print('> closed')

最佳答案

我建议使用语音协议(protocol)端点(例如“wss://YOUR_DEPLOYMENT.api.cris.ai/speech/recognition/interactive/cognitiveservices/v1”)。

这些端点使用的协议(protocol)记录在此处:Microsoft Speech WebSocket Protocol 。您可以找到协议(protocol) here 的 JavaScript 实现.

关于python - Azure 认知服务 - 使用 python 和 websockets 自定义语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com