Download synthesize.py from autotrust/TTS-V8: direct link, hf CLI and curl.
- Browser
- Download file 4.13 kB
-
https://huggingface.co/autotrust/TTS-V8/resolve/main/synthesize.py
- Command line
-
hf download hf://autotrust/TTS-V8/synthesize.py
-
curl -L -o synthesize.py https://huggingface.co/autotrust/TTS-V8/resolve/main/synthesize.py
4.13 kB
| #!/usr/bin/env python3 | |
| """Synthesize with the complete local V4 model (offline or bidirectional streaming), no downloads.""" | |
| import argparse | |
| import json | |
| import threading | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| import soundfile as sf | |
| ROOT = Path(__file__).resolve().parent | |
| def main(): | |
| voices = json.loads((ROOT / 'data/voices.json').read_text()) | |
| parser = argparse.ArgumentParser(description='完整V4模型:六音色中文语音合成(离线/双向流式),运行时离线') | |
| reference = parser.add_mutually_exclusive_group(required=True) | |
| reference.add_argument('--voice', choices=voices) | |
| reference.add_argument('--ref-audio', type=Path) | |
| parser.add_argument('--ref-text') | |
| text_source = parser.add_mutually_exclusive_group(required=True) | |
| text_source.add_argument('--text') | |
| text_source.add_argument('--text-file', type=Path) | |
| parser.add_argument('--output', type=Path, required=True) | |
| parser.add_argument('--stream', choices=['icl', 'xvec'], | |
| help='双向流式模式;不指定则为离线整句合成') | |
| parser.add_argument('--segment', choices=['sentence', 'token'], | |
| help='流式切分方式:sentence=按句(icl默认),token=逐token(xvec默认)') | |
| parser.add_argument('--cps', type=float, default=0, | |
| help='流式时模拟LLM逐字输入速度(字/秒);0表示一次性给出全文') | |
| parser.add_argument('--model-dir', type=Path, default=ROOT) | |
| parser.add_argument('--device', default='cuda:0') | |
| parser.add_argument('--seed', type=int, default=5000) | |
| parser.add_argument('--temperature', type=float, default=0.8) | |
| parser.add_argument('--speed', type=float, default=1.0, | |
| help='语速倍率(保持音高):1.0=原速,>1更快,<1更慢;可用范围0.5–2.0,建议0.8–1.3') | |
| parser.add_argument('--threads', type=int, default=2) | |
| args = parser.parse_args() | |
| if args.output.exists(): | |
| parser.error(f'输出文件已存在:{args.output}') | |
| text = args.text if args.text is not None else args.text_file.read_text(encoding='utf-8') | |
| if not text.strip(): | |
| parser.error('text不能为空') | |
| if args.ref_audio and not args.ref_text: | |
| parser.error('--ref-audio需要对应的--ref-text') | |
| if args.voice and args.ref_text: | |
| parser.error('--ref-text只用于自定义参考录音') | |
| from inference import TTSV4 | |
| model = TTSV4(args.model_dir, args.device, args.threads) | |
| info = '' | |
| if args.stream is None: | |
| audio, sample_rate = model.synthesize(text, voice=args.voice, ref_audio=args.ref_audio, ref_text=args.ref_text, | |
| seed=args.seed, temperature=args.temperature, speed=args.speed) | |
| else: | |
| session = model.stream(voice=args.voice, ref_audio=args.ref_audio, ref_text=args.ref_text, mode=args.stream, | |
| segment=args.segment, seed=args.seed, temperature=args.temperature, speed=args.speed) | |
| def feed(): | |
| if args.cps: | |
| for ch in text: | |
| session.push_text(ch) | |
| time.sleep(1 / args.cps) | |
| else: | |
| session.push_text(text) | |
| session.end() | |
| threading.Thread(target=feed, daemon=True).start() | |
| audio = np.concatenate(list(session.audio())) | |
| sample_rate = model.engine.sample_rate | |
| st = session.stats | |
| seg = args.segment or ('sentence' if args.stream == 'icl' else 'token') | |
| info = (f';流式{args.stream}/{seg},首包{1000 * (st.t_first_audio - st.t_first_text):.0f} ms(自首个文本起),' | |
| f'生成RTF {(st.t_done - st.t_prefill) / max(st.audio_s, 1e-6):.2f}') | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| with args.output.open('xb') as handle: | |
| sf.write(handle, audio, sample_rate, format='WAV', subtype='PCM_16') | |
| print(f'已保存:{args.output};{len(audio) / sample_rate:.2f}秒,{sample_rate} Hz,本地完整V4模型{info}') | |
| if __name__ == '__main__': | |
| main() | |