在 111labs 中,您现在还可以将流功能与输入流一起使用。但是,必须为此建立套接字连接。
不幸的是,我必须自己实现websocket连接,而不是使用elevenlabs python客户端,因为我必须使用websocket-client库,而elevenlabs客户端正在使用不同的websocket库。
然而问题是
data = json.loads(ws.recv())
永远阻塞,似乎什么也没有到达。我做错了什么?
这是我的代码(BOS和EOS与elevenlabs python客户端的文档代码完全相同,其余部分也几乎相同):
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
使用十一实验室客户端的原始代码(使用 websockets.sync.client 库),它可以按预期工作
通过在线程中使用 WebSocketApp 找到了解决方案。