File size: 5,327 Bytes
79b2661 | 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 | """
Fast Inference Script for 10M Modern TTS Model.
Generates 24kHz HD Audio from input text in milliseconds.
"""
import os
import time
import argparse
import torch
from models.tts_engine import TTS10MEngine
from data.text_cleaner import TextTokenizer
from utils.checkpoint import load_checkpoint, find_checkpoint
from utils.audio_sanity import AudioSanityGuardian, save_audio
from utils.long_form import synthesize_long_text
from config import TTS10MConfig
def run_inference(
text: str,
checkpoint_path: str = None,
output_wav: str = "output.wav",
num_steps: int = None,
speed: float = 1.0,
cfg_scale: float = None,
temperature: float = None,
sway_sampling_coef: float = None,
device: str = "cpu",
allow_random_weights: bool = False,
):
cfg = TTS10MConfig()
steps = num_steps if num_steps is not None else cfg.model.default_ode_steps
guidance = cfg_scale if cfg_scale is not None else cfg.model.default_cfg_scale
temp = temperature if temperature is not None else cfg.model.default_temperature
sway = sway_sampling_coef if sway_sampling_coef is not None else cfg.model.sway_sampling_coef
tokenizer = TextTokenizer()
text_ids = tokenizer.text_to_ids(text)
if text_ids.numel() == 0:
raise ValueError("Input text is empty after cleaning. Please provide valid text.")
print("\n==========================================")
print(" 10M Modern TTS Inference Engine")
print("==========================================")
print(f" Input Text: \"{text}\"")
print(f" ODE Steps: {steps} (Optimal Transport CFM)")
print(f" Target Device: {device}")
target_device = torch.device(device)
model = TTS10MEngine(cfg.model, cfg.training).to(target_device)
checkpoint_path = checkpoint_path or find_checkpoint()
if checkpoint_path:
model = load_checkpoint(model, checkpoint_path, device=str(target_device))
elif not allow_random_weights:
raise FileNotFoundError("Trained acoustic checkpoint required. Supply --checkpoint or use --allow-random-weights for a diagnostic.")
else:
print("Note: Running with initialized weights (specify --checkpoint path/to/model.safetensors for trained audio).")
model.eval()
start_t = time.perf_counter()
wav_np = synthesize_long_text(
model=model,
tokenizer=tokenizer,
text=text,
device=target_device,
sample_rate=cfg.audio.sample_rate,
num_steps=steps,
speed=speed,
cfg_scale=guidance,
temperature=temp,
sway_sampling_coef=sway,
min_frames=2,
)
waveform = torch.from_numpy(wav_np)
latency_ms = (time.perf_counter() - start_t) * 1000
_, audio_msg = AudioSanityGuardian.check_audio_waveform(waveform, cfg.audio.sample_rate)
duration_s = waveform.shape[-1] / float(cfg.audio.sample_rate)
rtf = (latency_ms / 1000.0) / duration_s if duration_s > 0 else 0.0
print("\n--- Synthesis Metrics ---")
print(f" Latency: {latency_ms:.1f} ms")
print(f" Audio Duration: {duration_s:.2f} s")
print(f" Real-Time Factor (RTF): {rtf:.3f}x (< 1.0x means faster than real-time!)")
print(f" Audio Health: {audio_msg}")
save_audio(output_wav, waveform, cfg.audio.sample_rate)
print(f"\nSaved generated audio to: {os.path.abspath(output_wav)}\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate 24kHz audio from text using 10M TTS model")
parser.add_argument("--text", type=str, default="नमस्ते! यह हमारा नया हिंदी टेक्स्ट टू स्पीच मॉडल बोल रहा है।", help="Text to speak")
parser.add_argument("--checkpoint", type=str, default=None, help="Path to .safetensors checkpoint")
parser.add_argument("--output", type=str, default="output.wav", help="Path to save output .wav file")
parser.add_argument("--steps", type=int, default=48, help="Number of ODE steps (16-48 recommended)")
parser.add_argument("--speed", type=float, default=0.92, help="Speech rate / speed multiplier (0.92 prevents syllable drop)")
parser.add_argument("--cfg_scale", type=float, default=1.5, help="Classifier-Free Guidance scale (1.0=off, 1.5=recommended)")
parser.add_argument("--temperature", type=float, default=0.667, help="Prior noise temperature (0.5-0.7 reduces background hiss)")
parser.add_argument("--sway", type=float, default=-1.0, help="Sway sampling coefficient (-1.0=F5-TTS default, 0.0=uniform)")
parser.add_argument("--device", type=str, default="cpu", help="Device (cpu or cuda)")
parser.add_argument("--allow-random-weights", action="store_true", help="Run a diagnostic with untrained weights")
args = parser.parse_args()
run_inference(
text=args.text,
checkpoint_path=args.checkpoint,
output_wav=args.output,
num_steps=args.steps,
speed=args.speed,
cfg_scale=args.cfg_scale,
temperature=args.temperature,
sway_sampling_coef=args.sway,
device=args.device,
allow_random_weights=args.allow_random_weights,
)
|