import gradio as gr from gruut import sentences import phonemizer global_phonemizer = phonemizer.backend.EspeakBackend(language='en-us', preserve_punctuation=True, with_stress=True, words_mismatch='ignore') from nltk import word_tokenize import nltk from cached_path import cached_path from dp.phonemizer import Phonemizer phonemizer = Phonemizer.from_checkpoint(str(cached_path('https://public-asai-dl-models.s3.eu-central-1.amazonaws.com/DeepPhonemizer/en_us_cmudict_ipa_forward.pt'))) import re from num2words import num2words def replace_numbers_with_words(text): pattern = r'\d+' matches = re.findall(pattern, text) for match in matches: word_equivalent = num2words(int(match)) text = text.replace(match, word_equivalent) return text nltk.download('punkt') # Borrowed from https://github.com/sidharthrajaram/StyleTTS2/blob/main/src/styletts2/phoneme.py, MIT License def gruut(text): phonemized = [] for sent in sentences(text, lang='en-us'): for word in sent: if isinstance(word.phonemes, Iterable): phonemized.append(''.join(word.phonemes)) elif isinstance(word.phonemes, str): phonemized.append(word.phonemes) phonemized_text = ' '.join(phonemized) return phonemized_text def phonemizerfunc(text): text = text.strip() text = text.replace('"', '') ps = global_phonemizer.phonemize([text]) ps = word_tokenize(ps[0]) ps = ' '.join(ps) return ps def dp(text): text = replace_numbers_with_words(text) return phonemizer(text, lang='en_us') def run(t, r): if r == 'phonemizer': return phonemizerfunc(t) if r == 'gruut': return gruut(t) if r == 'deep_phonemizer': return dp(t) with gr.Blocks() as demo: gr.Markdown("# PhonemizerHub") t = gr.Textbox(label="Text", placeholder="Enter text...", interactive=True) r = gr.Radio(choices=['phonemizer', 'gruut', 'deep_phonemizer'], value='phonemizer', interactive=True, label='Select a phonemizer') b = gr.Button("Phonemize") out = gr.Textbox(interactive=True, label="Phonemes") b.click(run, inputs=[t, r], outputs=[out]) demo.queue().launch()