cosyvoice.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 torch
  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. class CosyVoice:
  21. def __init__(self, model_dir):
  22. instruct = True if '-Instruct' in model_dir else False
  23. self.model_dir = model_dir
  24. if not os.path.exists(model_dir):
  25. model_dir = snapshot_download(model_dir)
  26. with open('{}/cosyvoice.yaml'.format(model_dir), 'r') as f:
  27. configs = load_hyperpyyaml(f)
  28. self.frontend = CosyVoiceFrontEnd(configs['get_tokenizer'],
  29. configs['feat_extractor'],
  30. '{}/campplus.onnx'.format(model_dir),
  31. '{}/speech_tokenizer_v1.onnx'.format(model_dir),
  32. '{}/spk2info.pt'.format(model_dir),
  33. instruct,
  34. configs['allowed_special'])
  35. self.model = CosyVoiceModel(configs['llm'], configs['flow'], configs['hift'])
  36. self.model.load('{}/llm.pt'.format(model_dir),
  37. '{}/flow.pt'.format(model_dir),
  38. '{}/hift.pt'.format(model_dir))
  39. del configs
  40. def list_avaliable_spks(self):
  41. spks = list(self.frontend.spk2info.keys())
  42. return spks
  43. def inference_sft(self, tts_text, spk_id):
  44. tts_speeches = []
  45. for i in self.frontend.text_normalize(tts_text, split=True):
  46. model_input = self.frontend.frontend_sft(i, spk_id)
  47. model_output = self.model.inference(**model_input)
  48. tts_speeches.append(model_output['tts_speech'])
  49. return {'tts_speech': torch.concat(tts_speeches, dim=1)}
  50. def inference_zero_shot(self, tts_text, prompt_text, prompt_speech_16k):
  51. prompt_text = self.frontend.text_normalize(prompt_text, split=False)
  52. tts_speeches = []
  53. for i in self.frontend.text_normalize(tts_text, split=True):
  54. model_input = self.frontend.frontend_zero_shot(i, prompt_text, prompt_speech_16k)
  55. model_output = self.model.inference(**model_input)
  56. tts_speeches.append(model_output['tts_speech'])
  57. return {'tts_speech': torch.concat(tts_speeches, dim=1)}
  58. def inference_cross_lingual(self, tts_text, prompt_speech_16k):
  59. if self.frontend.instruct is True:
  60. raise ValueError('{} do not support cross_lingual inference'.format(self.model_dir))
  61. tts_speeches = []
  62. for i in self.frontend.text_normalize(tts_text, split=True):
  63. model_input = self.frontend.frontend_cross_lingual(i, prompt_speech_16k)
  64. model_output = self.model.inference(**model_input)
  65. tts_speeches.append(model_output['tts_speech'])
  66. return {'tts_speech': torch.concat(tts_speeches, dim=1)}
  67. def inference_instruct(self, tts_text, spk_id, instruct_text):
  68. if self.frontend.instruct is False:
  69. raise ValueError('{} do not support instruct inference'.format(self.model_dir))
  70. instruct_text = self.frontend.text_normalize(instruct_text, split=False)
  71. tts_speeches = []
  72. for i in self.frontend.text_normalize(tts_text, split=True):
  73. model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text)
  74. model_output = self.model.inference(**model_input)
  75. tts_speeches.append(model_output['tts_speech'])
  76. return {'tts_speech': torch.concat(tts_speeches, dim=1)}