diff --git a/cosyvoice/cli/model.py b/cosyvoice/cli/model.py index de86a24..22affe4 100644 --- a/cosyvoice/cli/model.py +++ b/cosyvoice/cli/model.py @@ -19,6 +19,7 @@ from torch.nn import functional as F from contextlib import nullcontext import uuid from cosyvoice.utils.common import fade_in_out +from cosyvoice.utils.common import is_only_punctuation class CosyVoiceModel: @@ -145,6 +146,9 @@ class CosyVoiceModel: llm_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32), flow_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32), prompt_speech_feat=torch.zeros(1, 0, 80), stream=False, speed=1.0, **kwargs): + if is_only_punctuation(text): + logging.info('only punctuation, skip synthesis:{}'.format(text)) + return {'tts_speech': torch.zeros(1, int(0.01 * 22050))} #返回10ms空白音频,保证了一致的上下游处理逻辑 # this_uuid is used to track variables related to this inference thread this_uuid = str(uuid.uuid1()) with self.lock: diff --git a/cosyvoice/utils/common.py b/cosyvoice/utils/common.py index 25bc835..f926424 100644 --- a/cosyvoice/utils/common.py +++ b/cosyvoice/utils/common.py @@ -20,6 +20,7 @@ from typing import List import numpy as np import torch +import regex IGNORE_ID = -1 @@ -153,3 +154,9 @@ def set_all_random_seed(seed): np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) + + +def is_only_punctuation(text): + # Regular expression: Match strings that consist only of punctuation marks or are empty. + punctuation_pattern = r'^[\p{P}\p{S}]*$' + return bool(regex.fullmatch(punctuation_pattern, text))