Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	
		zhzluke96
		
	commited on
		
		
					Commit 
							
							·
						
						d8e7d56
	
1
								Parent(s):
							
							ec6a7d0
								
update
Browse files- modules/ChatTTS/example.ipynb +0 -0
- modules/api/impl/base_api.py +0 -48
- modules/devices.py +0 -8
- modules/normalization.py +2 -0
- modules/utils/normalization.py +0 -147
    	
        modules/ChatTTS/example.ipynb
    DELETED
    
    | The diff for this file is too large to render. 
		See raw diff | 
|  | 
    	
        modules/api/impl/base_api.py
    DELETED
    
    | @@ -1,48 +0,0 @@ | |
| 1 | 
            -
            from pydantic import BaseModel
         | 
| 2 | 
            -
             | 
| 3 | 
            -
             | 
| 4 | 
            -
            from modules.speaker import speaker_mgr
         | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
            from modules.data import styles_mgr
         | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
            from modules.api import utils as api_utils
         | 
| 11 | 
            -
            from modules.api.Api import APIManager
         | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
            async def list_styles():
         | 
| 15 | 
            -
                return {"message": "ok", "data": styles_mgr.list_items()}
         | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
            async def list_speakers():
         | 
| 19 | 
            -
                return {
         | 
| 20 | 
            -
                    "message": "ok",
         | 
| 21 | 
            -
                    "data": [spk.to_json() for spk in speaker_mgr.list_speakers()],
         | 
| 22 | 
            -
                }
         | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
            class CreateSpeaker(BaseModel):
         | 
| 26 | 
            -
                seed: int
         | 
| 27 | 
            -
                name: str = ""
         | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
            async def create_speaker(request: CreateSpeaker):
         | 
| 31 | 
            -
                speaker = speaker_mgr.create_speaker(request.seed, request.name)
         | 
| 32 | 
            -
                return {"message": "ok", "data": speaker.to_json()}
         | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
            async def refresh_speakers():
         | 
| 36 | 
            -
                speaker_mgr.refresh_speakers()
         | 
| 37 | 
            -
                return {"message": "ok"}
         | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
            def setup(app: APIManager):
         | 
| 41 | 
            -
                app.get("/v1/styles/list", response_model=api_utils.BaseResponse)(list_styles)
         | 
| 42 | 
            -
                app.get("/v1/speakers/list", response_model=api_utils.BaseResponse)(list_speakers)
         | 
| 43 | 
            -
                app.post("/v1/speaker/create", response_model=api_utils.BaseResponse)(
         | 
| 44 | 
            -
                    create_speaker
         | 
| 45 | 
            -
                )
         | 
| 46 | 
            -
                app.post("/v1/speaker/refresh", response_model=api_utils.BaseResponse)(
         | 
| 47 | 
            -
                    refresh_speakers
         | 
| 48 | 
            -
                )
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
    	
        modules/devices.py
    DELETED
    
    | @@ -1,8 +0,0 @@ | |
| 1 | 
            -
            import torch
         | 
| 2 | 
            -
             | 
| 3 | 
            -
             | 
| 4 | 
            -
            def torch_gc():
         | 
| 5 | 
            -
                if torch.cuda.is_available():
         | 
| 6 | 
            -
                    with torch.cuda.device("cuda"):
         | 
| 7 | 
            -
                        torch.cuda.empty_cache()
         | 
| 8 | 
            -
                        torch.cuda.ipc_collect()
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
    	
        modules/normalization.py
    CHANGED
    
    | @@ -143,6 +143,8 @@ def replace_unk_tokens(text): | |
| 143 | 
             
                把不在字典里的字符替换为 " , "
         | 
| 144 | 
             
                """
         | 
| 145 | 
             
                chat_tts = models.load_chat_tts()
         | 
|  | |
|  | |
| 146 | 
             
                tokenizer = chat_tts.pretrain_models["tokenizer"]
         | 
| 147 | 
             
                vocab = tokenizer.get_vocab()
         | 
| 148 | 
             
                vocab_set = set(vocab.keys())
         | 
|  | |
| 143 | 
             
                把不在字典里的字符替换为 " , "
         | 
| 144 | 
             
                """
         | 
| 145 | 
             
                chat_tts = models.load_chat_tts()
         | 
| 146 | 
            +
                if "tokenizer" not in chat_tts.pretrain_models:
         | 
