Audiction / normalize.py
Frosty40's picture
Audiction: text normalizer for TTS (rules + byt5-small fine-tune, hybrid)
5c2ad89
Raw History Blame Contribute Delete
7.93 kB
r"""Audiction: written text in, what a TTS voice should say out.
python3 normalize.py --model . "Doors open at 7:30 pm on 11/21."
echo "Tickets are \$25." | python3 normalize.py --model .
How a sentence is handled:
1. The rules (verbalize.py) speak it when they're sure of every part. Exact, deterministic, microseconds.
2. Otherwise the model reads it, and its reading is kept only if it passes a check: no digits left, every word of
the source kept in order, and every added word one the rules themselves use (number words, units, months ...).
3. If the model's reading doesn't pass, the rules speak everything they're sure of and leave the rest as written.
"""
import argparse, os, re, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import verbalize as V
from verbalize import verbalize
HERE = os.path.dirname(os.path.abspath(__file__))
WORD = re.compile(r"[A-Za-z]+(?:['’][A-Za-z]+)?")
PLAIN = re.compile(r"[A-Za-z]+(?:['’][A-Za-z]+)?")
STRIP = ".,;:!?\"'()[]“”‘’"
def _spoken_words():
"""Every word the rules can add: number words, units, months, abbreviation expansions, letters."""
words = set("abcdefghijklmnopqrstuvwxyz")
for n in list(range(0, 1001)) + [10 ** 6, 10 ** 9, 10 ** 12]:
words |= set(re.findall(r"[a-z]+", V.cardinal(n)))
for n in range(1, 101):
o = V.ordinal(n)
words |= set(re.findall(r"[a-z]+", o)) | set(re.findall(r"[a-z]+", o + "s"))
for table in (V.UNITS, V.CURRENCY):
for v in table.values():
words |= {w.lower() for w in re.findall(r"[A-Za-z]+", " ".join(v))}
for table in (V.ABBR_PRE, V.ABBR_POST, V.ABBR_NUM, V.ABBR_ANY, V.JOINERS):
words |= {w.lower() for w in re.findall(r"[A-Za-z]+", " ".join(table.values()))}
words |= {m.lower() for m in V.MONTHS} | {d.lower() for d in V.WEEKDAYS}
words |= set("point and minus percent degree degrees oh to a an the number versus dot slash at hundred half halves "
"quarter quarters thousand million billion trillion www north south east west parts per o'clock "
"tens twenties thirties forties fifties sixties seventies eighties nineties hundreds thousands".split())
return words
SPOKEN = _spoken_words()
def _unit_triggers():
"""Unit and currency words, each with the source marks that justify it ("kilowatt" needs "kW" or "kWh")."""
trig = {}
for key, forms in V.UNITS.items():
for w in re.findall(r"[a-z]+", " ".join(forms).lower()):
if w not in ("per",):
trig.setdefault(w, set()).add(key)
for sym, forms in V.CURRENCY.items():
for w in forms:
trig.setdefault(w, set()).add(sym)
trig.setdefault("percent", set()).add("%")
for w in ("degree", "degrees"):
trig.setdefault(w, set()).add("°")
return trig
UNIT_TRIGGERS = _unit_triggers()
def _has_mark(src, mark):
if not mark[0].isalpha():
return mark in src
return re.search(r"(?<![A-Za-z])\d+\s?" + re.escape(mark) + r"(?![A-Za-z])", src) is not None
def _words(s):
return [w.lower() for w in WORD.findall(s)]
def _plain(s):
# letters-only source tokens; tokens glued to digits or symbols ("5K", "$5bn") are the model's to read
return [w.lower() for t in s.split() if (w := t.strip(STRIP)) and PLAIN.fullmatch(w)]
def _merged(ws):
"""Join runs of single letters, so an acronym said letter by letter ("c p m") matches its source word ("cpm")."""
out = []
for w in ws:
if len(w) == 1 and out and out[-1][1]:
out[-1] = (out[-1][0] + w, True)
else:
out.append((w, len(w) == 1))
return [w for w, _ in out]
def _in_order(need, hay):
it = iter(hay)
return all(any(w == h for h in it) for w in need)
REPEAT = re.compile(r"\b(\w+)(?:\s+\1\b){5,}")
RAW = re.compile(r"[\d$£€%°#&@/]")
def acceptable(src, out):
"""True when the model's reading keeps every source word, adds only spoken-form words, and leaves no digit."""
if not out or re.search(r"\d", out) or REPEAT.search(out) or len(out) > 3 * len(src) + 40:
return False
hay = _words(out)
partial = verbalize(src, partial=True)
# The model only settles short spans (a score, a fraction, "15K"). Serials, IDs, ISBNs, IP addresses and
# many-part dates stay with the safe reading.
for tok in partial.split():
groups = re.findall(r"\d+", tok)
if groups and (len(groups) > 2 or max(map(len, groups)) > 4 or len(tok.strip(STRIP)) > 10):
return False
# Everything the rules are sure of must survive unchanged: the partial reading minus the spans left as written.
sure = " ".join(t for t in partial.split() if not RAW.search(t))
need = _words(sure)
if not (_in_order(need, hay) or _in_order(need, _merged(hay))):
return False
# Every currency symbol is spoken: "£100m" must say pounds.
for sym, forms in V.CURRENCY.items():
if sym in src and not set(forms) & set(hay):
return False
if re.search(r"[A-Za-z][.:][A-Za-z]", out) and not re.search(r"[A-Za-z][.:][A-Za-z]", src):
return False # "fourteen.thirty-one"
keep = _plain(src) + _words(sure) # whole source words and the rules' sure words, never fragments like "ers"
src_words = set(keep) | set(_merged(keep))
added = set(hay) - src_words
if added - SPOKEN:
return False
# a unit or currency word needs its mark in the source: no "kilowatt hours" without "kWh", no "dollars" without "$"
return all(any(_has_mark(src, m) for m in UNIT_TRIGGERS[w]) for w in added if w in UNIT_TRIGGERS)
class Normalizer:
def __init__(self, model_dir=".", device=None):
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
self.torch = torch
self.dev = device or ("cuda" if torch.cuda.is_available() else
"xpu" if hasattr(torch, "xpu") and torch.xpu.is_available() else "cpu")
self.tok = AutoTokenizer.from_pretrained(model_dir)
self.model = AutoModelForSeq2SeqLM.from_pretrained(model_dir).to(self.dev).eval()
self.stats = {"rules": 0, "model": 0, "fallback": 0}
def _generate(self, texts, bs=64):
out = []
for i in range(0, len(texts), bs):
x = self.tok(texts[i:i + bs], return_tensors="pt", padding=True).to(self.dev)
with self.torch.no_grad():
y = self.model.generate(**x, max_new_tokens=min(600, 2 * x["input_ids"].shape[1] + 20), num_beams=1)
out += [s.strip() for s in self.tok.batch_decode(y, skip_special_tokens=True)]
return out
def __call__(self, texts):
single = isinstance(texts, str)
texts = [texts] if single else list(texts)
res, todo = [None] * len(texts), []
for i, t in enumerate(texts):
r = verbalize(t)
if r is not None:
res[i] = r
self.stats["rules"] += 1
else:
todo.append(i)
if todo:
for i, r in zip(todo, self._generate([texts[i] for i in todo])):
if acceptable(texts[i], r):
res[i] = r
self.stats["model"] += 1
else:
res[i] = verbalize(texts[i], partial=True)
self.stats["fallback"] += 1
return res[0] if single else res
if __name__ == "__main__":
ap = argparse.ArgumentParser(description="Audiction: written text in, spoken form out.")
ap.add_argument("text", nargs="*", help="sentences to normalize (default: read lines from stdin)")
ap.add_argument("--model", default=".", help="model folder (default: this folder)")
a = ap.parse_args()
lines = a.text or [l.rstrip("\n") for l in sys.stdin if l.strip()]
n = Normalizer(a.model)
for line in n(lines):
print(line)