Spaces:
Runtime error
Runtime error
Goruut G2P
Browse files
app.py
CHANGED
|
@@ -21,34 +21,22 @@ try:
|
|
| 21 |
except ImportError:
|
| 22 |
PHONEMIZER_AVAILABLE = False
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
CHAR_LIMIT = None if IS_DUPLICATE else 5000
|
| 25 |
models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
|
| 26 |
pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'ab'}
|
| 27 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
| 28 |
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
|
| 29 |
|
| 30 |
-
# Language mappings similar to StyleTTS
|
| 31 |
-
LANG_NAMES = {
|
| 32 |
-
'en-us': 'english',
|
| 33 |
-
'cs': 'czech',
|
| 34 |
-
'da': 'danish',
|
| 35 |
-
'nl': 'dutch',
|
| 36 |
-
'et': 'estonian',
|
| 37 |
-
'fi': 'finnish',
|
| 38 |
-
'fr': 'french',
|
| 39 |
-
'de': 'german',
|
| 40 |
-
'el': 'greek',
|
| 41 |
-
'it': 'italian',
|
| 42 |
-
'no': 'norwegian',
|
| 43 |
-
'pl': 'polish',
|
| 44 |
-
'pt': 'portuguese',
|
| 45 |
-
'ru': 'russian',
|
| 46 |
-
'sl': 'slovene',
|
| 47 |
-
'es': 'spanish',
|
| 48 |
-
'sv': 'swedish',
|
| 49 |
-
'tr': 'turkish',
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
def text_to_ipa(text, lang='en-us'):
|
| 53 |
"""Convert text to IPA using phonemizer or return original text"""
|
| 54 |
if not PHONEMIZER_AVAILABLE:
|
|
@@ -93,7 +81,8 @@ def generate_first(text, voice='af_heart', speed=1, use_gpu=CUDA_AVAILABLE, lang
|
|
| 93 |
|
| 94 |
# Convert text to IPA if not English
|
| 95 |
if lang != 'en-us':
|
| 96 |
-
text =
|
|
|
|
| 97 |
|
| 98 |
pipeline = pipelines[voice[0]]
|
| 99 |
pack = pipeline.load_voice(voice)
|
|
@@ -185,6 +174,40 @@ def get_frankenstein():
|
|
| 185 |
with open('frankenstein5k.md', 'r') as r:
|
| 186 |
return r.read().strip()
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
CHOICES = {
|
| 189 |
'🇺🇸 🚺 Heart ❤️': 'af_heart',
|
| 190 |
'🇺🇸 🚺 Bella 🔥': 'af_bella',
|
|
@@ -299,13 +322,23 @@ with gr.Blocks() as app:
|
|
| 299 |
info='GPU is usually faster, but has a usage quota',
|
| 300 |
interactive=CUDA_AVAILABLE
|
| 301 |
)
|
| 302 |
-
with gr.Row(
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
|
| 310 |
random_btn = gr.Button('🎲 Random Quote 💬', variant='secondary')
|
| 311 |
with gr.Row():
|
|
|
|
| 21 |
except ImportError:
|
| 22 |
PHONEMIZER_AVAILABLE = False
|
| 23 |
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
from pygoruut.pygoruut import Pygoruut, PygoruutLanguages
|
| 27 |
+
pygoruut = Pygoruut()
|
| 28 |
+
goruut_langs = PygoruutLanguages()
|
| 29 |
+
# global_phonemizer_en = phonemizer.backend.EspeakBackend(language='en-us', preserve_punctuation=True, with_stress=True)
|
| 30 |
+
PYGORUUT_AVAILABLE = True
|
| 31 |
+
except ImportError:
|
| 32 |
+
PYGORUUT_AVAILABLE = False
|
| 33 |
+
|
| 34 |
CHAR_LIMIT = None if IS_DUPLICATE else 5000
|
| 35 |
models = {gpu: KModel().to('cuda' if gpu else 'cpu').eval() for gpu in [False] + ([True] if CUDA_AVAILABLE else [])}
|
| 36 |
pipelines = {lang_code: KPipeline(lang_code=lang_code, model=False) for lang_code in 'ab'}
|
| 37 |
pipelines['a'].g2p.lexicon.golds['kokoro'] = 'kˈOkəɹO'
|
| 38 |
pipelines['b'].g2p.lexicon.golds['kokoro'] = 'kˈQkəɹQ'
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def text_to_ipa(text, lang='en-us'):
|
| 41 |
"""Convert text to IPA using phonemizer or return original text"""
|
| 42 |
if not PHONEMIZER_AVAILABLE:
|
|
|
|
| 81 |
|
| 82 |
# Convert text to IPA if not English
|
| 83 |
if lang != 'en-us':
|
| 84 |
+
text = goruut_phonemize(text, lang, False, False)
|
| 85 |
+
# text = text_to_ipa(text, lang)
|
| 86 |
|
| 87 |
pipeline = pipelines[voice[0]]
|
| 88 |
pack = pipeline.load_voice(voice)
|
|
|
|
| 174 |
with open('frankenstein5k.md', 'r') as r:
|
| 175 |
return r.read().strip()
|
| 176 |
|
| 177 |
+
def filter_languages(search_text, selected_languages):
|
| 178 |
+
all_languages = languages.get_all_supported_languages()
|
| 179 |
+
|
| 180 |
+
# Extract last entry from search input
|
| 181 |
+
search_terms = search_text.replace(",,", ",").split(",") if search_text else []
|
| 182 |
+
last_term = search_terms[-1] if search_terms else ""
|
| 183 |
+
|
| 184 |
+
# Filter available languages
|
| 185 |
+
filtered = [lang for lang in all_languages if last_term == "" or (last_term.lower() in lang.lower())]
|
| 186 |
+
|
| 187 |
+
# If no results, show a message instead
|
| 188 |
+
if not filtered:
|
| 189 |
+
filtered = ["No match found..."]
|
| 190 |
+
else:
|
| 191 |
+
filtered = [filtered[0] + "..."] + filtered
|
| 192 |
+
|
| 193 |
+
return gr.update(choices=filtered), filtered[0] # Keep dropdown open and selectable
|
| 194 |
+
|
| 195 |
+
def dephon_offline(txt, language_tag, is_reverse, is_punct):
|
| 196 |
+
try:
|
| 197 |
+
response = pygoruut.phonemize(language=language_tag, sentence=txt, is_reverse=is_reverse)
|
| 198 |
+
except TypeError:
|
| 199 |
+
return ''
|
| 200 |
+
if not response or not response.Words:
|
| 201 |
+
return ''
|
| 202 |
+
if is_punct:
|
| 203 |
+
phonetic_line = str(response)
|
| 204 |
+
else:
|
| 205 |
+
phonetic_line = " ".join(word.Phonetic for word in response.Words)
|
| 206 |
+
return phonetic_line
|
| 207 |
+
|
| 208 |
+
def goruut_phonemize(sentence, language, is_reverse, is_punct):
|
| 209 |
+
return dephon_offline(sentence, language.strip(","), is_reverse, is_punct)
|
| 210 |
+
|
| 211 |
CHOICES = {
|
| 212 |
'🇺🇸 🚺 Heart ❤️': 'af_heart',
|
| 213 |
'🇺🇸 🚺 Bella 🔥': 'af_bella',
|
|
|
|
| 322 |
info='GPU is usually faster, but has a usage quota',
|
| 323 |
interactive=CUDA_AVAILABLE
|
| 324 |
)
|
| 325 |
+
with gr.Row(
|
| 326 |
+
if (PYGORUUT_AVAILABLE):):
|
| 327 |
+
# Goruut
|
| 328 |
+
lang = gr.Dropdown(
|
| 329 |
+
label="Available Languages",
|
| 330 |
+
choices=goruut_langs.get_all_supported_languages(),
|
| 331 |
+
interactive=True,
|
| 332 |
+
allow_custom_value=False
|
| 333 |
+
)
|
| 334 |
+
else:
|
| 335 |
+
# G2P
|
| 336 |
+
lang = gr.Dropdown(
|
| 337 |
+
LANGUAGE_CHOICES,
|
| 338 |
+
value='en-us',
|
| 339 |
+
label="Language",
|
| 340 |
+
info="Select language for G2P processing"
|
| 341 |
+
)
|
| 342 |
speed = gr.Slider(minimum=0.5, maximum=2, value=1, step=0.1, label='Speed')
|
| 343 |
random_btn = gr.Button('🎲 Random Quote 💬', variant='secondary')
|
| 344 |
with gr.Row():
|