cosyvoice.py 4.9 KB

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