Download code/src/lua_nova_v1/inference/cli.py from oProfM/LuaNovaV1: direct link, hf CLI and curl.
- Browser
- Download file 5.82 kB
-
https://huggingface.co/oProfM/LuaNovaV1/resolve/main/code/src/lua_nova_v1/inference/cli.py
- Command line
-
hf download hf://oProfM/LuaNovaV1/code/src/lua_nova_v1/inference/cli.py
-
curl -L -o cli.py https://huggingface.co/oProfM/LuaNovaV1/resolve/main/code/src/lua_nova_v1/inference/cli.py
5.82 kB
| """Command-line interface for Lua Nova V1 PyTorch & ONNX Runtime CPU synthesis.""" | |
| from __future__ import annotations | |
| import argparse | |
| from pathlib import Path | |
| import time | |
| import numpy as np | |
| from .onnx_engine import OnnxSynthesizer | |
| from .synthesizer import Synthesizer | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Synthesize 24 kHz PT-BR speech with LuaNovaBase CFM v5 + BigVGAN") | |
| parser.add_argument("--text", required=True, help="Input Portuguese text") | |
| parser.add_argument("--out", type=Path, required=True, help="Output .wav path") | |
| # PyTorch backend | |
| parser.add_argument("--cfm", type=Path, default=None, help="Path to CFM v5 checkpoint (.pt)") | |
| parser.add_argument( | |
| "--vocoder", | |
| default="nvidia/bigvgan_base_24khz_100band", | |
| help="Path to fine-tuned BigVGAN .pt checkpoint or Hugging Face model ID", | |
| ) | |
| parser.add_argument("--device", default="cpu") | |
| parser.add_argument("--steps", type=int, default=8) | |
| parser.add_argument("--solver", default="midpoint", choices=["euler", "midpoint"]) | |
| parser.add_argument("--temperature", type=float, default=0.35) | |
| parser.add_argument("--cfg-scale", type=float, default=1.0) | |
| parser.add_argument("--sway-coef", type=float, default=-0.5) | |
| parser.add_argument("--seed", type=int, default=0) | |
| # ONNX Runtime CPU backend | |
| parser.add_argument("--onnx-cfm", type=Path, default=None, help="Path to exported cfm_v5.onnx") | |
| parser.add_argument("--onnx-vocoder", type=Path, default=None, help="Path to exported bigvgan.onnx") | |
| parser.add_argument("--threads", type=int, default=None, help="ONNX Runtime intra-op CPU threads") | |
| # Shared & streaming options | |
| parser.add_argument("--voice", default="F1") | |
| parser.add_argument("--pitch-range", type=float, default=1.05) | |
| parser.add_argument("--speed", type=float, default=1.0) | |
| parser.add_argument("--no-normalize", action="store_true", help="Disable PT-BR text normalization") | |
| parser.add_argument("--no-postprocess", action="store_true", help="Disable RMS normalization and edge fades") | |
| parser.add_argument("--stream", action="store_true", help="Synthesize via chunked streaming and report TTFA") | |
| parser.add_argument("--chunk-frames", type=int, default=36, help="Mel frames per streamed vocoder chunk") | |
| args = parser.parse_args() | |
| use_onnx = args.onnx_cfm is not None or args.onnx_vocoder is not None | |
| if use_onnx: | |
| if args.onnx_cfm is None or args.onnx_vocoder is None: | |
| parser.error("both --onnx-cfm and --onnx-vocoder are required for ONNX inference") | |
| synth = OnnxSynthesizer(args.onnx_cfm, args.onnx_vocoder, threads=args.threads) | |
| t0 = time.perf_counter() | |
| if args.stream: | |
| chunks: list[np.ndarray] = [] | |
| ttfa_ms = None | |
| for chunk in synth.speak_stream( | |
| args.text, | |
| voice=args.voice, | |
| speed=args.speed, | |
| pitch_range=args.pitch_range, | |
| normalize=not args.no_normalize, | |
| chunk_frames=args.chunk_frames, | |
| ): | |
| if ttfa_ms is None: | |
| ttfa_ms = (time.perf_counter() - t0) * 1000.0 | |
| chunks.append(chunk) | |
| wav = np.concatenate(chunks, axis=0) if chunks else np.zeros(0, dtype=np.float32) | |
| else: | |
| ttfa_ms = None | |
| wav = synth.speak( | |
| args.text, | |
| voice=args.voice, | |
| speed=args.speed, | |
| pitch_range=args.pitch_range, | |
| normalize=not args.no_normalize, | |
| postprocess=not args.no_postprocess, | |
| ) | |
| else: | |
| if args.cfm is None: | |
| parser.error("provide --cfm (PyTorch) or --onnx-cfm/--onnx-vocoder (ONNX Runtime)") | |
| synth = Synthesizer.from_checkpoints(args.cfm, args.vocoder, device=args.device) | |
| t0 = time.perf_counter() | |
| if args.stream: | |
| chunks = [] | |
| ttfa_ms = None | |
| for chunk in synth.speak_stream( | |
| args.text, | |
| voice=args.voice, | |
| n_steps=args.steps, | |
| temperature=args.temperature, | |
| cfg_scale=args.cfg_scale, | |
| sway_coef=args.sway_coef, | |
| solver=args.solver, | |
| pitch_range=args.pitch_range, | |
| speed=args.speed, | |
| normalize=not args.no_normalize, | |
| seed=args.seed, | |
| chunk_frames=args.chunk_frames, | |
| ): | |
| if ttfa_ms is None: | |
| ttfa_ms = (time.perf_counter() - t0) * 1000.0 | |
| chunks.append(chunk) | |
| wav = np.concatenate(chunks, axis=0) if chunks else np.zeros(0, dtype=np.float32) | |
| else: | |
| ttfa_ms = None | |
| wav = synth.speak( | |
| args.text, | |
| voice=args.voice, | |
| n_steps=args.steps, | |
| temperature=args.temperature, | |
| cfg_scale=args.cfg_scale, | |
| sway_coef=args.sway_coef, | |
| solver=args.solver, | |
| pitch_range=args.pitch_range, | |
| speed=args.speed, | |
| normalize=not args.no_normalize, | |
| postprocess=not args.no_postprocess, | |
| seed=args.seed, | |
| ) | |
| elapsed = time.perf_counter() - t0 | |
| out_path = synth.save_wav(args.out, wav) | |
| dur_s = len(wav) / synth.sample_rate | |
| ttfa_info = f", TTFA={ttfa_ms:.1f}ms" if ttfa_ms is not None else "" | |
| print(f"Saved {out_path} ({dur_s:.2f}s audio in {elapsed:.3f}s, {dur_s / max(elapsed, 1e-6):.2f}x RT{ttfa_info})") | |
| if __name__ == "__main__": | |
| main() | |