File size: 2,168 Bytes
8ee2458
 
 
 
 
 
 
 
 
 
6800fe7
 
8ee2458
6800fe7
 
 
 
 
 
 
8ee2458
 
 
 
 
 
 
 
 
 
 
 
 
9a35a6d
8ee2458
 
 
 
 
 
 
6800fe7
8ee2458
9a35a6d
 
 
 
8ee2458
9a35a6d
 
 
 
 
 
8ee2458
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()