cosyvoice.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 time
  16. from hyperpyyaml import load_hyperpyyaml
  17. from modelscope import snapshot_download
  18. from cosyvoice.cli.frontend import CosyVoiceFrontEnd
  19. from cosyvoice.cli.model import CosyVoiceModel
  20. from cosyvoice.utils.file_utils import logging
  21. class CosyVoice:
  22. def __init__(self, model_dir, load_jit=True, load_trt=True, use_fp16=False):
  23. instruct = True if '-Instruct' in model_dir else False
  24. self.model_dir = model_dir
  25. if not os.path.exists(model_dir):
  26. model_dir = snapshot_download(model_dir)
  27. with open('{}/cosyvoice.yaml'.format(model_dir), 'r') as f:
  28. configs = load_hyperpyyaml(f)
  29. self.frontend = CosyVoiceFrontEnd(configs['get_tokenizer'],
  30. configs['feat_extractor'],
  31. '{}/campplus.onnx'.format(model_dir),
  32. '{}/speech_tokenizer_v1.onnx'.format(model_dir),
  33. '{}/spk2info.pt'.format(model_dir),
  34. instruct,
  35. configs['allowed_special'])
  36. self.model = CosyVoiceModel(configs['llm'], configs['flow'], configs['hift'])
  37. self.model.load('{}/llm.pt'.format(model_dir),
  38. '{}/flow.pt'.format(model_dir),
  39. '{}/hift.pt'.format(model_dir))
  40. if load_jit:
  41. self.model.load_jit('{}/llm.text_encoder.fp16.zip'.format(model_dir),
  42. '{}/llm.llm.fp16.zip'.format(model_dir))
  43. if load_trt:
  44. self.model.load_trt(model_dir, use_fp16)
  45. del configs
  46. def list_avaliable_spks(self):
  47. spks = list(self.frontend.spk2info.keys())
  48. return spks
  49. def inference_sft(self, tts_text, spk_id, stream=False):
  50. for i in self.frontend.text_normalize(tts_text, split=True):
  51. model_input = self.frontend.frontend_sft(i, spk_id)
  52. start_time = time.time()
  53. logging.info('synthesis text {}'.format(i))
  54. for model_output in self.model.inference(**model_input, stream=stream):
  55. speech_len = model_output['tts_speech'].shape[1] / 22050
  56. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  57. yield model_output
  58. start_time = time.time()
  59. def inference_zero_shot(self, tts_text, prompt_text, prompt_speech_16k, stream=False):
  60. prompt_text = self.frontend.text_normalize(prompt_text, split=False)
  61. for i in self.frontend.text_normalize(tts_text, split=True):
  62. model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_speech_16k)
  63. start_time = time.time()
  64. logging.info('synthesis text {}'.format(i))
  65. for model_output in self.model.inference(**model_input, stream=stream):
  66. speech_len = model_output['tts_speech'].shape[1] / 22050
  67. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  68. yield model_output
  69. start_time = time.time()
  70. def inference_cross_lingual(self, tts_text, prompt_speech_16k, stream=False):
  71. if self.frontend.instruct is True:
  72. raise ValueError('{} do not support cross_lingual inference'.format(self.model_dir))
  73. for i in self.frontend.text_normalize(tts_text, split=True):
  74. model_input = self.frontend.frontend_cross_lingual(i, prompt_speech_16k)
  75. start_time = time.time()
  76. logging.info('synthesis text {}'.format(i))
  77. for model_output in self.model.inference(**model_input, stream=stream):
  78. speech_len = model_output['tts_speech'].shape[1] / 22050
  79. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  80. yield model_output
  81. start_time = time.time()
  82. def inference_instruct(self, tts_text, spk_id, instruct_text, stream=False):
  83. if self.frontend.instruct is False:
  84. raise ValueError('{} do not support instruct inference'.format(self.model_dir))
  85. instruct_text = self.frontend.text_normalize(instruct_text, split=False)
  86. for i in self.frontend.text_normalize(tts_text, split=True):
  87. model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
  88. start_time = time.time()
  89. logging.info('synthesis text {}'.format(i))
  90. for model_output in self.model.inference(**model_input, stream=stream):
  91. speech_len = model_output['tts_speech'].shape[1] / 22050
  92. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  93. yield model_output
  94. start_time = time.time()