Spaces:
Running
Running
zhzluke96 commited on
Commit ·
84cfd61
1
Parent(s): 22884c9
update
Browse filesFiles changed (5)
12345678import torch
def torch_gc(): if torch.cuda.is_available(): with torch.cuda.device("cuda"): torch.cuda.empty_cache() torch.cuda.ipc_collect()7 unmodified lines8910111213141582 unmodified lines98991001011021031041057 unmodified lines
import logging
from modules import devices
logger = logging.getLogger(__name__)
82 unmodified lines
sample_rate = 24000
devices.torch_gc()
return [(sample_rate, np.array(wav).flatten().astype(np.float32)) for wav in wavs]
74 unmodified lines757677787980818283848384858687888922 unmodified lines1121131141131141151161171181191151161174 unmodified lines12212312412512612712812913013113213313413513613713813914014114214314414514614714814915038 unmodified lines18919019119219319419520 unmodified lines21621721820021922022112 unmodified lines23423523623723823924024124224324424524624724824974 unmodified lines "“": " ", "’": " ", "”": " ", '"': " ", "'": " ", ":": ",", ";": ",", "!": ".", "(": ",", ")": ",", # '[': ',', # ']': ',', "[": ",", "]": ",", ">": ",", "<": ",", "-": ",",22 unmodified lines return emojiswitch.demojize(text, delimiters=("", ""), lang="zh")
@pre_normalize()def apply_markdown_to_text(text): if is_markdown(text): text = markdown_to_text(text) return text
@post_normalize()def insert_spaces_between_uppercase(s): # 使用正则表达式在每个相邻的大写字母之间插入空格4 unmodified lines )
@pre_normalize()def apply_markdown_to_text(text): if is_markdown(text): text = markdown_to_text(text) return text
# 将 "xxx" => \nxxx\n# 将 'xxx' => \nxxx\n@pre_normalize()def replace_quotes(text): repl = r"\n\1\n" patterns = [ ['"', '"'], ["'", "'"], ["“", "”"], ["‘", "’"], ] for p in patterns: text = re.sub(rf"({p[0]}[^{p[0]}{p[1]}]+?{p[1]})", repl, text) return text
def ensure_suffix(a: str, b: str, c: str): a = a.strip() if not a.endswith(b):38 unmodified lines sentences = tx.normalize(part) dest_text = "" for sentence in sentences: sentence = apply_post_normalize(sentence) dest_text += sentence return dest_text
20 unmodified lines lines = [line for line in lines if line] lines = [sentence_normalize(line) for line in lines] content = "\n".join(lines) content = apply_post_normalize(content) return content
12 unmodified lines
*一条文本* """, """在沙漠、岩石、雪地上行走了很长的时间以后,小王子终于发现了一条大路。所有的大路都是通往人住的地方的。“你们好。”小王子说。这是一个玫瑰盛开的花园。“你好。”玫瑰花说道。小王子瞅着这些花,它们全都和他的那朵花一样。“你们是什么花?”小王子惊奇地问。“我们是玫瑰花。”花儿们说道。“啊!”小王子说……。 """, ]
for i, test_case in enumerate(test_cases):4 unmodified lines5678910111213141516171819204 unmodified linesimport numpy as npfrom io import BytesIO
INT16_MAX = np.iinfo(np.int16).max
def audio_to_int16(audio_data): if audio_data.dtype == np.float32: audio_data = (audio_data * INT16_MAX).astype(np.int16) if audio_data.dtype == np.float16: audio_data = (audio_data * INT16_MAX).astype(np.int16) return audio_data
def audiosegment_to_librosawav(audiosegment): channel_sounds = audiosegment.split_to_mono()11234567891011121314151624 unmodified lines41424332444546474 unmodified lines525354435556575818 unmodified lines777879688081828320 unmodified lines1041051069510710810911011111211311 unmodified lines12512612711312812913013113211813313413513635 unmodified lines17217317416016117517617713 unmodified lines191192193194181195196197198181 unmodified lines380381382369383384385386220 unmodified lines607608609596610611612613292 unmodified lines906907908895909910911912try:import spaces import spacesexcept:
class NoneSpaces: def __init__(self): pass
def GPU(self, fn): return fn
spaces = NoneSpaces()
import osimport logging
24 unmodified linesfrom modules.normalization import text_normalizefrom modules import refiner, config
from modules.utils import envfrom modules.utils import env, audiofrom modules.SentenceSplitter import SentenceSplitter
torch._dynamo.config.cache_size_limit = 644 unmodified lines "tts_max": 1000, "ssml_max": 5000, "spliter_threshold": 100, "max_batch_size": 12, "max_batch_size": 8,}
18 unmodified lines
@torch.inference_mode()@spaces.GPUdef synthesize_ssml(ssml: str, batch_size=8):def synthesize_ssml(ssml: str, batch_size=4): try: batch_size = int(batch_size) except Exception:20 unmodified lines
buffer.seek(0)
return buffer.read() audio_data = buffer.read() audio_data = audio.audio_to_int16(audio_data)
return audio_data
@torch.inference_mode()11 unmodified lines prefix, style, disable_normalize=False, batch_size=8, batch_size=4,): try: batch_size = int(batch_size) except Exception: batch_size = 8 batch_size = 4
max_len = webui_config["tts_max"] text = text.strip()[0:max_len]35 unmodified lines prompt2=prompt2, prefix=prefix, )
return sample_rate, audio_data else: spliter = SentenceSplitter(webui_config["spliter_threshold"]) sentences = spliter.parse(text)13 unmodified lines sample_rate = audio_data_batch[0][0] audio_data = np.concatenate([data for _, data in audio_data_batch])
audio_data = audio.audio_to_int16(audio_data) return sample_rate, audio_data return sample_rate, audio_data
@torch.inference_mode()181 unmodified lines batch_size_input = gr.Slider( 1, webui_config["max_batch_size"], value=8, value=4, step=1, label="Batch Size", )220 unmodified lines # batch size batch_size_input = gr.Slider( label="Batch Size", value=8, value=4, minimum=1, maximum=webui_config["max_batch_size"], step=1,292 unmodified lines
webui_config["tts_max"] = env.get_env_or_arg(args, "tts_max_len", 1000, int) webui_config["ssml_max"] = env.get_env_or_arg(args, "ssml_max_len", 5000, int) webui_config["max_batch_size"] = env.get_env_or_arg(args, "max_batch_size", 12, int) webui_config["max_batch_size"] = env.get_env_or_arg(args, "max_batch_size", 8, int)
demo = create_interface()
modules/devices.pyAdded+8
modules/generate_audio.py+4
modules/normalization.py+38-10
modules/utils/audio.py+10
webui.py+27-13