Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
from AinaTheme import theme | |
import phonemizer | |
from dotenv import load_dotenv | |
import logging | |
load_dotenv() | |
MAX_INPUT_TEXT_LEN = int(os.environ.get("MAX_INPUT_TEXT_LEN", default=325)) | |
critical_logger = logging.getLogger("phonemizer") | |
critical_logger.setLevel(logging.CRITICAL) | |
# init phonemizers | |
global_phonemizer_central = phonemizer.backend.EspeakBackend( | |
language="ca", | |
preserve_punctuation=True, | |
with_stress=True, | |
language_switch="remove-flags", | |
logger=critical_logger, | |
) | |
global_phonemizer_valencia = phonemizer.backend.EspeakBackend( | |
language="ca-va", | |
preserve_punctuation=True, | |
with_stress=True, | |
language_switch="remove-flags", | |
logger=critical_logger, | |
) | |
global_phonemizer_occidental = phonemizer.backend.EspeakBackend( | |
language="ca-nw", | |
preserve_punctuation=True, | |
with_stress=True, | |
language_switch="remove-flags", | |
logger=critical_logger, | |
) | |
global_phonemizer_balear = phonemizer.backend.EspeakBackend( | |
language="ca-ba", | |
preserve_punctuation=True, | |
with_stress=True, | |
language_switch="remove-flags", | |
logger=critical_logger, | |
) | |
global_phonemizer_algueres = phonemizer.backend.EspeakBackend( | |
language="ca-al", | |
preserve_punctuation=True, | |
with_stress=True, | |
language_switch="remove-flags", | |
logger=critical_logger, | |
) | |
global_phonemizer_rossellones = phonemizer.backend.EspeakBackend( | |
language="ca-ro", | |
preserve_punctuation=True, | |
with_stress=True, | |
language_switch="remove-flags", | |
logger=critical_logger, | |
) | |
def phonemiser(text, dialect): | |
phonemizers = {"Central": global_phonemizer_central, | |
"Valencia": global_phonemizer_valencia, | |
"Occidental": global_phonemizer_occidental, | |
"Balear": global_phonemizer_balear, | |
"Alguerès": global_phonemizer_algueres, | |
"Rosellonès": global_phonemizer_rossellones} | |
fonemitzador = phonemizers[dialect] #Set correct dialect for the phonemiser | |
# synthesize | |
fonemes = fonemitzador.phonemize([text], strip=True, njobs=1)[0] | |
return fonemes | |
title = "Transcripció fonètica multidialectal en català️" | |
description=""" | |
Transcripció fonètica per a diferents dialectes del català mitjançant eSpeak. | |
Phonetic transcription for different dialects of Catalan | |
using eSpeak. | |
repo: https://github.com/projecte-aina/espeak-ng/tree/dev-ca | |
""" | |
def submit_input(input_, dialect): | |
output = None | |
if input_ is not None and len(input_) < MAX_INPUT_TEXT_LEN: | |
output = phonemiser(input_, dialect) | |
else: | |
gr.Warning(f"Your text exceeds the {MAX_INPUT_TEXT_LEN}-character limit.") | |
return output | |
def change_interactive(text): | |
input_state = text | |
if input_state.strip() != "": | |
return gr.update(interactive = True) | |
else: | |
return gr.update(interactive = False) | |
def clean(): | |
return ( | |
None, | |
None, | |
) | |
with gr.Blocks(theme=theme) as app: | |
gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>") | |
gr.Markdown(description) | |
with gr.Row(equal_height=False, variant="panel"): | |
with gr.Column(variant='panel'): | |
input_ = gr.Textbox( | |
label="Text", | |
value="Les coses importants són les que no ho semblen.", | |
lines=4 | |
) | |
dialect = gr.Dropdown(label="Dialect", choices=["Central", "Valencia", "Occidental", "Balear", "Alguerès", "Rosellonès"], value="Central") | |
with gr.Row(variant="panel"): | |
clear_btn = gr.Button( | |
"Clean", | |
) | |
submit_btn = gr.Button( | |
"Submit", | |
variant="primary", | |
) | |
with gr.Column(variant='panel'): | |
output = gr.Textbox( | |
label="Output", | |
interactive=False, | |
show_copy_button=True | |
) | |
gr.Examples( | |
label="Examples", | |
examples=[ | |
["Les coses importants són les que no ho semblen.", "Central"], | |
["Les coses importants són les que no ho semblen.", "Valencia",], | |
["Les coses importants són les que no ho semblen.", "Occidental",], | |
["Les coses importants són les que no ho semblen.","Balear"], | |
], | |
inputs=[input_, dialect], | |
outputs=output, | |
fn=submit_input) | |
for button in [submit_btn, clear_btn]: | |
input_.change(fn=change_interactive, inputs=[input_], outputs=button, api_name=False) | |
clear_btn.click(fn=clean, inputs=[], outputs=[input_, output] , queue=False, api_name=False) | |
submit_btn.click(fn=submit_input, inputs=[input_, dialect], outputs=output, api_name="get-results", concurrency_limit=2) | |
app.launch(show_api=True, server_name="0.0.0.0", server_port=7860) | |