tiny-voca-gr / src /tiny_voca_gr /frontend.py
Konstantinos's picture
Super-squash branch 'main' using huggingface_hub
927ea15
Raw History Blame Contribute Delete
3.91 kB
"""Greek frontend configuration. Install before importing upstream model modules."""
import hashlib
import json
import re
import sys
import unicodedata
from dataclasses import dataclass
from functools import lru_cache
CONSONANTS = tuple("p b t d k ɡ f v θ ð s z m n ŋ l r ɾ j x ɣ ç ʝ ɲ ʎ c ɟ".split())
VOWELS = ("a", "e", "i", "o", "u")
SPECIALS = ("<pad>", "<bos>", "<eos>", "<sp>", "<unk>")
PUNCTUATION = (",", ".", "?", "!", ";", ":", '"', "'", "(", ")", "—", "…")
SYMBOLS = SPECIALS + PUNCTUATION + CONSONANTS + VOWELS
def normalize_text(text):
# Preserve tonos and dialytika. Greek semicolon/question mark -> question.
text = unicodedata.normalize("NFC", text)
text = text.translate(str.maketrans({";": "?", ";": "?", "·": ":", "·": ":",
"«": '"', "»": '"', "“": '"', "”": '"', "’": "'", "–": "—"}))
text = re.sub(r"\.{3,}", "…", text)
return " ".join(text.split())
def split_key(text):
return hashlib.sha256(normalize_text(text).casefold().encode()).hexdigest()
def install():
"""Configure upstream's tested IPA parser without its English phone fallbacks.
This is a process-local adapter. Always launch via tiny_voca_gr.run.
Inventory changes deliberately invalidate English checkpoints.
"""
existing = sys.modules.get("tiny_voca.text")
if existing is not None and getattr(existing, "_tiny_voca_gr_installed", False):
return existing
forbidden = {"tiny_voca.config", "tiny_voca.model", "tiny_voca.align", "tiny_voca.annotate",
"tiny_voca.data", "tiny_voca.train", "tiny_voca.infer"}
if forbidden.intersection(sys.modules):
raise RuntimeError("Install Greek frontend before importing tiny-voca model/config/alignment")
from tiny_voca import text
text.set_frontend("espeak")
text.FRONTENDS = ("espeak",)
text.ESPEAK_VOICE = "el"
text.FRONTEND_VERSION = 1001
text.CONSONANTS, text.VOWELS = CONSONANTS, VOWELS
text.PHONES = CONSONANTS + VOWELS
text.SYMBOLS = SYMBOLS
text.SYMBOL_TO_ID = {s: i for i, s in enumerate(SYMBOLS)}
text.PHONE_SET = frozenset(text.PHONES)
text.PHONE_IDS = frozenset(text.SYMBOL_TO_ID[p] for p in text.PHONES) | {text.SYMBOL_TO_ID[text.UNK]}
text.VOWEL_SET = frozenset(VOWELS)
text.VOICELESS = frozenset("p t k f θ s x ç c".split())
text.FALLBACK = {"g": ("ɡ",), "ts": ("t", "s"), "dz": ("d", "z")}
text.ABBREVIATIONS = set()
text.normalize_text = normalize_text
text.espeak_version = lru_cache(maxsize=1)(text.espeak_version)
# Include the actual G2P version so training and inference cannot silently drift.
base_identity = text.frontend_identity
def identity():
return {**base_identity(), "normalization": "greek-nfc-v1", **text.frontend_runtime()}
text.frontend_identity = identity
# Upstream evaluates Config() in TinyVoca.__init__'s default argument at
# import time. Passing --config alone cannot fix the English vocab default.
from tiny_voca import config as upstream_config
@dataclass(frozen=True)
class GreekConfig(upstream_config.Config):
speakers: tuple[str, ...] = ("M", "F")
vocab_size: int = len(SYMBOLS)
upstream_config.Config = GreekConfig
# Current upstream imports model from its package initializer before this
# adapter can configure text. Refresh the names model captured at import.
from tiny_voca import model as upstream_model
upstream_model.PHONE_IDS = text.PHONE_IDS
upstream_model.SYMBOL_TO_ID = text.SYMBOL_TO_ID
upstream_model.VOWELS = text.VOWELS
upstream_model.Config = GreekConfig
upstream_model.TinyVoca.__init__.__defaults__ = (GreekConfig(),)
text._tiny_voca_gr_installed = True
return text
def config():
return {"speakers": ["M", "F"], "vocab_size": len(SYMBOLS)}
if __name__ == "__main__":
print(json.dumps(config(), indent=2))