Spaces:
Running
Running
zhzluke96 commited on
Commit ·
f367757
1
Parent(s): bb4ceb3
update
Browse filesFiles changed (9)
1 unmodified line234567889101112131415161718192021222324252627282930313233343536379101112131415161718404119202122232444454647484950515225262728293055565758593132333461626364656667686970717235363738394041427475764344774546478182838485484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495961 unmodified line
import zhon
from modules.models import get_tokenizerfrom modules.utils.detect_lang import guess_lang
def split_zhon_sentence(text): result = [] pattern = re.compile(zhon.hanzi.sentence) start = 0 for match in pattern.finditer(text): # 获取匹配的中文句子 end = match.end() result.append(text[start:end]) start = end
# 最后一个中文句子后面的内容(如果有)也需要添加到结果中 if start < len(text): result.append(text[start:])
result = [t for t in result if t.strip()] return result
def split_en_sentence(text): """ Split English text into sentences. """ # Define a regex pattern for English sentence splitting pattern = re.compile(r"(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|\!)\s") result = pattern.split(text)
# Filter out any empty strings or strings that are just whitespace result = [sentence.strip() for sentence in result if sentence.strip()]
return result# 解析文本 并根据停止符号分割成句子# 可以设置最大阈值,即如果分割片段小于这个阈值会与下一段合并class SentenceSplitter: SEP_TOKEN = " "
def __init__(self, threshold=100): assert ( isinstance(threshold, int) and threshold > 0 ), "Threshold must be greater than 0."
def is_eng_sentence(text): return guess_lang(text) == "en" self.sentence_threshold = threshold self.tokenizer = get_tokenizer()
def count_tokens(self, text: str): return len(self.tokenizer.tokenize(text))
def split_zhon_paragraph(text): lines = text.split("\n") result = [] for line in lines: if is_eng_sentence(line): result.extend(split_en_sentence(line)) else: result.extend(split_zhon_sentence(line)) return result def parse(self, text: str): sentences = self.split_paragraph(text) sentences = self.merge_text_by_threshold(sentences)
return sentences
# 解析文本 并根据停止符号分割成句子# 可以设置最大阈值,即如果分割片段小于这个阈值会与下一段合并class SentenceSplitter: def __init__(self, threshold=100): self.sentence_threshold = threshold def merge_text_by_threshold(self, setences: list[str]): """ Merge text by threshold.
def parse(self, text): sentences = split_zhon_paragraph(text)
# 合并小于最大阈值的片段 merged_sentences = [] temp_sentence = [] for sentence in sentences: if len(sentence) < self.sentence_threshold: temp_sentence.extend(sentence) if len(temp_sentence) >= self.sentence_threshold: merged_sentences.append(temp_sentence) temp_sentence = [] If the length of the text is less than the threshold, merge it with the next text. """ merged_sentences: list[str] = [] temp_sentence = "" for sentence in setences: if len(temp_sentence) + len(sentence) < self.sentence_threshold: temp_sentence += SentenceSplitter.SEP_TOKEN + sentence else: if temp_sentence: merged_sentences.append(temp_sentence) temp_sentence = [] merged_sentences.append(temp_sentence) temp_sentence = sentence merged_sentences.append(sentence)
if temp_sentence: merged_sentences.append(temp_sentence)
joind_sentences = [ "".join(sentence) for sentence in merged_sentences if sentence ] return joind_sentences return merged_sentences
def split_paragraph(self, text: str): """ Split text into sentences. """ lines = text.split("\n") sentences: list[str] = [] for line in lines: if self.is_eng_sentence(line): sentences.extend(self.split_en_sentence(line)) else: sentences.extend(self.split_zhon_sentence(line)) return sentences
def is_eng_sentence(self, text: str): return guess_lang(text) == "en"
def split_en_sentence(self, text: str): """ Split English text into sentences. """ pattern = re.compile(r"(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|\!)\s") sentences = pattern.split(text)
sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
return sentences
def split_zhon_sentence(self, text: str): """ Split Chinese text into sentences. """ sentences: list[str] = [] pattern = re.compile(zhon.hanzi.sentence) start = 0 for match in pattern.finditer(text): end = match.end() sentences.append(text[start:end]) start = end
if start < len(text): sentences.append(text[start:])
sentences = [t for t in sentences if t.strip()] return sentences
if __name__ == "__main__":2 unmodified lines345678967 unmodified lines7778798081828384852 unmodified linesimport threading
import torchfrom transformers import LlamaTokenizer
from modules import configfrom modules.ChatTTS import ChatTTS67 unmodified lines instance = load_chat_tts() logger.info("ChatTTS models reloaded") return instance
def get_tokenizer() -> LlamaTokenizer: chat_tts = load_chat_tts() tokenizer = chat_tts.pretrain_models["tokenizer"] return tokenizer1 unmodified line234567789101 unmodified linefrom io import BytesIO
import numpy as npimport pyrubberband as pyrbimport soundfile as sffrom pydub import AudioSegment, effectsimport pyrubberband as pyrb
INT16_MAX = np.iinfo(np.int16).max
1234567891013 unmodified lines242526272829303132332425343536373839404142import htmlimport refrom html.parser import HTMLParser
# NOTE: 现在没用这个,因为不好解决转义字符的问题# 除非分段处理,但是太麻烦了...class HTMLTagRemover(HTMLParser): def __init__(self): super().__init__()13 unmodified lines return parser.get_data()
def remove_html_tags_re(text): text = html.unescape(text) html_tags_pattern = re.compile(r"</?([a-zA-Z1-9]+)[^>]*>") return re.sub(html_tags_pattern, " ", text)
if __name__ == "__main__": input_text = "<h1>一个标题</h1> 这是一段包含<code>标签</code>的文本。" output_text = remove_html_tags(input_text) input_text = """<h1>一个标题</h1> 这是一段包含<code>标签</code>的文本。 <code>&</code><设定>一些文本</设定>""" # input_text = "我&你" output_text = remove_html_tags_re(input_text) print(output_text) # 输出: 一个标题 这是一段包含标签的文本。18 unmodified lines1920212223242526272829303132283334353610 unmodified lines474849505152537 unmodified lines61626364656667606869707172737475767778797 unmodified lines878889789091929318 unmodified lines spk = row.get("speaker") style = row.get("style")
text = text_normalize(text)
if text.strip() == "": continue
ssml += f"{indent}<voice" if spk: ssml += f' spk="{spk}"' if style: ssml += f' style="{style}"' ssml += ">\n" ssml += f"{indent}{indent}{text_normalize(text)}\n" ssml += f"{indent}{indent}{text}\n" ssml += f"{indent}</voice>\n" # 原封不动输出回去是为了触发 loadding 效果 return msg, spk, style, f"<speak version='0.1'>\n{ssml}</speak>"10 unmodified lines with gr.Row(): with gr.Column(scale=1): with gr.Group(): gr.Markdown("🗣️Speaker") spk_input_dropdown = gr.Dropdown( choices=get_spk_choices(), interactive=True,7 unmodified lines show_label=False, value="*auto", )
with gr.Group(): gr.Markdown("📝Text Input") msg = gr.Textbox( lines=5, label="Message", placeholder="Type speaker message here" lines=5, label="Message", show_label=False, placeholder="Type speaker message here", ) add = gr.Button("Add") undo = gr.Button("Undo") clear = gr.Button("Clear")
with gr.Column(scale=5): with gr.Group(): gr.Markdown("📔Script")7 unmodified lines col_count=(4, "fixed"), )
send_to_ssml_btn = gr.Button("📩Send to SSML", variant="primary") send_to_ssml_btn = gr.Button("📩Send to SSML", variant="primary")
def add_message(msg, spk, style, sheet: pd.DataFrame): if not msg:21 unmodified lines2223242526272829303132332 unmodified lines363738333940414236 unmodified lines798081828377848586873 unmodified lines919293949596979098991001011021031041051061071081091101111121138 unmodified lines12212312410510612512612712812913011313113213313413513613713813914014114211 unmodified lines15415515613513615715815916016116216316416516616721 unmodified lines indent = " " * 2
for i, row in dataframe.iterrows(): text = row.iloc[1] text = text_normalize(text)
if text.strip() == "": continue
ssml += f"{indent}<voice" if spk: ssml += f' spk="{spk}"'2 unmodified lines if seed: ssml += f' seed="{seed}"' ssml += ">\n" ssml += f"{indent}{indent}{text_normalize(row.iloc[1])}\n" ssml += f"{indent}{indent}{text}\n" ssml += f"{indent}</voice>\n" # 原封不动输出回去是为了触发 loadding 效果 return dataframe, spk, style, seed, f"<speak version='0.1'>\n{ssml}</speak>"36 unmodified lines show_label=False, value="*auto", )
with gr.Group(): gr.Markdown("🗣️Seed") gr.Markdown("💃Inference Seed") infer_seed_input = gr.Number( value=42, label="Inference Seed",3 unmodified lines ) infer_seed_rand_button = gr.Button( value="🎲", # tooltip="Random Seed", variant="secondary", )
send_btn = gr.Button("📩Send to SSML", variant="primary") with gr.Group(): gr.Markdown("🎛️Spliter") eos_input = gr.Textbox( label="eos", value="[uv_break]", ) spliter_thr_input = gr.Slider( label="Spliter Threshold", value=100, minimum=50, maximum=1000, step=1, )
with gr.Column(scale=3): with gr.Group():8 unmodified lines ) long_text_split_button = gr.Button("🔪Split Text")
with gr.Row(): with gr.Column(scale=3): with gr.Group(): gr.Markdown("🎨Output") long_text_output = gr.DataFrame( headers=["index", "text", "length"], datatype=["number", "str", "number"], elem_id="long-text-output", interactive=False, interactive=True, wrap=True, value=[], row_count=(0, "dynamic"), col_count=(3, "fixed"), )
send_btn = gr.Button("📩Send to SSML", variant="primary")
spk_input_dropdown.change( fn=lambda x: x.startswith("*") and "-1" or x.split(":")[-1].strip(), inputs=[spk_input_dropdown],11 unmodified lines ) long_text_split_button.click( split_long_text, inputs=[long_text_input], outputs=[long_text_output], inputs=[ long_text_input, spliter_thr_input, eos_input, ], outputs=[ long_text_output, ], )
infer_seed_rand_button.click(275 unmodified lines276277278279280279280281282282283284285285286287275 unmodified lines
@torch.inference_mode()@spaces.GPU(duration=120)def split_long_text(long_text_input): spliter = SentenceSplitter(webui_config.spliter_threshold)def split_long_text(long_text_input, spliter_threshold=100, eos=""): spliter = SentenceSplitter(threshold=spliter_threshold) sentences = spliter.parse(long_text_input) sentences = [text_normalize(s) for s in sentences] sentences = [text_normalize(s) + eos for s in sentences] data = [] for i, text in enumerate(sentences): token_length = spliter.count_tokens(text) data.append([i, text, len(text)]) data.append([i, text, token_length]) return data25 unmodified lines26272829293025 unmodified linespython-boxftfylibrosapyrubberbandpyrubberbandhttps://github.com/Dao-AILab/flash-attention/releases/download/v2.5.9.post1/flash_attn-2.5.9.post1+cu118torch1.12cxx11abiFALSE-cp310-cp310-linux_x86_64.whl29 unmodified lines303132333435363738394033343529 unmodified linesdcls_patch()ignore_useless_warnings()
import subprocess
subprocess.run( "pip install flash-attn --no-build-isolation", env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"}, shell=True,)
def setup_webui_args(parser: argparse.ArgumentParser): parser.add_argument("--server_name", type=str, help="server name")modules/SentenceSplitter.py+75-67
modules/models.py+7
modules/utils/audio.py+1-1
modules/utils/html.py+18-2
modules/webui/ssml/podcast_tab.py+15-3
modules/webui/ssml/spliter_tab.py+36-8
modules/webui/webui_utils.py+5-4
requirements.txt+2-1
webui.py-8