mirror of
https://github.com/FunAudioLLM/CosyVoice.git
synced 2026-02-05 18:09:24 +08:00
fix(bug).when generating text that contains only punctuation marks or whitespace characters, the CPU usage reaches 100%, and the process crashes.
This commit is contained in:
@@ -31,7 +31,7 @@ except ImportError:
|
|||||||
from tn.chinese.normalizer import Normalizer as ZhNormalizer
|
from tn.chinese.normalizer import Normalizer as ZhNormalizer
|
||||||
from tn.english.normalizer import Normalizer as EnNormalizer
|
from tn.english.normalizer import Normalizer as EnNormalizer
|
||||||
use_ttsfrd = False
|
use_ttsfrd = False
|
||||||
from cosyvoice.utils.frontend_utils import contains_chinese, replace_blank, replace_corner_mark, remove_bracket, spell_out_number, split_paragraph
|
from cosyvoice.utils.frontend_utils import contains_chinese, replace_blank, replace_corner_mark, remove_bracket, spell_out_number, split_paragraph, is_only_punctuation
|
||||||
|
|
||||||
|
|
||||||
class CosyVoiceFrontEnd:
|
class CosyVoiceFrontEnd:
|
||||||
@@ -109,6 +109,10 @@ class CosyVoiceFrontEnd:
|
|||||||
|
|
||||||
def text_normalize(self, text, split=True):
|
def text_normalize(self, text, split=True):
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
|
# When generating text that contains only punctuation marks or whitespace characters
|
||||||
|
# - Returning empty texts ensures consistent processing logic.
|
||||||
|
if is_only_punctuation(text):
|
||||||
|
return []
|
||||||
if contains_chinese(text):
|
if contains_chinese(text):
|
||||||
if self.use_ttsfrd:
|
if self.use_ttsfrd:
|
||||||
texts = [i["text"] for i in json.loads(self.frd.do_voicegen_frd(text))["sentences"]]
|
texts = [i["text"] for i in json.loads(self.frd.do_voicegen_frd(text))["sentences"]]
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ from torch.nn import functional as F
|
|||||||
from contextlib import nullcontext
|
from contextlib import nullcontext
|
||||||
import uuid
|
import uuid
|
||||||
from cosyvoice.utils.common import fade_in_out
|
from cosyvoice.utils.common import fade_in_out
|
||||||
from cosyvoice.utils.common import is_only_punctuation
|
|
||||||
|
|
||||||
|
|
||||||
class CosyVoiceModel:
|
class CosyVoiceModel:
|
||||||
@@ -146,10 +145,6 @@ class CosyVoiceModel:
|
|||||||
llm_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32),
|
llm_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32),
|
||||||
flow_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):
|
prompt_speech_feat=torch.zeros(1, 0, 80), stream=False, speed=1.0, **kwargs):
|
||||||
# When generating text that contains only punctuation marks or whitespace characters
|
|
||||||
# - Returning 10ms of silence ensures consistent processing logic.
|
|
||||||
if is_only_punctuation(text):
|
|
||||||
return {'tts_speech': torch.zeros(1, int(0.01 * 22050))}
|
|
||||||
# this_uuid is used to track variables related to this inference thread
|
# this_uuid is used to track variables related to this inference thread
|
||||||
this_uuid = str(uuid.uuid1())
|
this_uuid = str(uuid.uuid1())
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ from typing import List
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
import regex
|
|
||||||
|
|
||||||
IGNORE_ID = -1
|
IGNORE_ID = -1
|
||||||
|
|
||||||
@@ -156,12 +155,6 @@ def set_all_random_seed(seed):
|
|||||||
torch.cuda.manual_seed_all(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))
|
|
||||||
|
|
||||||
|
|
||||||
def mask_to_bias(mask: torch.Tensor, dtype: torch.dtype) -> torch.Tensor:
|
def mask_to_bias(mask: torch.Tensor, dtype: torch.dtype) -> torch.Tensor:
|
||||||
assert mask.dtype == torch.bool
|
assert mask.dtype == torch.bool
|
||||||
assert dtype in [torch.float32, torch.bfloat16, torch.float16]
|
assert dtype in [torch.float32, torch.bfloat16, torch.float16]
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import regex
|
||||||
chinese_char_pattern = re.compile(r'[\u4e00-\u9fff]+')
|
chinese_char_pattern = re.compile(r'[\u4e00-\u9fff]+')
|
||||||
|
|
||||||
|
|
||||||
@@ -127,3 +128,9 @@ def replace_blank(text: str):
|
|||||||
else:
|
else:
|
||||||
out_str.append(c)
|
out_str.append(c)
|
||||||
return "".join(out_str)
|
return "".join(out_str)
|
||||||
|
|
||||||
|
|
||||||
|
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))
|
||||||
|
|||||||
Reference in New Issue
Block a user