lenML/ChatTTS-Forge
301
1import logging2import random3from typing import Any, Dict, List4 5import numpy as np6from lxml import etree7 8from modules.data import styles_mgr9from modules.speaker import speaker_mgr10 11logger = logging.getLogger(__name__)12 13 14def expand_spk(attrs: dict):15 input_spk = attrs.get("spk", "")16 if isinstance(input_spk, int):17 return18 if isinstance(input_spk, str) and input_spk.isdigit():19 attrs.update({"spk": int(input_spk)})20 return21 try:22 speaker = speaker_mgr.get_speaker(input_spk)23 attrs.update({"spk": speaker})24 except Exception as e:25 logger.error(f"apply style failed, {e}")26 27 28def expand_style(attrs: dict):29 if attrs.get("style", "") != "":30 try:31 params = styles_mgr.find_params_by_name(str(attrs["style"]))32 attrs.update(params)33 except Exception as e:34 logger.error(f"apply style failed, {e}")35 36 37def merge_prompt(attrs: dict, elem):38 39 def attr_num(attrs: Dict[str, Any], k: str, min_value: int, max_value: int):40 val = elem.get(k, attrs.get(k, ""))41 if val == "":42 return43 if val == "max":44 val = max_value45 if val == "min":46 val = min_value47 val = np.clip(int(val), min_value, max_value)48 if "prefix" not in attrs or attrs["prefix"] == None:49 attrs["prefix"] = ""50 attrs["prefix"] += " " + f"[{k}_{val}]"51 52 attr_num(attrs, "oral", 0, 9)53 attr_num(attrs, "speed", 0, 9)54 attr_num(attrs, "laugh", 0, 2)55 attr_num(attrs, "break", 0, 7)56 57 58def apply_random_seed(attrs: dict):59 seed = attrs.get("seed", "")60 if seed == "random" or seed == "rand":61 seed = random.randint(0, 2**32 - 1)62 attrs["seed"] = seed63 logger.info(f"random seed: {seed}")64 