Download inference.py from davitlive5/sonabo-v1: direct link, hf CLI and curl.
- Browser
- Download file 2.37 kB
-
https://huggingface.co/davitlive5/sonabo-v1/resolve/main/inference.py
- Command line
-
hf download hf://davitlive5/sonabo-v1/inference.py
-
curl -L -o inference.py https://huggingface.co/davitlive5/sonabo-v1/resolve/main/inference.py
2.37 kB
| """Armenian TTS inference example. | |
| usage: | |
| python inference.py --text "Բարև ձեզ։ Այսօր եղանակը արևոտ է։" --voice my_voice.wav --out out.wav | |
| python inference.py --file story.txt --voice my_voice.wav --out story.wav | |
| --voice: any 3-30 s clean recording of one speaker (wav/mp3). Only use voices you have permission to clone. | |
| """ | |
| import argparse, os, re, sys | |
| import numpy as np, soundfile as sf, torch | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from qwen_tts import Qwen3TTSModel | |
| from hy_norm import normalize | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--model", default=".") | |
| ap.add_argument("--text"); ap.add_argument("--file") | |
| ap.add_argument("--voice", required=True) | |
| ap.add_argument("--out", default="out.wav") | |
| ap.add_argument("--temperature", type=float, default=0.9) | |
| ap.add_argument("--batch", type=int, default=4) | |
| ap.add_argument("--device", default="cuda:0" if torch.cuda.is_available() else "cpu") | |
| a = ap.parse_args() | |
| text = a.text or open(a.file, encoding="utf-8").read() | |
| def split_sentences(t, max_chars=220): | |
| out = [] | |
| for p in re.split(r"(?<=[։:.!?;՞՜…])\s+|\n+", t.strip()): | |
| p = p.strip() | |
| while len(p) > max_chars: | |
| cut = max(p.rfind(",", 0, max_chars), p.rfind("՝", 0, max_chars), p.rfind(" ", 0, max_chars)) | |
| cut = cut if cut > 40 else max_chars | |
| out.append(p[:cut + 1].strip()); p = p[cut + 1:].strip() | |
| if p: out.append(p) | |
| return out | |
| dtype = torch.bfloat16 if a.device.startswith("cuda") else torch.float32 | |
| m = Qwen3TTSModel.from_pretrained(a.model, device_map=a.device, dtype=dtype) | |
| vp = m.create_voice_clone_prompt(ref_audio=a.voice, x_vector_only_mode=True) | |
| sents = split_sentences(normalize(text)) | |
| audio, sr = [], 24000 | |
| for i in range(0, len(sents), a.batch): | |
| b = sents[i:i + a.batch] | |
| wavs, sr = m.generate_voice_clone(text=b, language=["Auto"] * len(b), voice_clone_prompt=vp, | |
| non_streaming_mode=True, temperature=a.temperature, | |
| max_new_tokens=max(len(s) for s in b) * 2 + 60) | |
| for w in wavs: | |
| audio += [np.asarray(w, np.float32).reshape(-1), np.zeros(int(0.18 * sr), np.float32)] | |
| sf.write(a.out, np.concatenate(audio[:-1]), sr) | |
| print(f"wrote {a.out} ({sum(len(x) for x in audio[:-1]) / sr:.1f} s, {len(sents)} sentences)") | |