Download speak.py from RL10x/tiny-voca-hindi: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/RL10x/tiny-voca-hindi/resolve/main/speak.py
- Command line
-
hf download hf://RL10x/tiny-voca-hindi/speak.py
-
curl -L -o speak.py https://huggingface.co/RL10x/tiny-voca-hindi/resolve/main/speak.py
2.71 kB
| """Hindi text -> WAV with the PyTorch checkpoint. | |
| python speak.py "भारत एक विशाल देश है, जहाँ अनेक भाषाएँ बोली जाती हैं।" --out out.wav | |
| python speak.py "आज 15 अगस्त है और मौसम 32 डिग्री है।" --style happy --out happy.wav | |
| python speak.py --text-file story.txt --style conv --speed 1.1 --out story.wav | |
| Text is normalised first (numbers, dates, money, units and common English words -> Hindi words), then read sentence by | |
| sentence. Styles: neutral, conv, happy, sad, anger, fear, surprise, disgust. | |
| """ | |
| import argparse | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import soundfile as sf | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from hindi_text import normalize_hi # noqa: E402 | |
| from tiny_voca.infer import limit_peak, load_model, speak_text # noqa: E402 | |
| STYLES = ("neutral", "conv", "happy", "sad", "anger", "fear", "surprise", "disgust") | |
| def main(): | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("text", nargs="?", help="Hindi text (Devanagari)") | |
| ap.add_argument("--text-file", type=Path) | |
| ap.add_argument("--style", choices=STYLES, default="neutral") | |
| ap.add_argument("--speed", type=float, default=1.0, help="0.5 (slow) to 2 (fast)") | |
| here = Path(__file__).resolve().parent | |
| default = next((p for p in (here / "models" / "tiny_voca_hindi.pt", here / "tiny_voca_hindi.pt") if p.exists()), | |
| here / "models" / "tiny_voca_hindi.pt") | |
| ap.add_argument("--model", type=Path, default=default) | |
| ap.add_argument("--out", type=Path, default=Path("out.wav")) | |
| ap.add_argument("--device", default="cpu") | |
| a = ap.parse_args() | |
| text = a.text if a.text is not None else (a.text_file.read_text(encoding="utf-8") if a.text_file else None) | |
| if not text: | |
| ap.error("give the text or --text-file") | |
| if not a.model.exists(): | |
| sys.exit(f"{a.model} not found: run python download_models.py first") | |
| model = load_model(a.model, a.device) | |
| t0 = time.time() | |
| spoken = normalize_hi(text, english=True).replace("।", ".") | |
| wave, _ = speak_text(model, spoken, a.style, speed=a.speed, peak=None) | |
| wave = limit_peak(wave) | |
| seconds = len(wave) / model.config.sample_rate | |
| a.out.parent.mkdir(parents=True, exist_ok=True) | |
| sf.write(a.out, wave.numpy(), model.config.sample_rate, subtype="PCM_16") | |
| took = time.time() - t0 | |
| print(f"{a.out}: {seconds:.2f} s of audio in {took:.2f} s ({seconds / took:.0f}x real time)") | |
| if __name__ == "__main__": | |
| main() | |