AlexK-PL's picture
Update app.py
c8b6889 verified
import gradio as gr
import os
from AinaTheme import theme
from espeak_phonemizer import Phonemizer
from dotenv import load_dotenv
load_dotenv()
MAX_INPUT_TEXT_LEN = int(os.environ.get("MAX_INPUT_TEXT_LEN", default=325))
#fonemitzador = Phonemizer("ca")
def phonemiser(text, dialect):
dialects = {"Central": "ca", "Valencian": "ca-va", "North-West": "ca-nw", "Balear": "ca-ba"}
dialect = dialects[dialect] #Define dialect from espeak-ng-data/lang/roa/ca*
fonemitzador = Phonemizer(dialect) #Set correct dialect for the phonemiser
# synthesize
fonemes = fonemitzador.phonemize(text, keep_clause_breakers=True)
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", "Valencian", "North-West", "Balear"], 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.", "Valencian",],
["Les coses importants són les que no ho semblen.", "North-West",],
["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)