“`python 使用 websocket-client 建立与 elevenlabs 的 WebSocket 连接 “`

huangapple go评论147阅读模式
英文:

python: make websocket connection to elevenlabs with websocket-client

问题

在 elevenlabs 中,您现在可以使用流功能,同时还可以使用输入流。但是,必须建立套接字连接。

不幸的是,我必须自己实现 WebSocket 连接,而不是使用 elevenlabs 的 Python 客户端,因为我必须使用 WebSocket 客户端库,并且 elevenlabs 客户端使用不同的 WebSocket 库。

问题在于以下代码:

  1. data = json.loads(ws.recv())

它会永远阻塞,并且似乎永远不会收到任何数据。我做错了什么?

以下是我的代码(BOS 和 EOS 与 elevenlabs Python 客户端文档中的代码完全相同,其余部分几乎也是相同的代码):

  1. with closing(create_connection(url, header={"xi-api-key": "mykey"})) as ws:
  2. # 发送流的开始
  3. ws.send(BOS)
  4. # 流式传输文本块并接收音频
  5. for text_chunk in text_chunker(text):
  6. data = dict(text=text_chunk, try_trigger_generation=True)
  7. ws.send(json.dumps(data))
  8. try:
  9. data = json.loads(ws.recv()) # 在这里阻塞,永远无法继续
  10. if data["audio"]:
  11. yield base64.b64decode(data["audio"])
  12. except TimeoutError:
  13. pass
  14. # 发送流的结束
  15. ws.send(EOS)
  16. # 接收剩余的音频
  17. while True:
  18. try:
  19. data = json.loads(ws.recv())
  20. if data["audio"]:
  21. yield base64.b64decode(data["audio"])
  22. except ws.exceptions.ConnectionClosed:
  23. 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):

  1. with closing(create_connection(url, header={"xi-api-key":"mykey")) as ws:
  2. # Send beginning of stream
  3. ws.send(BOS)
  4. # Stream text chunks and receive audio
  5. for text_chunk in text_chunker(text):
  6. data = dict(text=text_chunk, try_trigger_generation=True)
  7. ws.send(json.dumps(data))
  8. try:
  9. data = json.loads(ws.recv()) -> blocking here, never gets over it
  10. if data["audio"]:
  11. yield base64.b64decode(data["audio"]) # type: ignore
  12. except TimeoutError:
  13. pass
  14. # Send end of stream
  15. ws.send(EOS)
  16. # Receive remaining audio
  17. while True:
  18. try:
  19. data = json.loads(ws.recv())
  20. if data["audio"]:
  21. yield base64.b64decode(data["audio"]) # type: ignore
  22. except ws.exceptions.ConnectionClosed:
  23. 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.

huangapple
  • 本文由 发表于 2023年8月10日 23:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定