cosyvoice.py 5.4 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 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):
  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. # TODO
  45. self.model.load_trt()
  46. del configs
  47. def list_avaliable_spks(self):
  48. spks = list(self.frontend.spk2info.keys())
  49. return spks
  50. def inference_sft(self, tts_text, spk_id, stream=False):
  51. for i in self.frontend.text_normalize(tts_text, split=True):
  52. model_input = self.frontend.frontend_sft(i, spk_id)
  53. start_time = time.time()
  54. logging.info('synthesis text {}'.format(i))
  55. for model_output in self.model.inference(**model_input, stream=stream):
  56. speech_len = model_output['tts_speech'].shape[1] / 22050
  57. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  58. yield model_output
  59. start_time = time.time()
  60. def inference_zero_shot(self, tts_text, prompt_text, prompt_speech_16k, stream=False):
  61. prompt_text = self.frontend.text_normalize(prompt_text, split=False)
  62. for i in self.frontend.text_normalize(tts_text, split=True):
  63. model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_speech_16k)
  64. start_time = time.time()
  65. logging.info('synthesis text {}'.format(i))
  66. for model_output in self.model.inference(**model_input, stream=stream):
  67. speech_len = model_output['tts_speech'].shape[1] / 22050
  68. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  69. yield model_output
  70. start_time = time.time()
  71. def inference_cross_lingual(self, tts_text, prompt_speech_16k, stream=False):
  72. if self.frontend.instruct is True:
  73. raise ValueError('{} do not support cross_lingual inference'.format(self.model_dir))
  74. for i in self.frontend.text_normalize(tts_text, split=True):
  75. model_input = self.frontend.frontend_cross_lingual(i, prompt_speech_16k)
  76. start_time = time.time()
  77. logging.info('synthesis text {}'.format(i))
  78. for model_output in self.model.inference(**model_input, stream=stream):
  79. speech_len = model_output['tts_speech'].shape[1] / 22050
  80. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  81. yield model_output
  82. start_time = time.time()
  83. def inference_instruct(self, tts_text, spk_id, instruct_text, stream=False):
  84. if self.frontend.instruct is False:
  85. raise ValueError('{} do not support instruct inference'.format(self.model_dir))
  86. instruct_text = self.frontend.text_normalize(instruct_text, split=False)
  87. for i in self.frontend.text_normalize(tts_text, split=True):
  88. model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
  89. start_time = time.time()
  90. logging.info('synthesis text {}'.format(i))
  91. for model_output in self.model.inference(**model_input, stream=stream):
  92. speech_len = model_output['tts_speech'].shape[1] / 22050
  93. logging.info('yield speech len {}, rtf {}'.format(speech_len, (time.time() - start_time) / speech_len))
  94. yield model_output
  95. start_time = time.time()