client.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (c) 2024 Alibaba Inc (authors: Xiang Lyu)
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
  17. sys.path.append('{}/../..'.format(ROOT_DIR))
  18. sys.path.append('{}/../../third_party/Matcha-TTS'.format(ROOT_DIR))
  19. import logging
  20. import argparse
  21. import torchaudio
  22. import cosyvoice_pb2
  23. import cosyvoice_pb2_grpc
  24. import grpc
  25. import torch
  26. import numpy as np
  27. from cosyvoice.utils.file_utils import load_wav
  28. def main():
  29. with grpc.insecure_channel("{}:{}".format(args.host, args.port)) as channel:
  30. stub = cosyvoice_pb2_grpc.CosyVoiceStub(channel)
  31. request = cosyvoice_pb2.Request()
  32. if args.mode == 'sft':
  33. logging.info('send sft request')
  34. sft_request = cosyvoice_pb2.sftRequest()
  35. sft_request.spk_id = args.spk_id
  36. sft_request.tts_text = args.tts_text
  37. request.sft_request.CopyFrom(sft_request)
  38. elif args.mode == 'zero_shot':
  39. logging.info('send zero_shot request')
  40. zero_shot_request = cosyvoice_pb2.zeroshotRequest()
  41. zero_shot_request.tts_text = args.tts_text
  42. zero_shot_request.prompt_text = args.prompt_text
  43. prompt_speech = load_wav(args.prompt_wav, 16000)
  44. zero_shot_request.prompt_audio = (prompt_speech.numpy() * (2**15)).astype(np.int16).tobytes()
  45. request.zero_shot_request.CopyFrom(zero_shot_request)
  46. elif args.mode == 'cross_lingual':
  47. logging.info('send cross_lingual request')
  48. cross_lingual_request = cosyvoice_pb2.crosslingualRequest()
  49. cross_lingual_request.tts_text = args.tts_text
  50. prompt_speech = load_wav(args.prompt_wav, 16000)
  51. cross_lingual_request.prompt_audio = (prompt_speech.numpy() * (2**15)).astype(np.int16).tobytes()
  52. request.cross_lingual_request.CopyFrom(cross_lingual_request)
  53. else:
  54. logging.info('send instruct request')
  55. instruct_request = cosyvoice_pb2.instructRequest()
  56. instruct_request.tts_text = args.tts_text
  57. instruct_request.spk_id = args.spk_id
  58. instruct_request.instruct_text = args.instruct_text
  59. request.instruct_request.CopyFrom(instruct_request)
  60. response = stub.Inference(request)
  61. logging.info('save response to {}'.format(args.tts_wav))
  62. tts_speech = torch.from_numpy(np.array(np.frombuffer(response.tts_audio, dtype=np.int16))).unsqueeze(dim=0)
  63. torchaudio.save(args.tts_wav, tts_speech, target_sr)
  64. logging.info('get response')
  65. if __name__ == "__main__":
  66. parser = argparse.ArgumentParser()
  67. parser.add_argument('--host',
  68. type=str,
  69. default='0.0.0.0')
  70. parser.add_argument('--port',
  71. type=int,
  72. default='50000')
  73. parser.add_argument('--mode',
  74. default='sft',
  75. choices=['sft', 'zero_shot', 'cross_lingual', 'instruct'],
  76. help='request mode')
  77. parser.add_argument('--tts_text',
  78. type=str,
  79. default='你好,我是通义千问语音合成大模型,请问有什么可以帮您的吗?')
  80. parser.add_argument('--spk_id',
  81. type=str,
  82. default='中文女')
  83. parser.add_argument('--prompt_text',
  84. type=str,
  85. default='希望你以后能够做的比我还好呦。')
  86. parser.add_argument('--prompt_wav',
  87. type=str,
  88. default='../../zero_shot_prompt.wav')
  89. parser.add_argument('--instruct_text',
  90. type=str,
  91. default='Theo \'Crimson\', is a fiery, passionate rebel leader. Fights with fervor for justice, but struggles with impulsiveness.')
  92. parser.add_argument('--tts_wav',
  93. type=str,
  94. default='demo.wav')
  95. args = parser.parse_args()
  96. prompt_sr, target_sr = 16000, 22050
  97. main()