File size: 4,772 Bytes
8d6afae | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | """Single-item inference CLI for OmniVoice.
Generates audio from a single text input using voice cloning,
voice design, or auto voice.
Usage:
# Voice cloning
omnivoice-infer --model k2-fsa/OmniVoice \
--text "Hello, this is a text for text-to-speech." \
--ref_audio ref.wav --ref_text "Reference transcript." --output out.wav
# Voice design
omnivoice-infer --model k2-fsa/OmniVoice \
--text "Hello, this is a text for text-to-speech." \
--instruct "male, British accent" --output out.wav
# Auto voice
omnivoice-infer --model k2-fsa/OmniVoice \
--text "Hello, this is a text for text-to-speech." --output out.wav
"""
import argparse
import logging
import torch
import soundfile as sf
from omnivoice.models.omnivoice import OmniVoice
from omnivoice.utils.common import str2bool
def get_best_device():
"""Auto-detect the best available device: CUDA > MPS > CPU."""
if torch.cuda.is_available():
return "cuda"
if torch.backends.mps.is_available():
return "mps"
return "cpu"
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="OmniVoice single-item inference",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--model",
type=str,
default="k2-fsa/OmniVoice",
help="Model checkpoint path or HuggingFace repo id.",
)
parser.add_argument(
"--text",
type=str,
required=True,
help="Text to synthesize.",
)
parser.add_argument(
"--output",
type=str,
required=True,
help="Output WAV file path.",
)
# Voice cloning
parser.add_argument(
"--ref_audio",
type=str,
default=None,
help="Reference audio file path for voice cloning.",
)
parser.add_argument(
"--ref_text",
type=str,
default=None,
help="Reference text describing the reference audio.",
)
# Voice design
parser.add_argument(
"--instruct",
type=str,
default=None,
help="Style instruction for voice design mode.",
)
parser.add_argument(
"--language",
type=str,
default=None,
help="Language name (e.g. 'English') or code (e.g. 'en').",
)
# Generation parameters
parser.add_argument("--num_step", type=int, default=32)
parser.add_argument("--guidance_scale", type=float, default=2.0)
parser.add_argument("--speed", type=float, default=1.0)
parser.add_argument(
"--duration",
type=float,
default=None,
help="Fixed output duration in seconds. If set, overrides the "
"model's duration estimation. The speed factor is automatically "
"adjusted to match while preserving language-aware pacing.",
)
parser.add_argument("--t_shift", type=float, default=0.1)
parser.add_argument("--denoise", type=str2bool, default=True)
parser.add_argument(
"--postprocess_output",
type=str2bool,
default=True,
)
parser.add_argument("--layer_penalty_factor", type=float, default=5.0)
parser.add_argument("--position_temperature", type=float, default=5.0)
parser.add_argument("--class_temperature", type=float, default=0.0)
parser.add_argument(
"--device",
type=str,
default=None,
help="Device to use for inference. Auto-detected if not specified.",
)
return parser
def main():
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
logging.basicConfig(format=formatter, level=logging.INFO, force=True)
args = get_parser().parse_args()
device = args.device or get_best_device()
logging.info(f"Loading model from {args.model} on {device} ...")
model = OmniVoice.from_pretrained(
args.model, device_map=device, dtype=torch.float16
)
logging.info(f"Generating audio for: {args.text[:80]}...")
audios = model.generate(
text=args.text,
language=args.language,
ref_audio=args.ref_audio,
ref_text=args.ref_text,
instruct=args.instruct,
duration=args.duration,
num_step=args.num_step,
guidance_scale=args.guidance_scale,
speed=args.speed,
t_shift=args.t_shift,
denoise=args.denoise,
postprocess_output=args.postprocess_output,
layer_penalty_factor=args.layer_penalty_factor,
position_temperature=args.position_temperature,
class_temperature=args.class_temperature,
)
sf.write(args.output, audios[0], model.sampling_rate)
logging.info(f"Saved to {args.output}")
if __name__ == "__main__":
main()
|