英文:
python: make websocket connection to elevenlabs with websocket-client
问题
在 elevenlabs 中,您现在可以使用流功能,同时还可以使用输入流。但是,必须建立套接字连接。
不幸的是,我必须自己实现 WebSocket 连接,而不是使用 elevenlabs 的 Python 客户端,因为我必须使用 WebSocket 客户端库,并且 elevenlabs 客户端使用不同的 WebSocket 库。
问题在于以下代码:
data = json.loads(ws.recv())
它会永远阻塞,并且似乎永远不会收到任何数据。我做错了什么?
以下是我的代码(BOS 和 EOS 与 elevenlabs Python 客户端文档中的代码完全相同,其余部分几乎也是相同的代码):
with closing(create_connection(url, header={"xi-api-key": "mykey"})) as ws:
# 发送流的开始
ws.send(BOS)
# 流式传输文本块并接收音频
for text_chunk in text_chunker(text):
data = dict(text=text_chunk, try_trigger_generation=True)
ws.send(json.dumps(data))
try:
data = json.loads(ws.recv()) # 在这里阻塞,永远无法继续
if data["audio"]:
yield base64.b64decode(data["audio"])
except TimeoutError:
pass
# 发送流的结束
ws.send(EOS)
# 接收剩余的音频
while True:
try:
data = json.loads(ws.recv())
if data["audio"]:
yield base64.b64decode(data["audio"])
except ws.exceptions.ConnectionClosed:
break
使用 elevenlabs 客户端的原始代码,它使用了 websockets.sync.client 库,可以按预期工作。
英文:
In elevenlabs you can now use the stream feature also with input stream. However, a socket connection must be established for this.
Unfortunately I have to implement the websocket connection myself instead of using the elevenlabs python client, because i have to use the websocket-client library and the elevenlabs client is using a different websocket library.
The problem is, however, that
data = json.loads(ws.recv())
blocks forever and nothing ever seems to arrive. what did I do wrong?
here is my code(BOS and EOS are exactly the same as in the documentations code of elevenlabs python client and rest of it is also nearly the same code):
with closing(create_connection(url, header={"xi-api-key":"mykey")) as ws:
# Send beginning of stream
ws.send(BOS)
# Stream text chunks and receive audio
for text_chunk in text_chunker(text):
data = dict(text=text_chunk, try_trigger_generation=True)
ws.send(json.dumps(data))
try:
data = json.loads(ws.recv()) -> blocking here, never gets over it
if data["audio"]:
yield base64.b64decode(data["audio"]) # type: ignore
except TimeoutError:
pass
# Send end of stream
ws.send(EOS)
# Receive remaining audio
while True:
try:
data = json.loads(ws.recv())
if data["audio"]:
yield base64.b64decode(data["audio"]) # type: ignore
except ws.exceptions.ConnectionClosed:
break
with the original code from the elevenlabs client , which uses the websockets.sync.client library, it works as expected
答案1
得分: 0
使用WebSocketApp在一个线程中找到了解决方案。
英文:
found a solution by using WebSocketApp in a thread.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论