| 147 | 
            +
                    return text
         | 
| 148 | 
             
                tokenizer = chat_tts.pretrain_models["tokenizer"]
         | 
| 149 | 
             
                vocab = tokenizer.get_vocab()
         | 
| 150 | 
             
                vocab_set = set(vocab.keys())
         | 
    	
        modules/utils/normalization.py
    DELETED
    
    | @@ -1,147 +0,0 @@ | |
| 1 | 
            -
            from modules.utils.zh_normalization.text_normlization import *
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            character_map = {
         | 
| 4 | 
            -
                ":": ",",
         | 
| 5 | 
            -
                ";": ",",
         | 
| 6 | 
            -
                "!": "。",
         | 
| 7 | 
            -
                "(": ",",
         | 
| 8 | 
            -
                ")": ",",
         | 
| 9 | 
            -
                "【": ",",
         | 
| 10 | 
            -
                "】": ",",
         | 
| 11 | 
            -
                "『": ",",
         | 
| 12 | 
            -
                "』": ",",
         | 
| 13 | 
            -
                "「": ",",
         | 
| 14 | 
            -
                "」": ",",
         | 
| 15 | 
            -
                "《": ",",
         | 
| 16 | 
            -
                "》": ",",
         | 
| 17 | 
            -
                "-": ",",
         | 
| 18 | 
            -
                "‘": " ",
         | 
| 19 | 
            -
                "“": " ",
         | 
| 20 | 
            -
                "’": " ",
         | 
| 21 | 
            -
                "”": " ",
         | 
| 22 | 
            -
                ":": ",",
         | 
| 23 | 
            -
                ";": ",",
         | 
| 24 | 
            -
                "!": ".",
         | 
| 25 | 
            -
                "(": ",",
         | 
| 26 | 
            -
                ")": ",",
         | 
| 27 | 
            -
                # '[': ',',
         | 
| 28 | 
            -
                # ']': ',',
         | 
| 29 | 
            -
                ">": ",",
         | 
| 30 | 
            -
                "<": ",",
         | 
| 31 | 
            -
                "-": ",",
         | 
| 32 | 
            -
            }
         | 
| 33 | 
            -
             | 
| 34 | 
            -
            character_to_word = {
         | 
| 35 | 
            -
                " & ": " and ",
         | 
| 36 | 
            -
            }
         | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
            def apply_character_to_word(text):
         | 
| 40 | 
            -
                for k, v in character_to_word.items():
         | 
| 41 | 
            -
                    text = text.replace(k, v)
         | 
| 42 | 
            -
                return text
         | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
            def apply_character_map(text):
         | 
| 46 | 
            -
                translation_table = str.maketrans(character_map)
         | 
| 47 | 
            -
                return text.translate(translation_table)
         | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
            def insert_spaces_between_uppercase(s):
         | 
| 51 | 
            -
                # 使用正则表达式在每个相邻的大写字母之间插入空格
         | 
| 52 | 
            -
                return re.sub(
         | 
| 53 | 
            -
                    r"(?<=[A-Z])(?=[A-Z])|(?<=[a-z])(?=[A-Z])|(?<=[\u4e00-\u9fa5])(?=[A-Z])|(?<=[A-Z])(?=[\u4e00-\u9fa5])",
         | 
| 54 | 
            -
                    " ",
         | 
| 55 | 
            -
                    s,
         | 
| 56 | 
            -
                )
         | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
            def ensure_suffix(a: str, b: str, c: str):
         | 
| 60 | 
            -
                a = a.strip()
         | 
| 61 | 
            -
                if not a.endswith(b):
         | 
| 62 | 
            -
                    a += c
         | 
| 63 | 
            -
                return a
         | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
            email_domain_map = {
         | 
| 67 | 
            -
                "outlook.com": "Out look",
         | 
| 68 | 
            -
                "hotmail.com": "Hot mail",
         | 
| 69 | 
            -
                "yahoo.com": "雅虎",
         | 
| 70 | 
            -
            }
         | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
            # 找到所有 email 并将 name 分割为单个字母,@替换为 at ,. 替换为 dot,常见域名替换为单词
         | 
| 74 | 
            -
            #
         | 
| 75 | 
            -
            # 例如:
         | 
| 76 | 
            -
            # [email protected] => z h z l u k e 9 6 at out look dot com
         | 
