Spaces:
Running on Zero
Running on Zero
Update inference_gradio.py
Browse files11 unmodified lines12131415161718192013 unmodified lines34353637383940414243444546474849505152535413 unmodified lines6869707172737415 unmodified lines909192939495969726 unmodified lines1241251261271281291301311321331343 unmodified lines13813914014114214314414514614714814915015115215326 unmodified lines18018118218318418518617 unmodified lines20420520620720820921021121221321421521621721821916 unmodified lines236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274206 unmodified lines481482483484485486487488489490491492493494495496101 unmodified lines59859960060160260360411 unmodified linesimport soundfile as sffrom pathlib import Pathfrom cached_path import cached_pathfrom lemas_tts.api import TTS# Global variablestts_api = None13 unmodified lines)REPO_ROOT = Path(__file__).resolve().parent# Local pretrained root (used when running from a repo / Space that bundles weights)# PRETRAINED_ROOT = str(REPO_ROOT / "pretrained_models")# HF location for pretrained assets (used as a fallback when local files are missing)PRETRAINED_ROOT = "hf://LEMAS-Project/LEMAS-TTS/pretrained_models"CKPTS_ROOT = os.path.join(PRETRAINED_ROOT, "ckpts")# 1) 指向你仓库里的 libespeak-ng.soESPEAK_LIB = os.path.join(PRETRAINED_ROOT, "espeak-ng-lib", "libespeak-ng.so")os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = str(ESPEAK_LIB)# 2) 指向你仓库里的 espeak-ng-dataESPEAK_DATA_DIR = os.path.join(PRETRAINED_ROOT, "espeak-ng-data")os.environ["ESPEAK_DATA_PATH"] = ESPEAK_DATA_DIRos.environ["ESPEAKNG_DATA_PATH"] = ESPEAK_DATA_DIRclass UVR5:13 unmodified linesmodel_path = os.path.join(model_dir, "Kim_Vocal_1.onnx")config_path = os.path.join(model_dir, "MDX-Net-Kim-Vocal1.json")configs = json.loads(open(config_path, "r", encoding="utf-8").read())model_data = ModelData(model_path=model_path,audio_path=model_dir,15 unmodified linesreturn output_audio.squeeze().T.numpy(), 44100denoise_model = UVR5(model_dir=cached_path(os.path.join(PRETRAINED_ROOT, "uvr5")),code_dir=cached_path(str(REPO_ROOT / "uvr5")),)def load_wav(audio_info, sr=16000, channel=1):26 unmodified linesdef get_checkpoints_project(project_name=None, is_gradio=True):"""Get available checkpoint files"""checkpoint_dir = [str(CKPTS_ROOT)]# Remote ckpt locations on HF (used if local ckpts are not present)remote_ckpts = {"multilingual_grl": f"{PRETRAINED_ROOT}/ckpts/multilingual_grl/multilingual_grl.safetensors","multilingual_prosody": f"{PRETRAINED_ROOT}/ckpts/multilingual_prosody/multilingual_prosody.safetensors",}if project_name is None:# Look for checkpoints in local directory3 unmodified linesfiles_checkpoints.extend(glob(os.path.join(path, "**/*.pt"), recursive=True))files_checkpoints.extend(glob(os.path.join(path, "**/*.safetensors"), recursive=True))break# Fallback: use HF ckptsif not files_checkpoints:files_checkpoints = list(remote_ckpts.values())else:if os.path.isdir(checkpoint_dir[0]):files_checkpoints = glob(os.path.join(checkpoint_dir[0], project_name, "*.pt"))files_checkpoints.extend(glob(os.path.join(checkpoint_dir[0], project_name, "*.safetensors")))else:ckpt = remote_ckpts.get(project_name)files_checkpoints = [ckpt] if ckpt is not None else []print("files_checkpoints:", project_name, files_checkpoints)# Separate pretrained and regular checkpointspretrained_checkpoints = [f for f in files_checkpoints if "pretrained_" in os.path.basename(f)]26 unmodified linesdef get_available_projects():"""Get available project names from data directory"""data_paths = [cached_path(str(PRETRAINED_ROOT / "data")),]project_list = []17 unmodified lines):global last_checkpoint, last_device, tts_api, last_ema# Resolve checkpoint path (local or HF)ckpt_path = file_checkpointif isinstance(ckpt_path, str) and ckpt_path.startswith("hf://"):try:ckpt_resolved = str(cached_path(ckpt_path))except Exception as e:traceback.print_exc()return None, f"Error downloading checkpoint: {str(e)}", ""else:ckpt_resolved = ckpt_pathif not os.path.isfile(ckpt_resolved):return None, "Checkpoint not found!", ""16 unmodified lines# Automatically enable prosody encoder when using the prosody checkpointuse_prosody_encoder = True if "prosody" in str(ckpt_resolved) else False# Resolve vocab file (local or HF)local_vocab = cached_path(str(PRETRAINED_ROOT / "data" / project / "vocab.txt"))if local_vocab.is_file():vocab_file = str(local_vocab)else:remote_vocab_map = {"multilingual_grl": cached_path(f"{PRETRAINED_ROOT}/data/multilingual_grl/vocab.txt"),"multilingual_prosody": cached_path(f"{PRETRAINED_ROOT}/data/multilingual_prosody/vocab.txt"),}remote_vocab = remote_vocab_map.get(project)if remote_vocab is None:return None, "Vocab file not found!", ""try:vocab_file = str(cached_path(remote_vocab))except Exception as e:traceback.print_exc()return None, f"Error downloading vocab: {str(e)}", ""# Resolve prosody encoder config & weightslocal_prosody_cfg = CKPTS_ROOT / "prosody_encoder" / "pretssel_cfg.json"local_prosody_ckpt = CKPTS_ROOT / "prosody_encoder" / "prosody_encoder_UnitY2.pt"if local_prosody_cfg.is_file():prosody_cfg_path = str(local_prosody_cfg)else:prosody_cfg_path = str(cached_path(f"{PRETRAINED_ROOT}/ckpts/prosody_encoder/pretssel_cfg.json"))if local_prosody_ckpt.is_file():prosody_ckpt_path = str(local_prosody_ckpt)else:prosody_ckpt_path = str(cached_path(f"{PRETRAINED_ROOT}/ckpts/prosody_encoder/prosody_encoder_UnitY2.pt"))try:tts_api = TTS(206 unmodified lines# Examplesdef _resolve_example(name: str) -> str:local = cached_path(os.path.join(PRETRAINED_ROOT, "data", "test_examples", name))if os.path.isfile(local):return localremote_map = {"en.wav": cached_path(os.path.join(PRETRAINED_ROOT, "data", "test_examples", "en.wav")),"es.wav": cached_path(os.path.join(PRETRAINED_ROOT, "data", "test_examples", "es.wav")),"pt.wav": cached_path(os.path.join(PRETRAINED_ROOT, "data", "test_examples", "pt.wav")),}url = remote_map.get(name)return str(cached_path(url)) if url is not None else ""examples = gr.Examples(examples=[101 unmodified linesserver_port=port,share=share,show_api=api,allowed_paths=[str(cached_path(os.path.join(PRETRAINED_ROOT, "data")))],)11 unmodified lines1213141516171813 unmodified lines32333435363738394041424344454613 unmodified lines606162636465666715 unmodified lines838485868788899026 unmodified lines1171181191201211223 unmodified lines12612712812913013113213313413513613726 unmodified lines16416516616716816917017 unmodified lines18818919019119219319419519616 unmodified lines213214215216217218219220221222223224225226227228229230231206 unmodified lines438439440441442443444445101 unmodified lines54754854955055155255311 unmodified linesimport soundfile as sffrom pathlib import Pathfrom lemas_tts.api import TTS, PRETRAINED_ROOT, CKPTS_ROOT# Global variablestts_api = None13 unmodified lines)REPO_ROOT = Path(__file__).resolve().parent# 1) 指向 `pretrained_models` 里的 libespeak-ng.so(本地路径)ESPEAK_LIB = Path(PRETRAINED_ROOT) / "espeak-ng-lib" / "libespeak-ng.so"os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = str(ESPEAK_LIB)# 2) 指向 `pretrained_models` 里的 espeak-ng-data(本地路径)ESPEAK_DATA_DIR = Path(PRETRAINED_ROOT) / "espeak-ng-data"os.environ["ESPEAK_DATA_PATH"] = str(ESPEAK_DATA_DIR)os.environ["ESPEAKNG_DATA_PATH"] = str(ESPEAK_DATA_DIR)class UVR5:13 unmodified linesmodel_path = os.path.join(model_dir, "Kim_Vocal_1.onnx")config_path = os.path.join(model_dir, "MDX-Net-Kim-Vocal1.json")with open(config_path, "r", encoding="utf-8") as f:configs = json.load(f)model_data = ModelData(model_path=model_path,audio_path=model_dir,15 unmodified linesreturn output_audio.squeeze().T.numpy(), 44100denoise_model = UVR5(model_dir=str(Path(PRETRAINED_ROOT) / "uvr5"),code_dir=str(REPO_ROOT / "uvr5"),)def load_wav(audio_info, sr=16000, channel=1):26 unmodified linesdef get_checkpoints_project(project_name=None, is_gradio=True):"""Get available checkpoint files"""checkpoint_dir = [str(CKPTS_ROOT)]if project_name is None:# Look for checkpoints in local directory3 unmodified linesfiles_checkpoints.extend(glob(os.path.join(path, "**/*.pt"), recursive=True))files_checkpoints.extend(glob(os.path.join(path, "**/*.safetensors"), recursive=True))breakelse:if os.path.isdir(checkpoint_dir[0]):files_checkpoints = glob(os.path.join(checkpoint_dir[0], project_name, "*.pt"))files_checkpoints.extend(glob(os.path.join(checkpoint_dir[0], project_name, "*.safetensors")))else:files_checkpoints = []print("files_checkpoints:", project_name, files_checkpoints)# Separate pretrained and regular checkpointspretrained_checkpoints = [f for f in files_checkpoints if "pretrained_" in os.path.basename(f)]26 unmodified linesdef get_available_projects():"""Get available project names from data directory"""data_paths = [str(Path(PRETRAINED_ROOT) / "data"),]project_list = []17 unmodified lines):global last_checkpoint, last_device, tts_api, last_ema# Resolve checkpoint path (local or HF-style, though we now rely on local PRETRAINED_ROOT)ckpt_path = file_checkpointckpt_resolved = ckpt_pathif not os.path.isfile(ckpt_resolved):return None, "Checkpoint not found!", ""16 unmodified lines# Automatically enable prosody encoder when using the prosody checkpointuse_prosody_encoder = True if "prosody" in str(ckpt_resolved) else False# Resolve vocab file (local)local_vocab = Path(PRETRAINED_ROOT) / "data" / project / "vocab.txt"if not local_vocab.is_file():return None, "Vocab file not found!", ""vocab_file = str(local_vocab)# Resolve prosody encoder config & weights (local)local_prosody_cfg = Path(CKPTS_ROOT) / "prosody_encoder" / "pretssel_cfg.json"local_prosody_ckpt = Path(CKPTS_ROOT) / "prosody_encoder" / "prosody_encoder_UnitY2.pt"if not local_prosody_cfg.is_file() or not local_prosody_ckpt.is_file():return None, "Prosody encoder files not found!", ""prosody_cfg_path = str(local_prosody_cfg)prosody_ckpt_path = str(local_prosody_ckpt)try:tts_api = TTS(206 unmodified lines# Examplesdef _resolve_example(name: str) -> str:local = Path(PRETRAINED_ROOT) / "data" / "test_examples" / namereturn str(local) if local.is_file() else ""examples = gr.Examples(examples=[101 unmodified linesserver_port=port,share=share,show_api=api,allowed_paths=[str(Path(PRETRAINED_ROOT) / "data")],)
inference_gradio.py+31-82