msgpack_api.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import httpx
  2. import ormsgpack
  3. from tools.commons import ServeReferenceAudio, ServeTTSRequest
  4. def audio_request():
  5. # priority: ref_id > references
  6. request = ServeTTSRequest(
  7. text="你说的对, 但是原神是一款由米哈游自主研发的开放世界手游.",
  8. # reference_id="114514",
  9. references=[
  10. ServeReferenceAudio(
  11. audio=open("lengyue.wav", "rb").read(),
  12. text=open("lengyue.lab", "r", encoding="utf-8").read(),
  13. )
  14. ],
  15. streaming=True,
  16. )
  17. with (
  18. httpx.Client() as client,
  19. open("hello.wav", "wb") as f,
  20. ):
  21. with client.stream(
  22. "POST",
  23. "http://127.0.0.1:8080/v1/tts",
  24. content=ormsgpack.packb(request, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
  25. headers={
  26. "authorization": "Bearer YOUR_API_KEY",
  27. "content-type": "application/msgpack",
  28. },
  29. timeout=None,
  30. ) as response:
  31. for chunk in response.iter_bytes():
  32. f.write(chunk)
  33. def asr_request():
  34. # Read the audio file
  35. with open(
  36. r"D:\PythonProject\fish-speech\.cache\test_audios\prompts\2648200402409733590.wav",
  37. "rb",
  38. ) as audio_file:
  39. audio_data = audio_file.read()
  40. # Prepare the request data
  41. request_data = {
  42. "audio": audio_data,
  43. "language": "en", # Optional: specify the language
  44. "ignore_timestamps": False, # Optional: set to True to ignore precise timestamps
  45. }
  46. # Send the request
  47. with httpx.Client() as client:
  48. response = client.post(
  49. "https://api.fish.audio/v1/asr",
  50. headers={
  51. "Authorization": "Bearer 8eda4aeed2bc4aec9489b3efad003799",
  52. "Content-Type": "application/msgpack",
  53. },
  54. content=ormsgpack.packb(request_data),
  55. )
  56. # Parse the response
  57. result = response.json()
  58. print(f"Transcribed text: {result['text']}")
  59. print(f"Audio duration: {result['duration']} seconds")
  60. for segment in result["segments"]:
  61. print(f"Segment: {segment['text']}")
  62. print(f"Start time: {segment['start']}, End time: {segment['end']}")
  63. if __name__ == "__main__":
  64. asr_request()