Instructions to use batuhan28/tr-fastconformer-stream with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- NeMo
How to use batuhan28/tr-fastconformer-stream with NeMo:
# tag did not correspond to a valid NeMo domain.
- Notebooks
- Google Colab
- Kaggle
Download v11/code/lib.py from batuhan28/tr-fastconformer-stream: direct link, hf CLI and curl.
- Browser
- Download file 20.1 kB
-
https://huggingface.co/batuhan28/tr-fastconformer-stream/resolve/main/v11/code/lib.py
- Command line
-
hf download hf://batuhan28/tr-fastconformer-stream/v11/code/lib.py
-
curl -L -o lib.py https://huggingface.co/batuhan28/tr-fastconformer-stream/resolve/main/v11/code/lib.py
20.1 kB
| """v11 Yardımcıları: ISSAI Kopuk-i Düzeltici, Fast-Reverb & Spectral Tilt, KIRAAT-Free Big KenLM, Blank-Penalty.""" | |
| import io, math, re, subprocess, shutil, os, sys, time, collections, unicodedata | |
| from pathlib import Path | |
| import numpy as np | |
| from scipy.signal import resample_poly, butter, sosfilt | |
| try: | |
| import soxr | |
| except ImportError: | |
| soxr = None | |
| SR = 16000 | |
| TOK0 = 0x4E00 | |
| _TR = str.maketrans({"â": "a", "î": "i", "û": "u", "ê": "e", "ô": "o", "ć": "c", | |
| "é": "e", "è": "e", "á": "a", "à": "a", "ó": "o", "ú": "u", "í": "i", | |
| "ñ": "n", "ß": "ss", "'": "", "’": "", "`": "", "‘": ""}) | |
| # EDA'da tespit edilen ISSAI NFD (\u0307) kopuk 'i' ve düşmüş belirtme eki düzeltici: | |
| _ISSAI_BROKEN_I_RE = re.compile( | |
| r"\bi\s+(stanbul\w*|talya\w*|zmir\w*|ngiltere\w*|ngiliz\w*|ran\w*|rak\w*|slam\w*|srail\w*|spanya\w*|" | |
| r"sveç\w*|sviçre\w*|skoçya\w*|rlanda\w*|zlanda\w*|yi\w*|şte|nsan\w*|ki\w*|lk\w*|çin\w*|çerde\w*|çeri\w*|" | |
| r"çişleri\w*|brahim\w*|smail\w*|sa\b|dris\w*|nşallah|nşaat\w*|statistik\w*|mparatorlu\w*|nce\w*|nfaz\w*|" | |
| r"hale\w*|lan\w*|laç\w*|lacı\w*|lgi\w*|fade\w*|leri\w*|lke\w*|mza\w*|nan\w*|nkar\w*|nkâr\w*|kiz\w*|" | |
| r"klim\w*|kram\w*|ktidar\w*|ktisat\w*|ktisadi\w*|mam\w*|mkan\w*|mtihan\w*|mtiyaz\w*|thal\w*|hracat\w*|" | |
| r"tiraz\w*|tiraf\w*|tibar\w*|ttifak\w*|zleyici\w*|zlemek\w*|stiklal\w*|stikrar\w*|stihdam\w*|stihbarat\w*|" | |
| r"htiyaç\w*|htiyac\w*|htimal\w*|htiyar\w*|badet\w*|bare\w*|cra\w*|cazet\w*|cat\w*|dare\w*|dari\w*|" | |
| r"ddia\w*|dam\w*|flas\w*|ftar\w*|ftira\w*|hlal\w*|hlas\w*|hanet\w*|hbar\w*|hraç\w*|kna\w*|kamet\w*|" | |
| r"kaz\w*|lham\w*|lim\w*|lmi\w*|lahi\w*|mal\w*|mar\w*|mha\w*|nat\w*|ncir\w*|ndirim\w*|ntihar\w*|" | |
| r"ptal\w*|pucu\w*|rade\w*|rfan\w*|sabet\w*|skan\w*|skele\w*|sraf\w*|stifa\w*|stismar\w*|stisna\w*|" | |
| r"syan\w*|şaret\w*|şgal\w*|şkence\w*|ştirak\w*|ştah\w*|taat\w*|zah\w*|zin\w*|zni\w*|zole\w*|" | |
| r"dil\w*|sis\b|l\b|li\b|lçe\w*|dli\w*|remsu\w*)\b" | |
| ) | |
| _UNIT_ABBR = {"km": "kilometre", "cm": "santimetre", "mm": "milimetre", "kg": "kilogram", | |
| "mg": "miligram", "ml": "mililitre", "lt": "litre", "gr": "gram"} | |
| _UNIT_RE = re.compile(r"(?<=\d)\s*(" + "|".join(_UNIT_ABBR.keys()) + r")\b\.?", flags=re.IGNORECASE) | |
| _DOT_ABBR = {"dr": "doktor", "prof": "profesör", "doç": "doçent", "mah": "mahallesi", | |
| "cad": "caddesi", "yy": "yüzyıl", "vb": "ve benzeri", "vs": "vesaire"} | |
| _DOT_RE = re.compile(r"\b(" + "|".join(_DOT_ABBR.keys()) + r")\.(?=\s|$)", flags=re.IGNORECASE) | |
| _YT_OUTRO_RE = re.compile(r"kanalımıza destek olmak için lütfen üye olun|beğen tuşuna basın yorum yapın|abone olmayı unutmayın") | |
| def lower_tr(t): | |
| t = t.replace("I\u0307", "i").replace("i\u0307", "i") | |
| return unicodedata.normalize("NFC", t).replace("İ", "i").replace("\u0130", "i").replace("I", "ı").lower() | |
| def lower_en(t): | |
| return t.replace("İ", "i").lower() | |
| B = "sıfır bir iki üç dört beş altı yedi sekiz dokuz".split() | |
| O = ["", "on", "yirmi", "otuz", "kırk", "elli", "altmış", "yetmiş", "seksen", "doksan"] | |
| SCALE = ["", "bin", "milyon", "milyar", "trilyon"] | |
| def cardinal(n): | |
| if n == 0: | |
| return B[0] | |
| if n >= 10 ** 15: | |
| return " ".join(B[int(x)] for x in str(n)) | |
| def three(v): | |
| h, t, u = v // 100, v // 10 % 10, v % 10 | |
| return " ".join(x for x in [(("" if h == 1 else B[h] + " ") + "yüz") if h else "", O[t], B[u] if u else ""] if x) | |
| parts, i = [], 0 | |
| while n: | |
| v = n % 1000 | |
| n //= 1000 | |
| if v: | |
| body = "" if (i == 1 and v == 1) else three(v) | |
| parts.append((body + " " + SCALE[i]).strip()) | |
| i += 1 | |
| return " ".join(reversed(parts)) | |
| def digits_spoken(s): | |
| return " ".join(B[int(c)] for c in s if c.isdigit()) | |
| def attach_tr_suffix(spoken, raw_suffix): | |
| suf = lower_tr(raw_suffix.lstrip("'’`‘")).translate(_TR) | |
| if not suf: | |
| return spoken | |
| words = spoken.split() | |
| if not words: | |
| return spoken | |
| last = words[-1] | |
| if last == "dört" and suf[0] in "aeıioöuü": | |
| words[-1] = "dörd" + suf | |
| else: | |
| words[-1] = last + suf | |
| return " ".join(words) | |
| _NUM = re.compile(r"(%?)(\d[\d.,]*\d|\d)(%?)(['’`‘][a-zA-ZçğıöşüÇĞİÖŞÜ]+)?") | |
| def verbalize(text, strict=True): | |
| bad = False | |
| def one(m): | |
| nonlocal bad | |
| pre, num, post, suffix = m.group(1), m.group(2), m.group(3), m.group(4) | |
| out = None | |
| if re.fullmatch(r"\d+", num): | |
| if len(num) >= 7 or (len(num) > 1 and num.startswith("0")): | |
| bad = True | |
| out = digits_spoken(num) | |
| else: | |
| out = cardinal(int(num)) | |
| elif re.fullmatch(r"\d{1,3}(\.\d{3})+", num): | |
| out = cardinal(int(num.replace(".", ""))) | |
| elif re.fullmatch(r"\d+,\d+", num): | |
| a, b = num.split(",") | |
| out = cardinal(int(a)) + " virgül " + digits_spoken(b) | |
| else: | |
| bad = True | |
| out = digits_spoken(num) | |
| if suffix and out: | |
| out = attach_tr_suffix(out, suffix) | |
| if pre or post: | |
| out = "yüzde " + out | |
| return " " + out + " " | |
| res = _NUM.sub(one, text) | |
| if bad and strict: | |
| return None | |
| return res | |
| def clean_text(t, lang="tr", strict=True): | |
| if not isinstance(t, str): | |
| return None | |
| t = _UNIT_RE.sub(lambda m: " " + _UNIT_ABBR[m.group(1).lower()] + " ", t) | |
| t = _DOT_RE.sub(lambda m: " " + _DOT_ABBR[m.group(1).lower()] + " ", t) | |
| t = verbalize(t, strict=strict) | |
| if t is None: | |
| return None | |
| t = (lower_en(t) if lang == "en" else lower_tr(t)).translate(_TR) | |
| t = re.sub(r"\s+", " ", re.sub(r"[^a-zçğıöşüwq ]", " ", t)).strip() | |
| if lang == "tr" and "i " in t: | |
| t = _ISSAI_BROKEN_I_RE.sub(r"i\1", t) | |
| t = re.sub(r"\b([a-zçğıöşü]*[eiöü][a-zçğıöşü]+)\s+i\b(?!\s+(?:kerim|şerif|nur|azam|alem|hukuk|nefise|sina|rüşd|tebrizi|ahmer))", r"\1i", t) | |
| return t | |
| def label_ok(text, dur, max_cps=25.0, min_cps=2.0): | |
| if not text or dur <= 0: | |
| return False | |
| if _YT_OUTRO_RE.search(text): | |
| return False | |
| cps = len(text) / dur | |
| if cps > max_cps or cps < min_cps: | |
| return False | |
| w = text.split() | |
| if len(w) >= 10 and len(set(w)) / len(w) < 0.25: | |
| return False | |
| if re.search(r"\b(\w+(?: \w+){0,3})(?: \1){5,}", text): | |
| return False | |
| return True | |
| def frames_needed(ids): | |
| return len(ids) + sum(a == b for a, b in zip(ids, ids[1:])) | |
| def lev(a, b): | |
| from rapidfuzz.distance import Levenshtein as L | |
| return L.distance(a, b) | |
| def wer_counts(refs, hyps): | |
| we = wn = ce = cn = 0 | |
| for r, h in zip(refs, hyps): | |
| rw, hw = r.split(), h.split() | |
| we += lev(rw, hw); wn += len(rw) | |
| ce += lev(r, h); cn += len(r) | |
| return we, wn, ce, cn | |
| def fast_resample(w, sr_in, sr_out): | |
| if sr_in == sr_out: | |
| return w.astype("float32", copy=False) | |
| if soxr is not None: | |
| return soxr.resample(w, sr_in, sr_out, quality="HQ").astype("float32") | |
| g = math.gcd(int(sr_in), int(sr_out)) | |
| return resample_poly(w, int(sr_out) // g, int(sr_in) // g).astype("float32") | |
| def to_16k(w, sr): | |
| return fast_resample(w, sr, SR) | |
| def decode_audio(raw): | |
| import soundfile as sf | |
| w, sr = sf.read(io.BytesIO(raw), dtype="float32", always_2d=False) | |
| if w.ndim > 1: | |
| w = w.mean(1) | |
| return to_16k(w, sr) | |
| _SOS_TEL = butter(4, [300, 3400], btype="bandpass", fs=8000, output="sos") | |
| def mulaw(w, mu=255): | |
| x = np.clip(w, -1, 1) | |
| y = np.sign(x) * np.log1p(mu * np.abs(x)) / np.log1p(mu) | |
| y = np.round((y + 1) / 2 * 255) / 255 * 2 - 1 | |
| return (np.sign(y) * ((1 + mu) ** np.abs(y) - 1) / mu).astype("float32") | |
| def telephone(w): | |
| n = fast_resample(w, 16000, 8000) | |
| n = mulaw(sosfilt(_SOS_TEL, n).astype("float32")) | |
| out = fast_resample(n, 8000, 16000) | |
| return out[: len(w)] if len(out) >= len(w) else np.pad(out, (0, len(w) - len(out))) | |
| def fast_room_reverb(w, rng): | |
| """0.1ms Vektörel 3-Tap Oda Yankısı (Canlı mikrofon / uzak konuşmacı simülasyonu).""" | |
| out = w.copy() | |
| d1 = int(rng.uniform(0.012, 0.035) * SR) | |
| d2 = int(rng.uniform(0.040, 0.085) * SR) | |
| d3 = int(rng.uniform(0.090, 0.145) * SR) | |
| g1 = float(rng.uniform(0.12, 0.28)) | |
| g2 = float(rng.uniform(0.06, 0.16)) | |
| g3 = float(rng.uniform(0.02, 0.08)) | |
| n = len(w) | |
| if d1 < n: out[d1:] += g1 * w[:-d1] | |
| if d2 < n: out[d2:] += g2 * w[:-d2] | |
| if d3 < n: out[d3:] += g3 * w[:-d3] | |
| peak = np.max(np.abs(out)) | |
| if peak > 0.99: | |
| out *= (0.95 / peak) | |
| return out.astype("float32") | |
| def spectral_tilt(w, rng): | |
| """0.05ms 1. Derece Pre/De-Emphasis Filtresi (Farklı mikrofon kapsülü / bas-tiz karakteri).""" | |
| alpha = float(rng.uniform(-0.35, 0.35)) | |
| out = np.empty_like(w) | |
| out[0] = w[0] | |
| out[1:] = w[1:] + alpha * w[:-1] | |
| return out | |
| def edge(w, rng, thr=0.01, max_pad=0.5): | |
| a = np.abs(w) | |
| if a.max() > 0: | |
| idx = np.flatnonzero(a > thr * a.max()) | |
| if len(idx): | |
| w = w[max(0, idx[0] - 800): idx[-1] + 800] | |
| pre = np.zeros(int(rng.uniform(0, max_pad) * SR), "float32") | |
| post = np.zeros(int(rng.uniform(0, max_pad) * SR), "float32") | |
| return np.concatenate([pre, w, post]) | |
| def augment(w, rng, tel_p=0.18, edge_p=0.25, reverb_p=0.20, noise_p=0.14, speed_p=0.12, gain_p=0.25): | |
| if reverb_p > 0 and rng.random() < reverb_p: | |
| w = fast_room_reverb(w, rng) | |
| if rng.random() < 0.20: | |
| w = spectral_tilt(w, rng) | |
| if tel_p > 0 and rng.random() < tel_p: | |
| w = telephone(w) | |
| if noise_p > 0 and rng.random() < noise_p: | |
| noise = rng.standard_normal(len(w)).astype("float32") | |
| snr = rng.uniform(8, 25) | |
| w = w + noise * np.sqrt((np.mean(w * w) + 1e-9) / 10 ** (snr / 10)) | |
| if speed_p > 0 and rng.random() < speed_p: | |
| rate = float(rng.choice([0.92, 0.96, 1.04, 1.08])) | |
| w = fast_resample(w, int(SR * rate), SR) | |
| if gain_p > 0 and rng.random() < gain_p: | |
| w = w * float(10 ** (rng.uniform(-8, 5) / 20)) | |
| if edge_p > 0 and rng.random() < edge_p: | |
| w = edge(w, rng) | |
| return np.clip(w, -1, 1).astype("float32") | |
| def make_batches(buffer, max_seconds, max_clips, rng): | |
| buffer = sorted(buffer, key=lambda z: len(z[0])) | |
| batches, cur, longest = [], [], 0 | |
| for item in buffer: | |
| n = len(item[0]) | |
| if cur and (max(longest, n) * (len(cur) + 1) > max_seconds * SR or len(cur) >= max_clips): | |
| batches.append(cur); cur, longest = [], 0 | |
| cur.append(item); longest = max(longest, n) | |
| if cur: | |
| batches.append(cur) | |
| order = rng.permutation(len(batches)) | |
| return [batches[i] for i in order] | |
| def pick_representative(durs, words, chars, n, k_dur=5, k_rate=3): | |
| d = np.asarray(durs, float); w = np.asarray(words, float); c = np.asarray(chars, float) | |
| N = len(d) | |
| if n >= N: | |
| return list(range(N)) | |
| rate = c / np.maximum(d, 1e-6) | |
| wl = c / np.maximum(w, 1) | |
| def qbin(x, k): | |
| return np.searchsorted(np.quantile(x, np.linspace(0, 1, k + 1)[1:-1]), x, side="right") | |
| strata = qbin(d, k_dur) * k_rate + qbin(rate, k_rate) | |
| ids = np.unique(strata) | |
| mass = {s: w[strata == s].sum() for s in ids} | |
| tot = sum(mass.values()) | |
| quota = {s: n * mass[s] / tot for s in ids} | |
| alloc = {s: int(np.floor(quota[s])) for s in ids} | |
| for s in sorted(ids, key=lambda s: -(quota[s] - alloc[s]))[: n - sum(alloc.values())]: | |
| alloc[s] += 1 | |
| chosen = [] | |
| for s in ids: | |
| idx = np.flatnonzero(strata == s) | |
| k = min(alloc[s], len(idx)) | |
| if k: | |
| order = idx[np.argsort(wl[idx], kind="stable")] | |
| chosen += [int(order[int((j + 0.5) / k * len(order))]) for j in range(k)] | |
| return sorted(chosen) | |
| def to_i16(w): | |
| return (np.clip(w, -1, 1) * 32767).astype("int16") | |
| def batch_stream(groups, share, hours, worker, nworkers, rng, read, ids_fn, blocked, seconds, max_clips, | |
| lang_of=lambda s: "tr", tel_p=0.18, edge_p=0.25, reverb_p=0.20, min_buffer=3200, ram_cache_shards=5, status_fn=None): | |
| from concurrent.futures import ThreadPoolExecutor | |
| sources = list(groups) | |
| weight = np.array([share[s] / (hours[s] / max(1, len(groups[s]))) for s in sources], dtype="float64") | |
| weight /= weight.sum() | |
| pools = {s: groups[s][worker::nworkers] for s in sources} | |
| assert all(pools.values()), "Worker başına shard kalmadı." | |
| ram_cache = collections.OrderedDict() | |
| ex = ThreadPoolExecutor(4) | |
| seen_counts = collections.Counter() | |
| def fetch_shard(shard_path): | |
| if shard_path in ram_cache: | |
| ram_cache.move_to_end(shard_path) | |
| return ram_cache[shard_path] | |
| d = read(shard_path) | |
| parsed = [] | |
| for a, t, dur in zip(d["audio"], d["text"], d["duration"]): | |
| if a and a.get("bytes") and dur is not None and 1.1 <= dur <= 22.0: | |
| parsed.append((a["bytes"], t, float(dur))) | |
| del d | |
| if ram_cache_shards > 0: | |
| ram_cache[shard_path] = parsed | |
| while len(ram_cache) > ram_cache_shards: | |
| ram_cache.popitem(last=False) | |
| return parsed | |
| def nxt(): | |
| s = sources[int(rng.choice(len(sources), p=weight))] | |
| shard_path = pools[s][int(rng.integers(len(pools[s])))] | |
| return s, shard_path, ex.submit(fetch_shard, shard_path) | |
| q = collections.deque([nxt() for _ in range(4)]) | |
| buf = [] | |
| first_flush = True | |
| batches_yielded = 0 | |
| while True: | |
| s, spath, fut = q.popleft() | |
| q.append(nxt()) | |
| if first_flush and worker == 0 and status_fn: | |
| status_fn(f"📥 [Worker 0] 4x Paralel Shard RAM'e çekiliyor: {spath} (Tampon: {len(buf)}/900 klip)...") | |
| rows = fut.result() | |
| lang = lang_of(s) | |
| target_buf = 900 if first_flush else min_buffer | |
| idxs = rng.permutation(len(rows)) | |
| # İlerleyen adımlarda (Cool-down) bozucu efektleri yarıya indir | |
| scale_aug = 0.45 if batches_yielded > 950 else 1.0 | |
| for i in idxs: | |
| raw_bytes, t, dur = rows[i] | |
| text = clean_text(t, lang) | |
| if not text or text in blocked or not label_ok(text, dur): | |
| continue | |
| if len(text) > 25: | |
| if seen_counts[text] >= 3: | |
| continue | |
| seen_counts[text] += 1 | |
| if len(seen_counts) > 250_000: | |
| seen_counts.clear() | |
| ids = ids_fn(text) | |
| if not ids: | |
| continue | |
| need = frames_needed(ids) + 2 | |
| if need >= int(dur * 25 * 0.9): | |
| continue | |
| w = augment(decode_audio(raw_bytes), rng, tel_p * scale_aug, edge_p, reverb_p * scale_aug) | |
| if need >= len(w) // 640 - 1: | |
| continue | |
| buf.append((to_i16(w), ids)) | |
| if first_flush and worker == 0 and status_fn and len(buf) % 150 == 0: | |
| status_fn(f"⚡ [Worker 0] Dev A100 Batch hazırlanıyor ({s}): {len(buf)}/{target_buf} klip...") | |
| if first_flush and len(buf) >= target_buf: | |
| break | |
| if len(buf) < target_buf: | |
| continue | |
| if first_flush and worker == 0 and status_fn: | |
| status_fn(f"🔥 İlk Dev Batch ({seconds} sn) hazır! A100 40GB VRAM tam kapasite devreye giriyor...") | |
| first_flush = False | |
| for b in make_batches(buf, seconds, max_clips, rng): | |
| batches_yielded += 1 | |
| yield b | |
| buf = [] | |
| def featurize_np(w, fb, window, n_fft=512, hop=160, preemph=0.97, pad_mode="constant", guard=2.0 ** -24): | |
| w = np.asarray(w, dtype="float32") | |
| x = np.concatenate([w[:1], w[1:] - preemph * w[:-1]]) if preemph else w | |
| xp = np.pad(x, (n_fft // 2, n_fft // 2), mode=pad_mode) | |
| n_all = 1 + len(w) // hop | |
| idx = np.arange(n_fft)[None, :] + hop * np.arange(n_all)[:, None] | |
| win = np.zeros(n_fft, "float32") | |
| lp = (n_fft - len(window)) // 2 | |
| win[lp: lp + len(window)] = window | |
| spec = np.abs(np.fft.rfft(xp[idx] * win[None, :], axis=1)).astype("float32") ** 2 | |
| mel = np.log(fb @ spec.T + guard) | |
| n = len(w) // hop | |
| mel = mel[:, :n] | |
| mean = mel.mean(1, keepdims=True) | |
| std = np.sqrt(((mel - mean) ** 2).sum(1, keepdims=True) / max(n - 1, 1)) | |
| return ((mel - mean) / (std + 1e-5)).astype("float32") | |
| def lm_line(sp, text): | |
| return " ".join(chr(TOK0 + i) for i in sp.encode(text)) | |
| def ensure_kenlm(log=print): | |
| p = shutil.which("lmplz") | |
| if p: | |
| return str(Path(p).parent) | |
| root = Path("/tmp/kenlm") | |
| if (root / "build/bin/lmplz").exists(): | |
| return str(root / "build/bin") | |
| return None | |
| def build_kenlm(corpus_path, out_binary, bin_dir, order=6, mem="24G", tmp="/tmp"): | |
| arpa = str(Path(out_binary).with_suffix(".arpa")) | |
| with open(corpus_path, "rb") as src: | |
| subprocess.run([f"{bin_dir}/lmplz", "-o", str(order), "-S", mem, "-T", tmp + "/", "--arpa", arpa, | |
| "--discount_fallback", "--prune", *map(str, ([0, 0, 1, 1, 2, 2] + [2] * 8)[:order])], | |
| stdin=src, check=True, stderr=subprocess.DEVNULL) | |
| subprocess.run([f"{bin_dir}/build_binary", "-q", "8", "-b", "8", "-a", "22", "trie", arpa, str(out_binary)], | |
| check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
| os.remove(arpa) | |
| class LMDecoder: | |
| def __init__(self, lm_path, vocab, alpha=0.95, beta=0.0, beam=48, top_tokens=20, thresh=15.0): | |
| self.V = vocab | |
| self.lm_path = str(lm_path) | |
| self.beam = beam | |
| self.top_tokens = min(top_tokens, vocab + 2) | |
| self.thresh = thresh | |
| self.backend = None | |
| try: | |
| from flashlight.lib.text.decoder import (CriterionType, KenLM, LexiconFreeDecoder, | |
| LexiconFreeDecoderOptions) | |
| from flashlight.lib.text.dictionary import Dictionary | |
| self._fl_CriterionType = CriterionType | |
| self._fl_Opts = LexiconFreeDecoderOptions | |
| self._fl_Cls = LexiconFreeDecoder | |
| toks = [chr(TOK0 + i) for i in range(vocab)] + ["<blank>", "<sil>"] | |
| self.dict = Dictionary(toks) | |
| self.blank, self.sil = vocab, vocab + 1 | |
| self.lm = KenLM(self.lm_path, self.dict) | |
| self.backend = "flashlight" | |
| self.set(alpha=alpha, beta=beta, beam=beam) | |
| except Exception: | |
| from pyctcdecode import build_ctcdecoder | |
| labels = [chr(TOK0 + i) + " " for i in range(vocab)] | |
| self._pyctc_labels = labels | |
| self._pyctc_build = build_ctcdecoder | |
| self.backend = "pyctcdecode" | |
| self.set(alpha=alpha, beta=beta, beam=beam) | |
| def set(self, alpha=0.95, beta=0.0, beam=None): | |
| if beam is not None: | |
| self.beam = beam | |
| if self.backend == "flashlight": | |
| self.opts = self._fl_Opts( | |
| self.beam, self.top_tokens, self.thresh, float(alpha), float(beta), False, self._fl_CriterionType.CTC | |
| ) | |
| self.dec = self._fl_Cls(self.opts, self.lm, self.sil, self.blank, []) | |
| else: | |
| self.dec = self._pyctc_build(self._pyctc_labels, kenlm_model_path=self.lm_path, alpha=float(alpha), beta=float(beta)) | |
| def decode(self, logp): | |
| if self.backend == "flashlight": | |
| T = logp.shape[0] | |
| em = np.full((T, self.V + 2), -50.0, dtype="float32") | |
| em[:, : self.V + 1] = logp | |
| em[:, self.sil] = logp[:, self.V] | |
| em = np.ascontiguousarray(em) | |
| res = self.dec.decode(em.ctypes.data, T, self.V + 2) | |
| if not res: | |
| return [] | |
| best = res[0].tokens | |
| out, prev = [], None | |
| for t in best: | |
| if t != prev and 0 <= t < self.V: | |
| out.append(t) | |
| prev = t | |
| return out | |
| else: | |
| txt = self.dec.decode(logp, beam_width=self.beam) | |
| return [ord(ch) - TOK0 for ch in txt if 0 <= ord(ch) - TOK0 < self.V] | |