Spaces:
Running
Running
zhzluke96 commited on
Commit ·
02e90e4
1
Parent(s): d6fe286
update
Browse filesFiles changed (26)
- modules/ChatTTS/ChatTTS/core.py+21-5
- modules/ChatTTS/ChatTTS/model/dvae.py+3-3
- modules/ChatTTS/ChatTTS/model/gpt.py+2-3
- modules/ChatTTS/ChatTTS/utils/gpu_utils.py+3-1
- modules/ChatTTS/ChatTTS/utils/infer_utils.py+5-5
- modules/SynthesizeSegments.py+2-2
- modules/api/Api.py+12-1
- modules/api/impl/google_api.py+16-3
- modules/api/impl/models_api.pyAdded+11
- modules/api/impl/openai_api.py+19-7
- modules/api/impl/ping_api.pyAdded+8
- modules/api/utils.py+6-2
- modules/config.py+2-8
- modules/devices/__init__.pyAdded
- modules/devices/devices.pyAdded+160
- modules/devices/mac_devices.pyAdded+42
- modules/generate_audio.py+33-5
- modules/models.py+24-20
- modules/normalization.py+47-2
- modules/refiner.py+1-1
- modules/speaker.py+11-2
- modules/synthesize_audio.py+2-1
- modules/utils/JsonObject.pyAdded+113
- modules/utils/cache.pyAdded+92
- modules/utils/zh_normalization/text_normlization.py+3-3
- webui.py+49-22
100 unmodified lines1011021031041051061071081091101111121131 unmodified line1151161171181191201211 unmodified line1231241251261271281296 unmodified lines136137138139140141142143144145146147100 unmodified linestokenizer_path: str = None,device: str = None,compile: bool = True,):if not device:device = select_device(4096)self.logger.log(logging.INFO, f"use {device}")if vocos_config_path:vocos = Vocos.from_hparams(vocos_config_path).to(device).eval()assert vocos_ckpt_path, "vocos_ckpt_path should not be None"vocos.load_state_dict(torch.load(vocos_ckpt_path))self.pretrain_models["vocos"] = vocos1 unmodified lineif dvae_config_path:cfg = OmegaConf.load(dvae_config_path)dvae = DVAE(**cfg).to(device).eval()assert dvae_ckpt_path, "dvae_ckpt_path should not be None"dvae.load_state_dict(torch.load(dvae_ckpt_path, map_location=device))self.pretrain_models["dvae"] = dvae1 unmodified lineif gpt_config_path:cfg = OmegaConf.load(gpt_config_path)gpt = GPT_warpper(**cfg).to(device).eval()assert gpt_ckpt_path, "gpt_ckpt_path should not be None"gpt.load_state_dict(torch.load(gpt_ckpt_path, map_location=device))if compile and "cuda" in str(device):6 unmodified linesassert os.path.exists(spk_stat_path), f"Missing spk_stat.pt: {spk_stat_path}"self.pretrain_models["spk_stat"] = torch.load(spk_stat_path).to(device)self.logger.log(logging.INFO, "gpt loaded.")if decoder_config_path:cfg = OmegaConf.load(decoder_config_path)decoder = DVAE(**cfg).to(device).eval()assert decoder_ckpt_path, "decoder_ckpt_path should not be None"decoder.load_state_dict(torch.load(decoder_ckpt_path, map_location=device))self.pretrain_models["decoder"] = decoder100 unmodified lines1011021031041051061071081091101111121131141151161171181191201211221231241251261271 unmodified line1291301311321331341351 unmodified line1371381391401411421436 unmodified lines150151152153154155156157158159160161162163100 unmodified linestokenizer_path: str = None,device: str = None,compile: bool = True,dtype: torch.dtype = torch.float32,dtype_vocos: torch.dtype = None,dtype_dvae: torch.dtype = None,dtype_gpt: torch.dtype = None,dtype_decoder: torch.dtype = None,):if not device:device = select_device(4096)self.logger.log(logging.INFO, f"use {device}")dtype_vocos = dtype_vocos or dtypedtype_dvae = dtype_dvae or dtypedtype_gpt = dtype_gpt or dtypedtype_decoder = dtype_decoder or dtypeif vocos_config_path:vocos = (Vocos.from_hparams(vocos_config_path).to(device=device, dtype=dtype_vocos).eval())assert vocos_ckpt_path, "vocos_ckpt_path should not be None"vocos.load_state_dict(torch.load(vocos_ckpt_path))self.pretrain_models["vocos"] = vocos1 unmodified lineif dvae_config_path:cfg = OmegaConf.load(dvae_config_path)dvae = DVAE(**cfg).to(device=device, dtype=dtype_dvae).eval()assert dvae_ckpt_path, "dvae_ckpt_path should not be None"dvae.load_state_dict(torch.load(dvae_ckpt_path, map_location=device))self.pretrain_models["dvae"] = dvae1 unmodified lineif gpt_config_path:cfg = OmegaConf.load(gpt_config_path)gpt = GPT_warpper(**cfg).to(device=device, dtype=dtype_gpt).eval()assert gpt_ckpt_path, "gpt_ckpt_path should not be None"gpt.load_state_dict(torch.load(gpt_ckpt_path, map_location=device))if compile and "cuda" in str(device):6 unmodified linesassert os.path.exists(spk_stat_path), f"Missing spk_stat.pt: {spk_stat_path}"self.pretrain_models["spk_stat"] = torch.load(spk_stat_path).to(device=device, dtype=dtype)self.logger.log(logging.INFO, "gpt loaded.")if decoder_config_path:cfg = OmegaConf.load(decoder_config_path)decoder = DVAE(**cfg).to(device=device, dtype=dtype_decoder).eval()assert decoder_ckpt_path, "decoder_ckpt_path should not be None"decoder.load_state_dict(torch.load(decoder_ckpt_path, map_location=device))self.pretrain_models["decoder"] = decoder
142 unmodified lines143144145146147148149150151142 unmodified lineselse:vq_feats = inp.detach().clone()temp = torch.chunk(vq_feats, 2, dim=1) # flatten trick :)temp = torch.stack(temp, -1)vq_feats = temp.reshape(*temp.shape[:2], -1)vq_feats = vq_feats.transpose(1, 2)dec_out = self.decoder(input=vq_feats)142 unmodified lines143144145146147148149150151142 unmodified lineselse:vq_feats = inp.detach().clone()vq_feats = vq_feats.view((vq_feats.size(0), 2, vq_feats.size(1)//2, vq_feats.size(2)),).permute(0, 2, 3, 1).flatten(2)vq_feats = vq_feats.transpose(1, 2)dec_out = self.decoder(input=vq_feats)
189 unmodified lines19019119219319419554 unmodified lines250251252253254255256257258189 unmodified linesattention_mask_cache[:, :attention_mask.shape[1]] = attention_maskfor i in tqdm(range(max_new_token)):model_input = self.prepare_inputs_for_generation(inputs_ids,outputs.past_key_values if i!=0 else None,54 unmodified linesend_idx = end_idx + (~finish).int()if finish.all():breakinputs_ids = [inputs_ids[idx, start_idx: start_idx+i] for idx, i in enumerate(end_idx.int())]inputs_ids = [i[:, 0] for i in inputs_ids] if infer_text else inputs_ids189 unmodified lines19019119219319419519619754 unmodified lines252253254255256257189 unmodified linesattention_mask_cache[:, :attention_mask.shape[1]] = attention_maskfor i in tqdm(range(max_new_token)):if finish.all():continuemodel_input = self.prepare_inputs_for_generation(inputs_ids,outputs.past_key_values if i!=0 else None,54 unmodified linesend_idx = end_idx + (~finish).int()inputs_ids = [inputs_ids[idx, start_idx: start_idx+i] for idx, i in enumerate(end_idx.int())]inputs_ids = [i[:, 0] for i in inputs_ids] if infer_text else inputs_ids
15 unmodified lines161718192021222315 unmodified linesif free_memory_mb < min_memory:logger.log(logging.WARNING, f'GPU {selected_gpu} has {round(free_memory_mb, 2)} MB memory left.')device = torch.device('cpu')else:logger.log(logging.WARNING, f'No GPU found, use CPU instead')device = torch.device('cpu')return device15 unmodified lines1617181920212223242515 unmodified linesif free_memory_mb < min_memory:logger.log(logging.WARNING, f'GPU {selected_gpu} has {round(free_memory_mb, 2)} MB memory left.')device = torch.device('cpu')elif torch.backends.mps.is_available() and torch.backends.mps.is_built():device = torch.device('mps')else:logger.log(logging.WARNING, f'No GPU found, use CPU instead')device = torch.device('cpu')return device
100 unmodified lines10110210310410510610710822 unmodified lines131132133134135136137138139140141100 unmodified lines"!": ".","(": ",",")": ",",# '[': ',',# ']': ',',">": ",","<": ",","-": ",",22 unmodified lines">": ">","?": "?","@": "@",# '[': '[',"\\": "\",# ']': ']',"^": "^",# '_': '_',"`": "`","{": "{","|": "|",100 unmodified lines10110210310410510610710822 unmodified lines131132133134135136137138139140141100 unmodified lines"!": ".","(": ",",")": ",","[": ",","]": ",",">": ",","<": ",","-": ",",22 unmodified lines">": ">","?": "?","@": "@","[": "[","\\": "\","]": "]","^": "^","_": "_","`": "`","{": "{","|": "|",
123456204 unmodified lines211212213214215216217import numpy as npfrom pydub import AudioSegmentfrom typing import Any, List, Dictfrom scipy.io.wavfile import writeimport iofrom modules.utils.audio import time_stretch, pitch_shift204 unmodified linesreturn AudioSegment.from_file(byte_io, format="wav")def synthesize_segment(segment: Dict[str, Any]) -> AudioSegment | None:if "break" in segment:pause_segment = AudioSegment.silent(duration=segment["break"])return pause_segment123456204 unmodified lines211212213214215216217import numpy as npfrom pydub import AudioSegmentfrom typing import Any, List, Dict, Unionfrom scipy.io.wavfile import writeimport iofrom modules.utils.audio import time_stretch, pitch_shift204 unmodified linesreturn AudioSegment.from_file(byte_io, format="wav")def synthesize_segment(segment: Dict[str, Any]) -> Union[AudioSegment, None]:if "break" in segment:pause_segment = AudioSegment.silent(duration=segment["break"])return pause_segment
26 unmodified lines2728293031323326 unmodified linesdef __init__(self, no_docs=False, exclude_patterns=[]):self.app = FastAPI(title="ChatTTS Forge API",description="ChatTTS-Forge 是一个功能强大的文本转语音生成工具,支持通过类 SSML 语法生成丰富的音频长文本,并提供全面的 API 服务,适用于各种场景。\n\nChatTTS-Forge is a powerful text-to-speech generation tool that supports generating rich audio long texts through class SSML syntax\n\n https://github.com/lenML/ChatTTS-Forge",version="0.1.0",redoc_url=None if no_docs else "/redoc",docs_url=None if no_docs else "/docs",26 unmodified lines27282930313233343536373839404142434426 unmodified linesdef __init__(self, no_docs=False, exclude_patterns=[]):self.app = FastAPI(title="ChatTTS Forge API",description="""ChatTTS-Forge 是一个功能强大的文本转语音生成工具,支持通过类 SSML 语法生成丰富的音频长文本,并提供全面的 API 服务,适用于各种场景。<br/>ChatTTS-Forge is a powerful text-to-speech generation tool that supports generating rich audio long texts through class SSML syntax项目地址: [https://github.com/lenML/ChatTTS-Forge](https://github.com/lenML/ChatTTS-Forge)> 所有生成音频的 POST api都无法在此页面调试,调试建议使用 playground <br/>> All audio generation POST APIs cannot be debugged on this page, it is recommended to use playground for debugging> 如果你不熟悉本系统,建议从这个一键脚本开始,在colab中尝试一下:<br/>> [https://colab.research.google.com/github/lenML/ChatTTS-Forge/blob/main/colab.ipynb](https://colab.research.google.com/github/lenML/ChatTTS-Forge/blob/main/colab.ipynb)""",version="0.1.0",redoc_url=None if no_docs else "/redoc",docs_url=None if no_docs else "/docs",
29 unmodified lines303132333435124 unmodified lines16016116216316416529 unmodified linesclass VoiceSelectionParams(BaseModel):languageCode: str = "ZH-CN"name: str = "female2"style: str = ""temperature: float = 0.3124 unmodified linesdef setup(app: APIManager):app.post("/v1/google/text:synthesize", response_model=GoogleTextSynthesizeResponse)(google_text_synthesize)29 unmodified lines30313233343536124 unmodified lines16116216316416516616716816917017117217317417517617717829 unmodified linesclass VoiceSelectionParams(BaseModel):languageCode: str = "ZH-CN"name: str = "female2"style: str = ""temperature: float = 0.3124 unmodified linesdef setup(app: APIManager):app.post("/v1/text:synthesize",response_model=GoogleTextSynthesizeResponse,description="""google api document: <br/>[https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize](https://cloud.google.com/text-to-speech/docs/reference/rest/v1/text/synthesize)- 多个属性在本系统中无用仅仅是为了兼容google api- voice 中的 topP, topK, temperature 为本系统中的参数- voice.name 即 speaker name (或者speaker seed)- voice.seed 为 infer seed (可在webui中测试具体作用)- 编码格式影响的是 audioContent 的二进制格式,所以所有format都是返回带有base64数据的json""",)(google_text_synthesize)
1234567891011from modules.api import utils as api_utilsfrom modules.api.Api import APIManagerfrom modules.models import reload_chat_tts
def setup(app: APIManager): @app.get("/v1/models/reload", response_model=api_utils.BaseResponse) async def reload_models(): # Reload models reload_chat_tts() return api_utils.success_response("Models reloaded")27 unmodified lines282930313233343536373825 unmodified lines646566676869707135 unmodified lines10710810911011111227 unmodified linesmodel: str = "chattts-4w"voice: str = "female2"response_format: Literal["mp3", "wav"] = "mp3"speed: int = Field(1, ge=1, le=10, description="Speed of the audio")style: str = ""# 是否开启batch合成,小于等于1表示不适用batch# 开启batch合成会自动分割句子batch_size: int = Field(1, ge=1, le=10, description="Batch size")spliter_threshold: float = Field(100, ge=10, le=1024, description="Threshold for sentence spliter")25 unmodified linesparams = api_utils.calc_spk_style(spk=voice, style=style)spk = params.get("spk", -1)seed = params.get("seed", 42)temperature = params.get("temperature", 0.3)prompt1 = params.get("prompt1", "")prompt2 = params.get("prompt2", "")prefix = params.get("prefix", "")35 unmodified linesdef setup(api_manager: APIManager):api_manager.post("/v1/openai/audio/speech", response_class=FileResponse)(openai_speech_api)27 unmodified lines282930313233343536373825 unmodified lines646566676869707135 unmodified lines10710810911011111211311411511611711811912012112212312427 unmodified linesmodel: str = "chattts-4w"voice: str = "female2"response_format: Literal["mp3", "wav"] = "mp3"speed: float = Field(1, ge=0.1, le=10, description="Speed of the audio")style: str = ""# 是否开启batch合成,小于等于1表示不适用batch# 开启batch合成会自动分割句子batch_size: int = Field(1, ge=1, le=20, description="Batch size")spliter_threshold: float = Field(100, ge=10, le=1024, description="Threshold for sentence spliter")25 unmodified linesparams = api_utils.calc_spk_style(spk=voice, style=style)spk = params.get("spk", -1)seed = params.get("seed", request.seed or 42)temperature = params.get("temperature", request.temperature or 0.3)prompt1 = params.get("prompt1", "")prompt2 = params.get("prompt2", "")prefix = params.get("prefix", "")35 unmodified linesdef setup(api_manager: APIManager):api_manager.post("/v1/audio/speech",response_class=FileResponse,description="""openai api document:[https://platform.openai.com/docs/guides/text-to-speech](https://platform.openai.com/docs/guides/text-to-speech)以下属性为本系统自定义属性,不在openai文档中:- batch_size: 是否开启batch合成,小于等于1表示不使用batch (不推荐)- spliter_threshold: 开启batch合成时,句子分割的阈值- style: 风格> model 可填任意值""",)(openai_speech_api)
12345678from modules.api import utils as api_utilsfrom modules.api.Api import APIManager
def setup(app: APIManager): @app.get("/v1/ping", response_model=api_utils.BaseResponse) async def ping(): return {"message": "ok", "data": "pong"}1234530 unmodified lines3637383940419 unmodified lines51525354555657from pydantic import BaseModelfrom typing import Anyimport torch30 unmodified lines}def wav_to_mp3(wav_data, bitrate="48k"):audio = AudioSegment.from_wav(wav_data,9 unmodified linesreturn defaultdef calc_spk_style(spk: str | int, style: str | int):voice_attrs = {"spk": None,"seed": None,1234530 unmodified lines363738394041424344459 unmodified lines55565758596061from pydantic import BaseModelfrom typing import Any, Unionimport torch30 unmodified lines}def success_response(data: Any, message: str = "Success") -> BaseResponse:return BaseResponse(message=message, data=data)def wav_to_mp3(wav_data, bitrate="48k"):audio = AudioSegment.from_wav(wav_data,9 unmodified linesreturn defaultdef calc_spk_style(spk: Union[str, int], style: Union[str, int]):voice_attrs = {"spk": None,"seed": None,
1234567891011enable_model_compile = Falselru_size = 64args = {}api = Nonemodel_config = {"half": False}disable_tqdm = False12345from modules.utils.JsonObject import JsonObjectruntime_env_vars = JsonObject({})api = None
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160from functools import lru_cacheimport sysimport torchfrom modules import config
import logging
logger = logging.getLogger(__name__)
if sys.platform == "darwin": from modules.devices import mac_devices
def has_mps() -> bool: if sys.platform != "darwin": return False else: return mac_devices.has_mps
def get_cuda_device_id(): return ( int(config.runtime_env_vars.device_id) if config.runtime_env_vars.device_id is not None and config.runtime_env_vars.device_id.isdigit() else 0 ) or torch.cuda.current_device()
def get_cuda_device_string(): if config.runtime_env_vars.device_id is not None: return f"cuda:{config.runtime_env_vars.device_id}"
return "cuda"
def get_available_gpus() -> list[tuple[int, int]]: """ Get the list of available GPUs and their free memory.
:return: A list of tuples where each tuple contains (GPU index, free memory in bytes). """ available_gpus = [] for i in range(torch.cuda.device_count()): props = torch.cuda.get_device_properties(i) free_memory = props.total_memory - torch.cuda.memory_reserved(i) available_gpus.append((i, free_memory)) return available_gpus
def get_memory_available_gpus(min_memory=2048): available_gpus = get_available_gpus() memory_available_gpus = [ gpu for gpu, free_memory in available_gpus if free_memory > min_memory ] return memory_available_gpus
def get_target_device_id_or_memory_available_gpu(): memory_available_gpus = get_memory_available_gpus() device_id = get_cuda_device_id() if device_id not in memory_available_gpus: if len(memory_available_gpus) != 0: logger.warning( f"Device {device_id} is not available or does not have enough memory. will try to use {memory_available_gpus}" ) config.runtime_env_vars.device_id = str(memory_available_gpus[0]) else: logger.warning( f"Device {device_id} is not available or does not have enough memory. Using CPU instead." ) return "cpu" return get_cuda_device_string()
def get_optimal_device_name(): if config.runtime_env_vars.use_cpu: return "cpu"
if torch.cuda.is_available(): return get_target_device_id_or_memory_available_gpu()
if has_mps(): return "mps"
return "cpu"
def get_optimal_device(): return torch.device(get_optimal_device_name())
def get_device_for(task): if task in config.cmd_opts.use_cpu or "all" in config.cmd_opts.use_cpu: return cpu
return get_optimal_device()
def torch_gc(): try: if torch.cuda.is_available(): with torch.cuda.device(get_cuda_device_string()): torch.cuda.empty_cache() torch.cuda.ipc_collect()
if has_mps(): mac_devices.torch_mps_gc() except Exception as e: logger.error(f"Error in torch_gc", exc_info=True)
cpu: torch.device = torch.device("cpu")device: torch.device = get_optimal_device()dtype: torch.dtype = torch.float32dtype_dvae: torch.dtype = torch.float32dtype_vocos: torch.dtype = torch.float32dtype_gpt: torch.dtype = torch.float32dtype_decoder: torch.dtype = torch.float32
def reset_device(): if config.runtime_env_vars.half: global dtype global dtype_dvae global dtype_vocos global dtype_gpt global dtype_decoder dtype = torch.float16 dtype_dvae = torch.float16 dtype_vocos = torch.float16 dtype_gpt = torch.float16 dtype_decoder = torch.float16
logger.info("Using half precision: torch.float16")
if ( config.runtime_env_vars.device_id is not None or config.runtime_env_vars.use_cpu is not None ): global device device = get_optimal_device()
logger.info(f"Using device: {device}")
@lru_cachedef first_time_calculation(): """ just do any calculation with pytorch layers - the first time this is done it allocaltes about 700MB of memory and spends about 2.7 seconds doing that, at least wih NVidia. """
x = torch.zeros((1, 1)).to(device, dtype) linear = torch.nn.Linear(1, 1).to(device, dtype) linear(x)
x = torch.zeros((1, 1, 3, 3)).to(device, dtype) conv2d = torch.nn.Conv2d(1, 1, (3, 3)).to(device, dtype) conv2d(x)123456789101112131415161718192021222324252627282930313233343536373839404142import torchimport loggingfrom packaging import versionimport torch.backendsimport torch.backends.mps
logger = logging.getLogger(__name__)
def check_for_mps() -> bool: if version.parse(torch.__version__) <= version.parse("2.0.1"): if not getattr(torch, "has_mps", False): return False try: torch.zeros(1).to(torch.device("mps")) return True except Exception: return False else: try: return torch.backends.mps.is_available() and torch.backends.mps.is_built() except: logger.warning("MPS garbage collection failed", exc_info=True) return False
has_mps = check_for_mps()
def torch_mps_gc() -> None: try: from torch.mps import empty_cache
empty_cache() except Exception: logger.warning("MPS garbage collection failed", exc_info=True)
if __name__ == "__main__": print(torch.__version__) print(has_mps) torch_mps_gc()7 unmodified lines891011121314151617181920212223242522 unmodified lines4849505152535410 unmodified lines6566676869707131 unmodified lines1031041051061071087 unmodified linesimport loggingfrom modules import deviceslogger = logging.getLogger(__name__)@torch.inference_mode()def generate_audio(text: str,temperature: float = 0.3,top_P: float = 0.7,top_K: float = 20,spk: int | Speaker = -1,infer_seed: int = -1,use_decoder: bool = True,prompt1: str = "",22 unmodified linestemperature: float = 0.3,top_P: float = 0.7,top_K: float = 20,spk: int | Speaker = -1,infer_seed: int = -1,use_decoder: bool = True,prompt1: str = "",10 unmodified lines"prompt2": prompt2 or "","prefix": prefix or "","repetition_penalty": 1.0,"disable_tqdm": config.disable_tqdm,}if isinstance(spk, int):31 unmodified linesreturn [(sample_rate, np.array(wav).flatten().astype(np.float32)) for wav in wavs]if __name__ == "__main__":import soundfile as sf7 unmodified lines8910111213141516171819202122232425262722 unmodified lines5051525354555610 unmodified lines6768697071727331 unmodified lines1051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351367 unmodified linesimport loggingfrom modules.devices import devicesfrom typing import Unionfrom modules.utils.cache import conditional_cachelogger = logging.getLogger(__name__)def generate_audio(text: str,temperature: float = 0.3,top_P: float = 0.7,top_K: float = 20,spk: Union[int, Speaker] = -1,infer_seed: int = -1,use_decoder: bool = True,prompt1: str = "",22 unmodified linestemperature: float = 0.3,top_P: float = 0.7,top_K: float = 20,spk: Union[int, Speaker] = -1,infer_seed: int = -1,use_decoder: bool = True,prompt1: str = "",10 unmodified lines"prompt2": prompt2 or "","prefix": prefix or "","repetition_penalty": 1.0,"disable_tqdm": config.runtime_env_vars.off_tqdm,}if isinstance(spk, int):31 unmodified linesreturn [(sample_rate, np.array(wav).flatten().astype(np.float32)) for wav in wavs]lru_cache_enabled = Falsedef setup_lru_cache():global generate_audio_batchglobal lru_cache_enabledif lru_cache_enabled:returnlru_cache_enabled = Truedef should_cache(*args, **kwargs):spk_seed = kwargs.get("spk", -1)infer_seed = kwargs.get("infer_seed", -1)return spk_seed != -1 and infer_seed != -1lru_size = config.runtime_env_vars.lru_sizeif isinstance(lru_size, int):generate_audio_batch = conditional_cache(lru_size, should_cache)(generate_audio_batch)logger.info(f"LRU cache enabled with size {lru_size}")else:logger.debug(f"LRU cache failed to enable, invalid size {lru_size}")if __name__ == "__main__":import soundfile as sf
1234567891011121314151 unmodified line17181920212223242526272829303132333435363738394041from modules.ChatTTS import ChatTTSimport torchfrom modules import configimport logginglogger = logging.getLogger(__name__)device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")print(f"device use {device}")chat_tts = None1 unmodified lineglobal chat_ttsif chat_tts:return chat_ttschat_tts = ChatTTS.Chat()chat_tts.load_models(compile=config.enable_model_compile,source="local",local_path="./models/ChatTTS",device=device,)if config.model_config.get("half", False):logging.info("half precision enabled")for model_name, model in chat_tts.pretrain_models.items():if isinstance(model, torch.nn.Module):model.cpu()if torch.cuda.is_available():torch.cuda.empty_cache()model.half()if torch.cuda.is_available():model.cuda()model.eval()logger.log(logging.INFO, f"{model_name} converted to half precision.")return chat_tts12345678910111 unmodified line131415161718192021222324252627282930313233343536373839404142434445import torchfrom modules.ChatTTS import ChatTTSfrom modules import configfrom modules.devices import devicesimport logginglogger = logging.getLogger(__name__)chat_tts = None1 unmodified lineglobal chat_ttsif chat_tts:return chat_ttschat_tts = ChatTTS.Chat()chat_tts.load_models(compile=config.runtime_env_vars.compile,source="local",local_path="./models/ChatTTS",device=devices.device,dtype=devices.dtype,dtype_vocos=devices.dtype_vocos,dtype_dvae=devices.dtype_dvae,dtype_gpt=devices.dtype_gpt,dtype_decoder=devices.dtype_decoder,)devices.torch_gc()return chat_ttsdef reload_chat_tts():logging.info("Reloading ChatTTS models")global chat_ttsif chat_tts:if torch.cuda.is_available():for model_name, model in chat_tts.pretrain_models.items():if isinstance(model, torch.nn.Module):model.cpu()torch.cuda.empty_cache()chat_tts = Nonereturn load_chat_tts()
12345680 unmodified lines87888990919293949596979810 unmodified lines1091101111121131141156 unmodified lines12212312412512612758 unmodified lines18618718818919019119251 unmodified lines244245246247248249from modules.utils.zh_normalization.text_normlization import *import emojiswitchfrom modules.utils.markdown import markdown_to_textpost_normalize_pipeline = []pre_normalize_pipeline = []80 unmodified lines">": ",","<": ",","-": ",",}character_to_word = {" & ": " and ",}@post_normalize()def apply_character_to_word(text):10 unmodified lines@post_normalize()def apply_emoji_map(text):return emojiswitch.demojize(text, delimiters=("", ""), lang="zh")@post_normalize()6 unmodified lines)@pre_normalize()def apply_markdown_to_text(text):if is_markdown(text):58 unmodified linespattern = re.compile(r"(\[.+?\])|([^[]+)")def normalize_part(part):sentences = tx.normalize(part)dest_text = ""for sentence in sentences:sentence = apply_post_normalize(sentence)51 unmodified lines“我们是玫瑰花。”花儿们说道。“啊!”小王子说……。""",]for i, test_case in enumerate(test_cases):12345678910111213141580 unmodified lines9697989910010110210310410510610710810911011111210 unmodified lines1231241251261271281291306 unmodified lines13713813914014114214314414514614714814915015115215315415515615715815916016116258 unmodified lines22122222322422522622751 unmodified lines279280281282283284285286287288289290291292293294from modules.utils.zh_normalization.text_normlization import *import emojiswitchfrom modules.utils.markdown import markdown_to_textfrom modules import modelsimport redef is_chinese(text):# 中文字符的 Unicode 范围是 \u4e00-\u9fffchinese_pattern = re.compile(r"[\u4e00-\u9fff]")return bool(chinese_pattern.search(text))post_normalize_pipeline = []pre_normalize_pipeline = []80 unmodified lines">": ",","<": ",","-": ",","~": " ","~": " ","/": " ",}character_to_word = {" & ": " and ",}## ---------- post normalize ----------@post_normalize()def apply_character_to_word(text):10 unmodified lines@post_normalize()def apply_emoji_map(text):lang = "zh" if is_chinese(text) else "en"return emojiswitch.demojize(text, delimiters=("", ""), lang=lang)@post_normalize()6 unmodified lines)@post_normalize()def replace_unk_tokens(text):"""把不在字典里的字符替换为 " , """"chat_tts = models.load_chat_tts()tokenizer = chat_tts.pretrain_models["tokenizer"]vocab = tokenizer.get_vocab()vocab_set = set(vocab.keys())# 添加所有英语字符vocab_set.update(set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))vocab_set.update(set(" \n\r\t"))replaced_chars = [char if char in vocab_set else " , " for char in text]output_text = "".join(replaced_chars)return output_text## ---------- pre normalize ----------@pre_normalize()def apply_markdown_to_text(text):if is_markdown(text):58 unmodified linespattern = re.compile(r"(\[.+?\])|([^[]+)")def normalize_part(part):sentences = tx.normalize(part) if is_chinese(part) else [part]dest_text = ""for sentence in sentences:sentence = apply_post_normalize(sentence)51 unmodified lines“我们是玫瑰花。”花儿们说道。“啊!”小王子说……。""","""State-of-the-art Machine Learning for PyTorch, TensorFlow, and JAX.🤗 Transformers provides APIs and tools to easily download and train state-of-the-art pretrained models. Using pretrained models can reduce your compute costs, carbon footprint, and save you the time and resources required to train a model from scratch. These models support common tasks in different modalities, such as:📝 Natural Language Processing: text classification, named entity recognition, question answering, language modeling, summarization, translation, multiple choice, and text generation.🖼️ Computer Vision: image classification, object detection, and segmentation.🗣️ Audio: automatic speech recognition and audio classification.🐙 Multimodal: table question answering, optical character recognition, information extraction from scanned documents, video classification, and visual question answering.""",]for i, test_case in enumerate(test_cases):
28 unmodified lines2930313233343528 unmodified lines"temperature": temperature,"repetition_penalty": repetition_penalty,"max_new_token": max_new_token,"disable_tqdm": config.disable_tqdm,},do_text_normalization=False,)28 unmodified lines2930313233343528 unmodified lines"temperature": temperature,"repetition_penalty": repetition_penalty,"max_new_token": max_new_token,"disable_tqdm": config.runtime_env_vars.off_tqdm,},do_text_normalization=False,)
123448 unmodified lines53545556575846 unmodified lines105106107108109110111112113114115116117import osimport torchfrom modules import models48 unmodified linesreturn is_update# 每个speaker就是一个 emb 文件 .pt# 管理 speaker 就是管理 ./data/speaker/ 下的所有 speaker46 unmodified linesself.refresh_speakers()return speakerdef get_speaker(self, name) -> Speaker | None:for speaker in self.speakers.values():if speaker.name == name:return speakerreturn Nonedef get_speaker_by_id(self, id) -> Speaker | None:for speaker in self.speakers.values():if str(speaker.id) == str(id):return speaker1234548 unmodified lines545556575859606162636465666746 unmodified lines114115116117118119120121122123124125126import osfrom typing import Unionimport torchfrom modules import models48 unmodified linesreturn is_updatedef __hash__(self):return hash(str(self.id))def __eq__(self, other):if not isinstance(other, Speaker):return Falsereturn str(self.id) == str(other.id)# 每个speaker就是一个 emb 文件 .pt# 管理 speaker 就是管理 ./data/speaker/ 下的所有 speaker46 unmodified linesself.refresh_speakers()return speakerdef get_speaker(self, name) -> Union[Speaker, None]:for speaker in self.speakers.values():if speaker.name == name:return speakerreturn Nonedef get_speaker_by_id(self, id) -> Union[Speaker, None]:for speaker in self.speakers.values():if str(speaker.id) == str(id):return speaker
12349 unmodified lines14151617181920import iofrom modules.SentenceSplitter import SentenceSplitterfrom modules.SynthesizeSegments import SynthesizeSegments, combine_audio_segments9 unmodified linestemperature: float = 0.3,top_P: float = 0.7,top_K: float = 20,spk: int | Speaker = -1,infer_seed: int = -1,use_decoder: bool = True,prompt1: str = "",123459 unmodified lines15161718192021import iofrom typing import Unionfrom modules.SentenceSplitter import SentenceSplitterfrom modules.SynthesizeSegments import SynthesizeSegments, combine_audio_segments9 unmodified linestemperature: float = 0.3,top_P: float = 0.7,top_K: float = 20,spk: Union[int, Speaker] = -1,infer_seed: int = -1,use_decoder: bool = True,prompt1: str = "",
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113class JsonObject: def __init__(self, initial_dict=None): """ Initialize the JsonObject with an optional initial dictionary.
:param initial_dict: A dictionary to initialize the JsonObject. """ # If no initial dictionary is provided, use an empty dictionary self._dict_obj = initial_dict if initial_dict is not None else {}
def __getattr__(self, name): """ Get an attribute value. If the attribute does not exist, look it up in the internal dictionary.
:param name: The name of the attribute. :return: The value of the attribute. :raises AttributeError: If the attribute is not found in the dictionary. """ try: return self._dict_obj[name] except KeyError: return None
def __setattr__(self, name, value): """ Set an attribute value. If the attribute name is '_dict_obj', set it directly as an instance attribute. Otherwise, store it in the internal dictionary.
:param name: The name of the attribute. :param value: The value to set. """ if name == "_dict_obj": super().__setattr__(name, value) else: self._dict_obj[name] = value
def __delattr__(self, name): """ Delete an attribute. If the attribute does not exist, look it up in the internal dictionary and remove it.
:param name: The name of the attribute. :raises AttributeError: If the attribute is not found in the dictionary. """ try: del self._dict_obj[name] except KeyError: return
def __getitem__(self, key): """ Get an item value from the internal dictionary.
:param key: The key of the item. :return: The value of the item. :raises KeyError: If the key is not found in the dictionary. """ if key not in self._dict_obj: return None return self._dict_obj[key]
def __setitem__(self, key, value): """ Set an item value in the internal dictionary.
:param key: The key of the item. :param value: The value to set. """ self._dict_obj[key] = value
def __delitem__(self, key): """ Delete an item from the internal dictionary.
:param key: The key of the item. :raises KeyError: If the key is not found in the dictionary. """ del self._dict_obj[key]
def to_dict(self): """ Convert the JsonObject back to a regular dictionary.
:return: The internal dictionary. """ return self._dict_obj
def has_key(self, key): """ Check if the key exists in the internal dictionary.
:param key: The key to check. :return: True if the key exists, False otherwise. """ return key in self._dict_obj
def keys(self): """ Get a list of keys in the internal dictionary.
:return: A list of keys. """ return self._dict_obj.keys()
def values(self): """ Get a list of values in the internal dictionary.
:return: A list of values. """ return self._dict_obj.values()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192from typing import Callable, TypeVar, Anyfrom typing_extensions import ParamSpec
from functools import lru_cache, _CacheInfo
def conditional_cache(maxsize: int, condition: Callable): def decorator(func): @lru_cache_ext(maxsize=maxsize) def cached_func(*args, **kwargs): return func(*args, **kwargs)
def wrapper(*args, **kwargs): if condition(*args, **kwargs): return cached_func(*args, **kwargs) else: return func(*args, **kwargs)
return wrapper
return decorator
def hash_list(l: list) -> int: __hash = 0 for i, e in enumerate(l): __hash = hash((__hash, i, hash_item(e))) return __hash
def hash_dict(d: dict) -> int: __hash = 0 for k, v in d.items(): __hash = hash((__hash, k, hash_item(v))) return __hash
def hash_item(e) -> int: if hasattr(e, "__hash__") and callable(e.__hash__): try: return hash(e) except TypeError: pass if isinstance(e, (list, set, tuple)): return hash_list(list(e)) elif isinstance(e, (dict)): return hash_dict(e) else: raise TypeError(f"unhashable type: {e.__class__}")
PT = ParamSpec("PT")RT = TypeVar("RT")
def lru_cache_ext( *opts, hashfunc: Callable[..., int] = hash_item, **kwopts) -> Callable[[Callable[PT, RT]], Callable[PT, RT]]: def decorator(func: Callable[PT, RT]) -> Callable[PT, RT]: class _lru_cache_ext_wrapper: args: tuple kwargs: dict[str, Any]
def cache_info(self) -> _CacheInfo: ... def cache_clear(self) -> None: ...
@classmethod @lru_cache(*opts, **kwopts) def cached_func(cls, args_hash: int) -> RT: return func(*cls.args, **cls.kwargs)
@classmethod def __call__(cls, *args: PT.args, **kwargs: PT.kwargs) -> RT: __hash = hashfunc( ( id(func), *[hashfunc(a) for a in args], *[(hashfunc(k), hashfunc(v)) for k, v in kwargs.items()], ) )
cls.args = args cls.kwargs = kwargs
cls.cache_info = cls.cached_func.cache_info cls.cache_clear = cls.cached_func.cache_clear
return cls.cached_func(__hash)
return _lru_cache_ext_wrapper()
return decorator71 unmodified lines72737475767778798071 unmodified linesreturn sentencesdef _post_replace(self, sentence: str) -> str:sentence = sentence.replace('/', '每')sentence = sentence.replace('~', '至')sentence = sentence.replace('~', '至')sentence = sentence.replace('①', '一')sentence = sentence.replace('②', '二')sentence = sentence.replace('③', '三')71 unmodified lines72737475767778798071 unmodified linesreturn sentencesdef _post_replace(self, sentence: str) -> str:# sentence = sentence.replace('/', '每')# sentence = sentence.replace('~', '至')# sentence = sentence.replace('~', '至')sentence = sentence.replace('①', '一')sentence = sentence.replace('②', '二')sentence = sentence.replace('③', '三')
13 unmodified lines1415161718192021222 unmodified lines2526272829303132333435363738394041424344102 unmodified lines147148149150151152153715 unmodified lines86987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889913 unmodified linesimport osimport loggingfrom numpy import clipfrom modules.synthesize_audio import synthesize_audiologging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"),2 unmodified linesimport gradio as grimport ioimport reimport numpy as npimport torchfrom modules.ssml import parse_ssmlfrom modules.SynthesizeSegments import SynthesizeSegments, combine_audio_segmentsfrom modules.generate_audio import generate_audio, generate_audio_batchfrom modules.speaker import speaker_mgrfrom modules.data import styles_mgrfrom modules.api.utils import calc_spk_stylefrom modules.normalization import text_normalizefrom modules import refiner, config102 unmodified linesprompt1 = prompt1 or params.get("prompt1", "")prompt2 = prompt2 or params.get("prompt2", "")infer_seed = clip(infer_seed, -1, 2**32 - 1)infer_seed = int(infer_seed)if not disable_normalize:715 unmodified linestype=int,help="Max batch size for TTS",)args = parser.parse_args()server_name = env.get_env_or_arg(args, "server_name", "0.0.0.0", str)server_port = env.get_env_or_arg(args, "server_port", 7860, int)share = env.get_env_or_arg(args, "share", False, bool)debug = env.get_env_or_arg(args, "debug", False, bool)auth = env.get_env_or_arg(args, "auth", None, str)half = env.get_env_or_arg(args, "half", False, bool)off_tqdm = env.get_env_or_arg(args, "off_tqdm", False, bool)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", 8, int)demo = create_interface()if auth:auth = tuple(auth.split(":"))if half:config.model_config["half"] = Trueif off_tqdm:config.disable_tqdm = Truedemo.queue().launch(server_name=server_name,13 unmodified lines14151617181920212223242 unmodified lines2728293031323334353637383940414243102 unmodified lines146147148149150151152715 unmodified lines86886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592613 unmodified linesimport osimport loggingimport numpy as npfrom modules.devices import devicesfrom modules.synthesize_audio import synthesize_audiofrom modules.utils.cache import conditional_cachelogging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"),2 unmodified linesimport gradio as grimport torchfrom modules.ssml import parse_ssmlfrom modules.SynthesizeSegments import SynthesizeSegments, combine_audio_segmentsfrom modules.speaker import speaker_mgrfrom modules.data import styles_mgrfrom modules.api.utils import calc_spk_styleimport modules.generate_audio as generatefrom modules.normalization import text_normalizefrom modules import refiner, config102 unmodified linesprompt1 = prompt1 or params.get("prompt1", "")prompt2 = prompt2 or params.get("prompt2", "")infer_seed = np.clip(infer_seed, -1, 2**32 - 1)infer_seed = int(infer_seed)if not disable_normalize:715 unmodified linestype=int,help="Max batch size for TTS",)parser.add_argument("--lru_size",type=int,default=64,help="Set the size of the request cache pool, set it to 0 will disable lru_cache",)parser.add_argument("--device_id",type=str,help="Select the default CUDA device to use (export CUDA_VISIBLE_DEVICES=0,1,etc might be needed before)",default=None,)parser.add_argument("--use_cpu",nargs="+",help="use CPU as torch device for specified modules",default=[],type=str.lower,)parser.add_argument("--compile", action="store_true", help="Enable model compile")args = parser.parse_args()def get_and_update_env(*args):val = env.get_env_or_arg(*args)key = args[1]config.runtime_env_vars[key] = valreturn valserver_name = get_and_update_env(args, "server_name", "0.0.0.0", str)server_port = get_and_update_env(args, "server_port", 7860, int)share = get_and_update_env(args, "share", False, bool)debug = get_and_update_env(args, "debug", False, bool)auth = get_and_update_env(args, "auth", None, str)half = get_and_update_env(args, "half", False, bool)off_tqdm = get_and_update_env(args, "off_tqdm", False, bool)lru_size = get_and_update_env(args, "lru_size", 64, int)device_id = get_and_update_env(args, "device_id", None, str)use_cpu = get_and_update_env(args, "use_cpu", [], list)compile = get_and_update_env(args, "compile", False, bool)webui_config["tts_max"] = get_and_update_env(args, "tts_max_len", 1000, int)webui_config["ssml_max"] = get_and_update_env(args, "ssml_max_len", 5000, int)webui_config["max_batch_size"] = get_and_update_env(args, "max_batch_size", 8, int)demo = create_interface()if auth:auth = tuple(auth.split(":"))generate.setup_lru_cache()devices.reset_device()devices.first_time_calculation()demo.queue().launch(server_name=server_name,
modules/ChatTTS/ChatTTS/core.py+21-5
modules/ChatTTS/ChatTTS/model/dvae.py+3-3
modules/ChatTTS/ChatTTS/model/gpt.py+2-3
modules/ChatTTS/ChatTTS/utils/gpu_utils.py+3-1
modules/ChatTTS/ChatTTS/utils/infer_utils.py+5-5
modules/SynthesizeSegments.py+2-2
modules/api/Api.py+12-1
modules/api/impl/google_api.py+16-3
modules/api/impl/models_api.pyAdded+11
modules/api/impl/openai_api.py+19-7
modules/api/impl/ping_api.pyAdded+8
modules/api/utils.py+6-2
modules/config.py+2-8
modules/devices/__init__.pyAdded
modules/devices/devices.pyAdded+160
modules/devices/mac_devices.pyAdded+42
modules/generate_audio.py+33-5
modules/models.py+24-20
modules/normalization.py+47-2
modules/refiner.py+1-1
modules/speaker.py+11-2
modules/synthesize_audio.py+2-1
modules/utils/JsonObject.pyAdded+113
modules/utils/cache.pyAdded+92
modules/utils/zh_normalization/text_normlization.py+3-3
webui.py+49-22