Spaces:
Sleeping
Sleeping
File size: 3,915 Bytes
a2ba6ce c22768e a2ba6ce c22768e 9302f36 c22768e b32ee69 c22768e 4b1af3f c22768e 9302f36 c22768e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 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()
@spaces.GPU
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()
|