msgpack_api.py 964 B

12345678910111213141516171819202122232425262728293031323334
  1. import httpx
  2. import ormsgpack
  3. from tools.commons import ServeReferenceAudio, ServeTTSRequest
  4. # priority: ref_id > references
  5. request = ServeTTSRequest(
  6. text="你说的对, 但是原神是一款由米哈游自主研发的开放世界手游.",
  7. # reference_id="114514",
  8. references=[
  9. ServeReferenceAudio(
  10. audio=open("lengyue.wav", "rb").read(),
  11. text=open("lengyue.lab", "r", encoding="utf-8").read(),
  12. )
  13. ],
  14. streaming=True,
  15. )
  16. with (
  17. httpx.Client() as client,
  18. open("hello.wav", "wb") as f,
  19. ):
  20. with client.stream(
  21. "POST",
  22. "http://127.0.0.1:8080/v1/tts",
  23. content=ormsgpack.packb(request, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
  24. headers={
  25. "authorization": "Bearer YOUR_API_KEY",
  26. "content-type": "application/msgpack",
  27. },
  28. timeout=None,
  29. ) as response:
  30. for chunk in response.iter_bytes():
  31. f.write(chunk)