Spaces:
Sleeping
Sleeping
Download app.py from Pavanb/WavTTS_Inference: direct link, hf CLI and curl.
- Browser
- Download file 3.92 kB
-
https://huggingface.co/spaces/Pavanb/WavTTS_Inference/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pavanb/WavTTS_Inference/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pavanb/WavTTS_Inference/resolve/main/app.py
3.92 kB
| import sys | |
| import tempfile | |
| from importlib.resources import files | |
| from pathlib import Path | |
| ROOT_DIR = Path(__file__).resolve().parent | |
| SRC_DIR = ROOT_DIR / "src" | |
| if str(SRC_DIR) not in sys.path: | |
| sys.path.insert(0, str(SRC_DIR)) | |
| import gradio as gr | |
| import soundfile as sf | |
| import spaces | |
| from cached_path import cached_path | |
| from hydra.utils import get_class | |
| from omegaconf import OmegaConf | |
| from wavtts.infer.utils_infer import ( | |
| cfg_strength, | |
| cross_fade_duration, | |
| device, | |
| fix_duration, | |
| infer_process, | |
| load_model, | |
| nfe_step, | |
| preprocess_ref_audio_text, | |
| remove_silence_for_generated_wav, | |
| shift, | |
| speed, | |
| sway_sampling_coef, | |
| target_rms, | |
| timestep_mapping, | |
| timestep_power, | |
| ) | |
| DEFAULT_MODEL = "WavTTS" | |
| DEFAULT_CKPT_FILE = "hf://worstchan/WavTTS/model_1200000.pt" | |
| def load_wavtts_model(): | |
| model_cfg = OmegaConf.load(str(files("wavtts").joinpath(f"configs/{DEFAULT_MODEL}.yaml"))) | |
| model_cls = get_class(f"wavtts.model.{model_cfg.model.backbone}") | |
| ckpt_file = str(cached_path(DEFAULT_CKPT_FILE)) | |
| print(f"Using config {model_cfg.model.name} with checkpoint {ckpt_file}...") | |
| return load_model( | |
| model_cls, | |
| model_cfg.model.arch, | |
| ckpt_file, | |
| device=device, | |
| cfm_kwargs=getattr(model_cfg.model, "cfm", {}) or {}, | |
| waveform_kwargs=model_cfg.model.waveform, | |
| ) | |
| ema_model = load_wavtts_model() | |
| def run_tts(ref_audio, ref_text, gen_text, remove_silence=False): | |
| if not ref_audio: | |
| raise gr.Error("Please upload a reference audio file.") | |
| if not gen_text or not gen_text.strip(): | |
| raise gr.Error("Please enter the text to synthesize.") | |
| ref_audio, ref_text = preprocess_ref_audio_text(ref_audio, ref_text or "") | |
| audio_segment, final_sample_rate, _ = infer_process( | |
| ref_audio, | |
| ref_text, | |
| gen_text.strip(), | |
| ema_model, | |
| target_rms=target_rms, | |
| cross_fade_duration=cross_fade_duration, | |
| nfe_step=nfe_step, | |
| cfg_strength=cfg_strength, | |
| sway_sampling_coef=sway_sampling_coef, | |
| timestep_mapping=timestep_mapping, | |
| timestep_power=timestep_power, | |
| shift=shift, | |
| speed=speed, | |
| fix_duration=fix_duration, | |
| device=device, | |
| ) | |
| if audio_segment is None: | |
| raise gr.Error("No audio was generated. Please try a shorter text or a different reference audio.") | |
| output_wav_path = tempfile.mktemp(suffix=".wav") | |
| sf.write(output_wav_path, audio_segment, final_sample_rate) | |
| if remove_silence: | |
| remove_silence_for_generated_wav(output_wav_path) | |
| return output_wav_path | |
| demo = gr.Interface( | |
| fn=run_tts, | |
| inputs=[ | |
| gr.Audio(label="Reference Audio", type="filepath"), | |
| gr.Textbox( | |
| label="Reference Text", | |
| placeholder="Some call me nature, others call me mother nature.", | |
| lines=2, | |
| ), | |
| gr.Textbox( | |
| label="Generation Text", | |
| placeholder="The text you want WavTTS to synthesize.", | |
| lines=4, | |
| ), | |
| gr.Checkbox(label="Remove Silence from Output?", value=False), | |
| ], | |
| outputs=gr.Audio(label="Generated Speech", type="filepath"), | |
| title="🗣️ WavTTS Demo", | |
| description=( | |
| "Upload a short reference voice clip, provide its transcript, and generate speech in the same voice. " | |
| "Reference text is recommended; leaving it blank triggers automatic ASR and may be slower on first use." | |
| ), | |
| examples=[ | |
| [ | |
| str(files("wavtts").joinpath("infer/examples/basic_ref_en.wav")), | |
| "Some call me nature, others call me mother nature.", | |
| "I don't really care what you call me. I've been a silent spectator, watching species evolve, empires rise and fall.", | |
| False, | |
| ], | |
| ], | |
| cache_examples=False, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |