Download src/tiny_voca_gr/speak.py from ComputationalLinguisticsPlatform/tiny-voca-gr: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/ComputationalLinguisticsPlatform/tiny-voca-gr/resolve/main/src/tiny_voca_gr/speak.py
- Command line
-
hf download hf://ComputationalLinguisticsPlatform/tiny-voca-gr/src/tiny_voca_gr/speak.py
-
curl -L -o speak.py https://huggingface.co/ComputationalLinguisticsPlatform/tiny-voca-gr/resolve/main/src/tiny_voca_gr/speak.py
2.01 kB
| """Greek sentence handling and synthesis using a trained Greek checkpoint.""" | |
| import argparse | |
| from pathlib import Path | |
| import re | |
| from .frontend import install, normalize_text | |
| ABBREVIATIONS = frozenset(("κ", "κα", "κος", "κυρ", "δρ", "πχ", "δηλ", "αρ", "σελ")) | |
| def sentences(text): | |
| text = normalize_text(text) | |
| # Quotes stay attached to the preceding sentence; decimals remain intact. | |
| parts = re.split(r'(?<=[.!?…])\s+|(?<=[.!?…]["\)])\s+', text) | |
| result, pending = [], "" | |
| for part in parts: | |
| pending = (pending + " " + part).strip() | |
| word = re.search(r"([^\W\d_]+)\.$", pending) | |
| if word and (word[1].casefold() in ABBREVIATIONS or len(word[1]) == 1): | |
| continue | |
| if any(c.isalnum() for c in pending): | |
| result.append(pending) | |
| pending = "" | |
| if pending and any(c.isalnum() for c in pending): | |
| result.append(pending) | |
| return result | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--checkpoint", type=Path, required=True) | |
| parser.add_argument("--voice", choices=("M", "F"), required=True) | |
| parser.add_argument("--text", required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--device", default="cpu") | |
| args = parser.parse_args() | |
| from .backend import configure_environment | |
| configure_environment(args.device) | |
| install() | |
| from .backend import install as install_backend | |
| install_backend() | |
| from tiny_voca import infer | |
| import soundfile as sf | |
| infer.sentences = sentences | |
| infer.ENGLISH_ONLY = "Greek frontend produced no pronounceable text" | |
| model = infer.load_model(args.checkpoint, device=args.device) | |
| wave, _ = infer.speak_text(model, normalize_text(args.text), args.voice) | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| sf.write(args.output, wave.numpy(), model.config.sample_rate, subtype="PCM_16") | |
| if __name__ == "__main__": | |
| main() | |