| 77 | 
            -
            def email_detect(text):
         | 
| 78 | 
            -
                email_pattern = re.compile(r"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})")
         | 
| 79 | 
            -
             | 
| 80 | 
            -
                def replace(match):
         | 
| 81 | 
            -
                    email = match.group(1)
         | 
| 82 | 
            -
                    name, domain = email.split("@")
         | 
| 83 | 
            -
                    name = " ".join(name)
         | 
| 84 | 
            -
                    if domain in email_domain_map:
         | 
| 85 | 
            -
                        domain = email_domain_map[domain]
         | 
| 86 | 
            -
                    domain = domain.replace(".", " dot ")
         | 
| 87 | 
            -
                    return f"{name} at {domain}"
         | 
| 88 | 
            -
             | 
| 89 | 
            -
                return email_pattern.sub(replace, text)
         | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
            def pre_normalize(text):
         | 
| 93 | 
            -
                # NOTE: 效果一般...
         | 
| 94 | 
            -
                # text = email_detect(text)
         | 
| 95 | 
            -
                return text
         | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
            def post_normalize(text):
         | 
| 99 | 
            -
                text = insert_spaces_between_uppercase(text)
         | 
| 100 | 
            -
                text = apply_character_map(text)
         | 
| 101 | 
            -
                text = apply_character_to_word(text)
         | 
| 102 | 
            -
                return text
         | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
            def text_normalize(text, is_end=False):
         | 
| 106 | 
            -
                # https://github.com/PaddlePaddle/PaddleSpeech/tree/develop/paddlespeech/t2s/frontend/zh_normalization
         | 
| 107 | 
            -
                tx = TextNormalizer()
         | 
| 108 | 
            -
             | 
| 109 | 
            -
                # 匹配 \[.+?\] 的部分
         | 
| 110 | 
            -
                pattern = re.compile(r"(\[.+?\])|([^[]+)")
         | 
| 111 | 
            -
             | 
| 112 | 
            -
                def normalize_part(part):
         | 
| 113 | 
            -
                    part = pre_normalize(part)
         | 
| 114 | 
            -
                    sentences = tx.normalize(part)
         | 
| 115 | 
            -
                    dest_text = ""
         | 
| 116 | 
            -
                    for sentence in sentences:
         | 
| 117 | 
            -
                        dest_text += post_normalize(sentence)
         | 
| 118 | 
            -
                    return dest_text
         | 
| 119 | 
            -
             | 
| 120 | 
            -
                def replace(match):
         | 
| 121 | 
            -
                    if match.group(1):
         | 
| 122 | 
            -
                        return f" {match.group(1)} "
         | 
| 123 | 
            -
                    else:
         | 
| 124 | 
            -
                        return normalize_part(match.group(2))
         | 
| 125 | 
            -
             | 
| 126 | 
            -
                result = pattern.sub(replace, text)
         | 
| 127 | 
            -
             | 
| 128 | 
            -
                # NOTE: 加了会有杂音...
         | 
| 129 | 
            -
                # if is_end:
         | 
| 130 | 
            -
                # 加这个是为了防止吞字
         | 
| 131 | 
            -
                # result = ensure_suffix(result, "[uv_break]", "。。。[uv_break]。。。")
         | 
| 132 | 
            -
             | 
| 133 | 
            -
                return result
         | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
| 136 | 
            -
            if __name__ == "__main__":
         | 
| 137 | 
            -
                print(
         | 
| 138 | 
            -
                    text_normalize(
         | 
| 139 | 
            -
                        "ChatTTS是专门为对话场景设计的文本转语音模型,例如LLM助手对话任务。它支持英文和中文两种语言。最大的模型使用了10万小时以上的中英文数据进行训练。在HuggingFace中开源的版本为4万小时训练且未SFT的版本."
         | 
| 140 | 
            -
                    )
         | 
| 141 | 
            -
                )
         | 
| 142 | 
            -
                print(
         | 
| 143 | 
            -
                    text_normalize(
         | 
| 144 | 
            -
                        " [oral_9] [laugh_0] [break_0] 电 [speed_0] 影 [speed_0] 中 梁朝伟 [speed_9] 扮演的陈永仁的编号27149"
         | 
| 145 | 
            -
                    )
         | 
| 146 | 
            -
                )
         | 
| 147 | 
            -
                print(text_normalize(" 明天有62%的概率降雨"))
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
