Download examples/generate.py from neugi-com/swarai: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/neugi-com/swarai/resolve/main/examples/generate.py
- Command line
-
hf download hf://neugi-com/swarai/examples/generate.py
-
curl -L -o generate.py https://huggingface.co/neugi-com/swarai/resolve/main/examples/generate.py
2.62 kB
| #!/usr/bin/env python3 | |
| """SWARAI — local inference example. | |
| Loads the SWARAI model directory (full fine-tune of Qwen3-TTS-0.6B-Base) | |
| and synthesizes Indonesian speech with the single built-in speaker | |
| "swarai". | |
| Requirements: | |
| pip install -U qwen-tts soundfile torch | |
| # GPU recommended (bf16, ~4 GB VRAM). CPU works but is slow. | |
| # Optional: pip install -U flash-attn --no-build-isolation | |
| Usage: | |
| python generate.py --model . \ | |
| --text "Halo, saya Suaray. Saya adalah model suara Bahasa Indonesia." \ | |
| --out swarai-demo.wav | |
| python generate.py --model . --text "Selamat pagi." \ | |
| --out pagi.wav --sample # sampling instead of greedy | |
| Notes: | |
| - The model speaks with ONE built-in voice ("swarai"). It is not a general | |
| voice cloner: there is no reference-audio input in this path. The base | |
| Qwen3-TTS cloning capability is not exposed by this fine-tuned CustomVoice | |
| model. | |
| - The voice is the X-lord narrator (Dewi Anggreini reading "Ronggeng Dukuh | |
| Paruk"). It is NOT a LibriVox voice. LibriVox readers 2814/3205/3232 were | |
| used only for small separate probe fine-tunes during development. | |
| - Indonesian text works with the default language handling; the model | |
| was fine-tuned on normalized modern Indonesian (EYD). | |
| - Pronunciation: write "SWARAI" as "Suaray" and "neugi.com" as | |
| "niugi dot kom" in the input text if you want the brand names read | |
| the way the author pronounces them. | |
| """ | |
| import argparse | |
| import soundfile as sf | |
| import torch | |
| from qwen_tts import Qwen3TTSModel | |
| def main() -> None: | |
| ap = argparse.ArgumentParser(description="SWARAI local inference") | |
| ap.add_argument("--model", required=True, | |
| help="path to the SWARAI model directory") | |
| ap.add_argument("--text", required=True, help="text to synthesize") | |
| ap.add_argument("--out", default="swarai-output.wav", | |
| help="output wav path (24 kHz)") | |
| ap.add_argument("--sample", action="store_true", | |
| help="use sampling (do_sample=True) instead of greedy") | |
| ap.add_argument("--device", default="cuda:0", | |
| help='torch device, e.g. "cuda:0" or "cpu"') | |
| args = ap.parse_args() | |
| tts = Qwen3TTSModel.from_pretrained( | |
| args.model, | |
| device_map=args.device, | |
| dtype=torch.bfloat16, | |
| attn_implementation="sdpa", | |
| ) | |
| wavs, sr = tts.generate_custom_voice( | |
| text=args.text, | |
| speaker="swarai", | |
| do_sample=args.sample, | |
| ) | |
| sf.write(args.out, wavs[0], sr) | |
| print(f"saved {args.out} sr={sr} duration={len(wavs[0]) / sr:.2f}s") | |
| if __name__ == "__main__": | |
| main() | |