-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: cosyvoice support pt file #2431
Open
Minamiyama
wants to merge
5
commits into
xorbitsai:main
Choose a base branch
from
Minamiyama:ENH/cosyvoice-support-pt-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,14 @@ | |
from cosyvoice.cli.frontend import CosyVoiceFrontEnd | ||
from cosyvoice.cli.model import CosyVoiceModel | ||
from cosyvoice.utils.file_utils import logging | ||
import torch | ||
|
||
class CosyVoice: | ||
|
||
def __init__(self, model_dir, load_jit=True): | ||
self.default_voices = ['中文女', '中文男', '日语男', '粤语女', '英文女', '英文男', '韩语女'] | ||
self.pt_cache = {} | ||
|
||
instruct = True if '-Instruct' in model_dir else False | ||
self.model_dir = model_dir | ||
if not os.path.exists(model_dir): | ||
|
@@ -49,8 +53,29 @@ def list_avaliable_spks(self): | |
return spks | ||
|
||
def inference_sft(self, tts_text, spk_id, stream=False): | ||
if spk_id not in self.default_voices and os.environ["COSYVOICE_PT_PATH"] is not None: | ||
if spk_id not in self.pt_cache: | ||
self.pt_cache[spk_id] = torch.load(f'{os.environ["COSYVOICE_PT_PATH"]}/{spk_id}.pt') | ||
newspk = self.pt_cache[spk_id] | ||
for i in self.frontend.text_normalize(tts_text, split=True): | ||
model_input = self.frontend.frontend_sft(i, spk_id) | ||
if newspk is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that the |
||
model_input = self.frontend.frontend_sft(i, "中文女") | ||
|
||
model_input["flow_embedding"] = newspk["flow_embedding"] | ||
model_input["llm_embedding"] = newspk["llm_embedding"] | ||
|
||
model_input["llm_prompt_speech_token"] = newspk["llm_prompt_speech_token"] | ||
model_input["llm_prompt_speech_token_len"] = newspk["llm_prompt_speech_token_len"] | ||
|
||
model_input["flow_prompt_speech_token"] = newspk["flow_prompt_speech_token"] | ||
model_input["flow_prompt_speech_token_len"] = newspk["flow_prompt_speech_token_len"] | ||
|
||
model_input["prompt_speech_feat_len"] = newspk["prompt_speech_feat_len"] | ||
model_input["prompt_speech_feat"] = newspk["prompt_speech_feat"] | ||
model_input["prompt_text"] = newspk["prompt_text"] | ||
model_input["prompt_text_len"] = newspk["prompt_text_len"] | ||
else: | ||
model_input = self.frontend.frontend_sft(i, spk_id) | ||
start_time = time.time() | ||
logging.info('synthesis text {}'.format(i)) | ||
for model_output in self.model.inference(**model_input, stream=stream): | ||
|
@@ -87,9 +112,24 @@ def inference_cross_lingual(self, tts_text, prompt_speech_16k, stream=False): | |
def inference_instruct(self, tts_text, spk_id, instruct_text, stream=False): | ||
if self.frontend.instruct is False: | ||
raise ValueError('{} do not support instruct inference'.format(self.model_dir)) | ||
if spk_id not in self.pt_cache: | ||
self.pt_cache[spk_id] = torch.load(f'{os.environ["COSYVOICE_PT_PATH"]}/{spk_id}.pt') | ||
newspk = self.pt_cache[spk_id] | ||
instruct_text = self.frontend.text_normalize(instruct_text, split=False) | ||
for i in self.frontend.text_normalize(tts_text, split=True): | ||
model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text) | ||
if newspk is not None: | ||
model_input = self.frontend.frontend_instruct(i, "中文女", instruct_text) | ||
|
||
model_input["flow_embedding"] = newspk["flow_embedding"] | ||
model_input["llm_embedding"] = newspk["llm_embedding"] | ||
|
||
model_input["llm_prompt_speech_token"] = newspk["llm_prompt_speech_token"] | ||
model_input["llm_prompt_speech_token_len"] = newspk["llm_prompt_speech_token_len"] | ||
|
||
model_input["flow_prompt_speech_token"] = newspk["flow_prompt_speech_token"] | ||
model_input["flow_prompt_speech_token_len"] = newspk["flow_prompt_speech_token_len"] | ||
else: | ||
model_input = self.frontend.frontend_instruct(i, spk_id, instruct_text) | ||
start_time = time.time() | ||
logging.info('synthesis text {}'.format(i)) | ||
for model_output in self.model.inference(**model_input, stream=stream): | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
os.environ["COSYVOICE_PT_PATH"]
may raise a KeyError when spk_id not in self.default_voices. Is this expected?