client_http.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of NVIDIA CORPORATION nor the names of its
  12. # contributors may be used to endorse or promote products derived
  13. # from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. import requests
  27. import soundfile as sf
  28. import json
  29. import numpy as np
  30. import argparse
  31. def get_args():
  32. parser = argparse.ArgumentParser(
  33. formatter_class=argparse.ArgumentDefaultsHelpFormatter
  34. )
  35. parser.add_argument(
  36. "--server-url",
  37. type=str,
  38. default="localhost:8000",
  39. help="Address of the server",
  40. )
  41. parser.add_argument(
  42. "--reference-audio",
  43. type=str,
  44. default="../../example/prompt_audio.wav",
  45. help="Path to a single audio file. It can't be specified at the same time with --manifest-dir",
  46. )
  47. parser.add_argument(
  48. "--reference-text",
  49. type=str,
  50. default="吃燕窝就选燕之屋,本节目由26年专注高品质燕窝的燕之屋冠名播出。豆奶牛奶换着喝,营养更均衡,本节目由豆本豆豆奶特约播出。",
  51. help="",
  52. )
  53. parser.add_argument(
  54. "--target-text",
  55. type=str,
  56. default="身临其境,换新体验。塑造开源语音合成新范式,让智能语音更自然。",
  57. help="",
  58. )
  59. parser.add_argument(
  60. "--model-name",
  61. type=str,
  62. default="spark_tts",
  63. choices=[
  64. "f5_tts",
  65. "spark_tts",
  66. "cosyvoice2"],
  67. help="triton model_repo module name to request",
  68. )
  69. parser.add_argument(
  70. "--output-audio",
  71. type=str,
  72. default="output.wav",
  73. help="Path to save the output audio",
  74. )
  75. return parser.parse_args()
  76. def prepare_request(
  77. waveform,
  78. reference_text,
  79. target_text,
  80. sample_rate=16000,
  81. padding_duration: int = None,
  82. audio_save_dir: str = "./",
  83. ):
  84. assert len(waveform.shape) == 1, "waveform should be 1D"
  85. lengths = np.array([[len(waveform)]], dtype=np.int32)
  86. if padding_duration:
  87. # padding to nearset 10 seconds
  88. samples = np.zeros(
  89. (
  90. 1,
  91. padding_duration
  92. * sample_rate
  93. * ((int(len(waveform) / sample_rate) // padding_duration) + 1),
  94. ),
  95. dtype=np.float32,
  96. )
  97. samples[0, : len(waveform)] = waveform
  98. else:
  99. samples = waveform
  100. samples = samples.reshape(1, -1).astype(np.float32)
  101. data = {
  102. "inputs": [
  103. {
  104. "name": "reference_wav",
  105. "shape": samples.shape,
  106. "datatype": "FP32",
  107. "data": samples.tolist()
  108. },
  109. {
  110. "name": "reference_wav_len",
  111. "shape": lengths.shape,
  112. "datatype": "INT32",
  113. "data": lengths.tolist(),
  114. },
  115. {
  116. "name": "reference_text",
  117. "shape": [1, 1],
  118. "datatype": "BYTES",
  119. "data": [reference_text]
  120. },
  121. {
  122. "name": "target_text",
  123. "shape": [1, 1],
  124. "datatype": "BYTES",
  125. "data": [target_text]
  126. }
  127. ]
  128. }
  129. return data
  130. if __name__ == "__main__":
  131. args = get_args()
  132. server_url = args.server_url
  133. if not server_url.startswith(("http://", "https://")):
  134. server_url = f"http://{server_url}"
  135. url = f"{server_url}/v2/models/{args.model_name}/infer"
  136. waveform, sr = sf.read(args.reference_audio)
  137. assert sr == 16000, "sample rate hardcoded in server"
  138. samples = np.array(waveform, dtype=np.float32)
  139. data = prepare_request(samples, args.reference_text, args.target_text)
  140. rsp = requests.post(
  141. url,
  142. headers={"Content-Type": "application/json"},
  143. json=data,
  144. verify=False,
  145. params={"request_id": '0'}
  146. )
  147. result = rsp.json()
  148. audio = result["outputs"][0]["data"]
  149. audio = np.array(audio, dtype=np.float32)
  150. if args.model_name == "spark_tts":
  151. sample_rate = 16000
  152. else:
  153. sample_rate = 24000
  154. sf.write(args.output_audio, audio, sample_rate, "PCM_16")