File size: 2,006 Bytes
927ea15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """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()
|