import os import re import torch # Configure eSpeak (ensure paths are correctly set for your environment) def configure_espeak(): """Configure espeak-ng paths for Windows""" os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = r"C:\Program Files\eSpeak NG\libespeak-ng.dll" os.environ["PHONEMIZER_ESPEAK_PATH"] = r"C:\Program Files\eSpeak NG\espeak-ng.exe" print("PHONEMIZER_ESPEAK_LIBRARY:", os.environ.get("PHONEMIZER_ESPEAK_LIBRARY")) print("PHONEMIZER_ESPEAK_PATH:", os.environ.get("PHONEMIZER_ESPEAK_PATH")) if not os.path.exists(os.environ["PHONEMIZER_ESPEAK_LIBRARY"]): raise FileNotFoundError(f"Could not find espeak library at {os.environ['PHONEMIZER_ESPEAK_LIBRARY']}") if not os.path.exists(os.environ["PHONEMIZER_ESPEAK_PATH"]): raise FileNotFoundError(f"Could not find espeak executable at {os.environ['PHONEMIZER_ESPEAK_PATH']}") # Call the configuration function for eSpeak if os.name == 'nt': configure_espeak() import phonemizer def split_num(num): num = num.group() if '.' in num: return num elif ':' in num: h, m = [int(n) for n in num.split(':')] if m == 0: return f"{h} o'clock" elif m < 10: return f'{h} oh {m}' return f'{h} {m}' year = int(num[:4]) if year < 1100 or year % 1000 < 10: return num left, right = num[:2], int(num[2:4]) s = 's' if num.endswith('s') else '' if 100 <= year % 1000 <= 999: if right == 0: return f'{left} hundred{s}' elif right < 10: return f'{left} oh {right}{s}' return f'{left} {right}{s}' def flip_money(m): m = m.group() bill = 'dollar' if m[0] == '$' else 'pound' if m[-1].isalpha(): return f'{m[1:]} {bill}s' elif '.' not in m: s = '' if m[1:] == '1' else 's' return f'{m[1:]} {bill}{s}' b, c = m[1:].split('.') s = '' if b == '1' else 's' c = int(c.ljust(2, '0')) coins = f"cent{'' if c == 1 else 's'}" if m[0] == '$' else ('penny' if c == 1 else 'pence') return f'{b} {bill}{s} and {c} {coins}' def point_num(num): a, b = num.group().split('.') return ' point '.join([a, ' '.join(b)]) def normalize_text(text): text = text.replace(chr(8216), "'").replace(chr(8217), "'") text = text.replace('«', chr(8220)).replace('»', chr(8221)) text = text.replace(chr(8220), '"').replace(chr(8221), '"') text = text.replace('(', '«').replace(')', '»') for a, b in zip('、。!,:;?', ',.!,:;?'): text = text.replace(a, b+' ') text = re.sub(r'[^\S \n]', ' ', text) text = re.sub(r' +', ' ', text) text = re.sub(r'(?<=\n) +(?=\n)', '', text) text = re.sub(r'\bD[Rr]\.(?= [A-Z])', 'Doctor', text) text = re.sub(r'\b(?:Mr\.|MR\.(?= [A-Z]))', 'Mister', text) text = re.sub(r'\b(?:Ms\.|MS\.(?= [A-Z]))', 'Miss', text) text = re.sub(r'\b(?:Mrs\.|MRS\.(?= [A-Z]))', 'Mrs', text) text = re.sub(r'\betc\.(?! [A-Z])', 'etc', text) text = re.sub(r'(?i)\b(y)eah?\b', r"\1e'a", text) text = re.sub(r'\d*\.\d+|\b\d{4}s?\b|(? 510: tokens = tokens[:510] print('Truncated to 510 tokens') ref_s = voicepack[len(tokens)] out = forward(model, tokens, ref_s, speed) ps = ''.join(next(k for k, v in VOCAB.items() if i == v) for i in tokens) return out, ps @torch.no_grad() def forward(model, tokens, ref_s, speed): # Device management device = ref_s.device # Tokenization tokens = torch.LongTensor([[0, *tokens, 0]]).to(device) input_lengths = torch.LongTensor([tokens.shape[-1]]) # Text Mask text_mask = length_to_mask(input_lengths).to(device) # Predictor bert_dur = model.bert(tokens, attention_mask=(~text_mask).int()) d_en = model.bert_encoder(bert_dur).transpose(-1, -2) s = ref_s[:, 128:] d = model.predictor.text_encoder(d_en, s, input_lengths, text_mask) # Fusion layers x, _ = model.predictor.lstm(d) duration = model.predictor.duration_proj(x) duration = torch.sigmoid(duration).sum(axis=-1) / speed # Prediction pred_dur = torch.round(duration).clamp(min=1).long() pred_aln_trg = torch.zeros(input_lengths, pred_dur.sum().item()) c_frame = 0 for i in range(pred_aln_trg.size(0)): pred_aln_trg[i, c_frame:c_frame + pred_dur[0, i].item()] = 1 c_frame += pred_dur[0, i].item() # Decoder en = d.transpose(-1, -2) @ pred_aln_trg.unsqueeze(0).to(device) F0_pred, N_pred = model.predictor.F0Ntrain(en, s) # Output t_en = model.text_encoder(tokens, input_lengths, text_mask) asr = t_en @ pred_aln_trg.unsqueeze(0).to(device) return model.decoder(asr, F0_pred, N_pred, ref_s[:, :128]).squeeze().cpu().numpy()