Add SmallBE_TTS_Advanced model, offline BelG2P+StressML pipeline, and inference scripts
c48b587 verified Download src/phoneme-encoder.js from archivartaunik/SmallBE_TTS_Advanced: direct link, hf CLI and curl.
- Browser
- Download file 7.03 kB
-
https://huggingface.co/archivartaunik/SmallBE_TTS_Advanced/resolve/main/src/phoneme-encoder.js
- Command line
-
hf download hf://archivartaunik/SmallBE_TTS_Advanced/src/phoneme-encoder.js
-
curl -L -o phoneme-encoder.js https://huggingface.co/archivartaunik/SmallBE_TTS_Advanced/resolve/main/src/phoneme-encoder.js
7.03 kB
| /** | |
| * Belarusian IPA phoneme encoder & text splitter matching | |
| * tiny-voca / PhonemeEncoder.kt and be_ipa_map.py. | |
| */ | |
| const PAD = '<pad>'; | |
| const BOS = '<bos>'; | |
| const EOS = '<eos>'; | |
| const SP = '<sp>'; | |
| const UNK = '<unk>'; | |
| const SPECIALS = new Set([PAD, BOS, EOS, SP, UNK]); | |
| const PUNCTUATION = new Set([',', '.', '?', '!', ';', ':', '"', "'", '(', ')', '—', '…']); | |
| const VOWELS = new Set(['a', 'ɐ', 'ɛ', 'i', 'ɔ', 'u', 'ɨ', 'ə', 'ɪ']); | |
| const STRESS_MARKS = new Map([ | |
| ['ˈ', 1n], | |
| ['ˌ', 2n], | |
| ]); | |
| const QUOTES_TRANS = { | |
| '‘': "'", | |
| '’': "'", | |
| '‚': "'", | |
| '‛': "'", | |
| '`': "'", | |
| '´': "'", | |
| '“': '"', | |
| '”': '"', | |
| '„': '"', | |
| '‟': '"', | |
| '«': '"', | |
| '»': '"', | |
| '–': '—', | |
| '―': '—', | |
| '[': '(', | |
| '{': '(', | |
| ']': ')', | |
| '}': ')', | |
| }; | |
| const RAW_BELG2P_TO_CANONICAL = { | |
| // Vowels | |
| a: 'a', | |
| ɐ: 'ɐ', | |
| ɛ: 'ɛ', | |
| i: 'i', | |
| ɔ: 'ɔ', | |
| u: 'u', | |
| ɨ: 'ɨ', | |
| ə: 'ə', | |
| ɪ: 'ɪ', | |
| // Labials / labiodentals / bilabials | |
| p: 'p', | |
| 'p\u02B2': 'pʲ', | |
| b: 'b', | |
| 'b\u02B2': 'bʲ', | |
| f: 'f', | |
| 'f\u02B2': 'fʲ', | |
| ʋ: 'v', | |
| 'ʋ\u02B2': 'vʲ', | |
| 'β\u031E': 'β', | |
| 'β\u031E\u02B2': 'βʲ', | |
| β: 'β', | |
| 'β\u02B2': 'βʲ', | |
| w: 'w', | |
| 'u\u032F': 'w', | |
| m: 'm', | |
| 'm\u02B2': 'mʲ', | |
| ɱ: 'ɱ', | |
| // Dentals / alveolars | |
| 't\u032A': 't', | |
| 't\u032A\u02B2': 'tʲ', | |
| t: 't', | |
| 't\u02B2': 'tʲ', | |
| 'd\u032A': 'd', | |
| 'd\u032A\u02B2': 'dʲ', | |
| d: 'd', | |
| 'd\u02B2': 'dʲ', | |
| 's\u032A': 's', | |
| 's\u0331\u02B2': 'sʲ', | |
| s: 's', | |
| 's\u02B2': 'sʲ', | |
| 'z\u032A': 'z', | |
| 'z\u0331\u02B2': 'zʲ', | |
| z: 'z', | |
| 'z\u02B2': 'zʲ', | |
| 'n\u032A': 'n', | |
| n: 'n', | |
| 'n\u02B2': 'nʲ', | |
| ɫ: 'ɫ', | |
| 'l\u02B2': 'lʲ', | |
| r: 'r', | |
| // Affricates | |
| 't\u0361s\u032A': 't͡s', | |
| 't\u0361s': 't͡s', | |
| 't\u0361\u035Fs\u02B2': 't͡sʲ', | |
| 't\u0361s\u02B2': 't͡sʲ', | |
| 'd\u0361z\u032A': 'd͡z', | |
| 'd\u0361z': 'd͡z', | |
| 'd\u0361\u035Fz\u02B2': 'd͡zʲ', | |
| 'd\u0361z\u02B2': 'd͡zʲ', | |
| // Postalveolars / retroflexes | |
| ʂ: 'ʂ', | |
| ʃ: 'ʂ', | |
| ʐ: 'ʐ', | |
| ʒ: 'ʐ', | |
| 't\u0361ʂ': 't͡ʂ', | |
| 't\u0361ʃ': 't͡ʂ', | |
| 'd\u0361ʐ': 'd͡ʐ', | |
| 'd\u0361ʒ': 'd͡ʐ', | |
| // Palatal / velar / glottal | |
| j: 'j', | |
| k: 'k', | |
| 'k\u02B2': 'kʲ', | |
| g: 'ɡ', | |
| 'g\u02B2': 'ɡʲ', | |
| ɡ: 'ɡ', | |
| 'ɡ\u02B2': 'ɡʲ', | |
| x: 'x', | |
| 'x\u02B2': 'xʲ', | |
| ɣ: 'ɣ', | |
| 'ɣ\u02B2': 'ɣʲ', | |
| ɦ: 'ɣ', | |
| 'ɦ\u02B2': 'ɣʲ', | |
| }; | |
| export class PhonemeEncoder { | |
| constructor(meta) { | |
| this.sampleRate = meta.sample_rate ?? 24000; | |
| this.hop = meta.hop ?? 256; | |
| this.maxTokens = meta.max_tokens ?? 512; | |
| this.voices = Array.isArray(meta.voices) ? [...meta.voices] : ['MALE1', 'FEMALE1']; | |
| this.sym2id = new Map(); | |
| meta.symbols.forEach((sym, idx) => this.sym2id.set(sym, BigInt(idx))); | |
| this.unkId = this.sym2id.get(UNK); | |
| this.phoneSet = new Set(meta.symbols.filter((sym) => !SPECIALS.has(sym) && !PUNCTUATION.has(sym))); | |
| this.belg2pToCanonical = new Map(); | |
| for (const [rawKey, canon] of Object.entries(RAW_BELG2P_TO_CANONICAL)) { | |
| const kn = rawKey.normalize('NFC'); | |
| this.belg2pToCanonical.set(kn, canon); | |
| const gem = `${canon}\u02D0`; | |
| this.belg2pToCanonical.set(`${kn}\u02D0`, this.phoneSet.has(gem) ? gem : canon); | |
| } | |
| this.keysByLenDesc = Array.from(this.belg2pToCanonical.keys()).sort((a, b) => b.length - a.length); | |
| } | |
| encode(ipaText) { | |
| if (typeof ipaText !== 'string' || !ipaText.trim()) { | |
| return { ids: new BigInt64Array(0), stress: new BigInt64Array(0), symbols: [], unknown: [], hasPronounceable: false }; | |
| } | |
| const nfc = ipaText.normalize('NFC'); | |
| const normalized = Array.from(nfc, (ch) => QUOTES_TRANS[ch] ?? ch) | |
| .join('') | |
| .replace(/\.{3,}/gu, '…') | |
| .replace(/-{2,}|\s-\s/gu, '—') | |
| .replaceAll('-', ' '); | |
| const symbols = [BOS]; | |
| const stress = [0n]; | |
| const unknown = []; | |
| let pendingStress = 0n; | |
| let pendingSpace = false; | |
| for (let i = 0; i < normalized.length;) { | |
| const cp = normalized.codePointAt(i); | |
| const ch = String.fromCodePoint(cp); | |
| const step = ch.length; | |
| const stressMark = STRESS_MARKS.get(ch); | |
| if (stressMark !== undefined) { | |
| if (pendingStress === 0n) pendingStress = stressMark; | |
| i += step; | |
| continue; | |
| } | |
| if (/\s/u.test(ch)) { | |
| pendingSpace = true; | |
| i += step; | |
| continue; | |
| } | |
| if (PUNCTUATION.has(ch)) { | |
| pendingSpace = false; | |
| pendingStress = 0n; | |
| if (symbols.at(-1) !== ch) { | |
| symbols.push(ch); | |
| stress.push(0n); | |
| } | |
| i += step; | |
| continue; | |
| } | |
| const matched = this.keysByLenDesc.find((key) => normalized.startsWith(key, i)); | |
| if (matched !== undefined) { | |
| const canon = this.belg2pToCanonical.get(matched); | |
| const prev = symbols.at(-1); | |
| if (pendingSpace && (this.phoneSet.has(prev) || prev === UNK)) { | |
| symbols.push(SP); | |
| stress.push(0n); | |
| } | |
| pendingSpace = false; | |
| symbols.push(canon); | |
| if (VOWELS.has(canon) && pendingStress !== 0n) { | |
| stress.push(pendingStress); | |
| pendingStress = 0n; | |
| } else { | |
| stress.push(0n); | |
| } | |
| i += matched.length; | |
| } else { | |
| if (ch !== '\u02D0' && !/[\p{Cf}\p{Mn}]/u.test(ch)) { | |
| const prev = symbols.at(-1); | |
| if (pendingSpace && (this.phoneSet.has(prev) || prev === UNK)) { | |
| symbols.push(SP); | |
| stress.push(0n); | |
| } | |
| pendingSpace = false; | |
| unknown.push(ch); | |
| symbols.push(UNK); | |
| stress.push(0n); | |
| } | |
| i += step; | |
| } | |
| } | |
| symbols.push(EOS); | |
| stress.push(0n); | |
| const hasPronounceable = symbols.some((sym) => this.phoneSet.has(sym) || sym === UNK); | |
| const ids = BigInt64Array.from(symbols, (sym) => this.sym2id.get(sym) ?? this.unkId); | |
| const stressArr = BigInt64Array.from(stress); | |
| return { ids, stress: stressArr, symbols, unknown, hasPronounceable }; | |
| } | |
| } | |
| export function ensureFinished(text) { | |
| const body = text.replace(/[\s\ufeff]+$/gu, ''); | |
| if (!body) return text; | |
| const last = body.split(/[\s\ufeff]+/u).at(-1) ?? ''; | |
| const marked = /[\p{L}\p{N}]/u.test(last) | |
| ? /[.!?…,;:]$/u.test(last.replace(/["'”’)\]»]+$/u, '')) | |
| : /[.!?…]/u.test(last) || [',', ';', ':'].includes(last); | |
| return marked ? text : `${body}.`; | |
| } | |
| export function splitSentences(text) { | |
| return ensureFinished(text) | |
| .replace(/[\s\ufeff]+/gu, ' ') | |
| .trim() | |
| .split(/(?:(?<=[.!?…])|(?<=[.!?…]["')\]]))\s+(?=["'(\[«]?[A-ZА-ЯЁІЎ0-9])/u) | |
| .map((s) => s.trim()) | |
| .filter((s) => s.length > 0 && /[\p{L}\p{N}]/u.test(s)); | |
| } | |
| export function splitShorter(text) { | |
| const clauses = text | |
| .split(/(?<=[,;:—])\s+/u) | |
| .map((s) => s.trim()) | |
| .filter(Boolean); | |
| if (clauses.length > 1) return clauses; | |
| const words = text.split(/\s+/u).filter(Boolean); | |
| if (words.length < 2) return []; | |
| const half = Math.ceil(words.length / 2); | |
| return [words.slice(0, half).join(' '), words.slice(half).join(' ')]; | |
| } | |