import gradio as gr from transformers import pipeline import os import azure.cognitiveservices.speech as speechsdk dialects = {"Palestinian/Jordanian": "P", "Syrian": "S", "Lebanese": "L", "Egyptian": "E"} translator = pipeline(task="translation", model="guymorlan/English2Dialect") transliterator = pipeline(task="translation", model="guymorlan/DialectTransliterator") speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION')) def translate_text(input_text): inputs = [f"{val} {input_text}" for val in dialects.values()] result = translator (inputs) audio_files = [] return result[0]["translation_text"], result[1]["translation_text"], result[2]["translation_text"], result[3]["translation_text"] def get_audio(input_text): audio_config = speechsdk.audio.AudioOutputConfig(filename=f"{input_text}.wav") speech_config.speech_synthesis_voice_name='ar-SY-AmanyNeural' speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) speech_synthesis_result = speech_synthesizer.speak_text_async(input_text).get() return f"{input_text}.wav" def get_transliteration(input_text): result = transliterator([input_text]) return result[0]["translation_text"] css = """ #liter textarea, #trans textarea { font-size: 25px;} #trans textarea { direction: rtl; }; """ with gr.Blocks(title = "English to Levantine Arabic", css=css) as demo: with gr.Row(): with gr.Column(): input_text = gr.inputs.Textbox(label="Input", placeholder="Enter English text", lines=1) gr.Examples(["I wanted to go to the store yesterday, but it rained", "How are you feeling today?", "Let's go to your place"], input_text) btn = gr.Button("Translate", label="Translate") gr.Markdown("Built by [Guy Mor-Lan](mailto:guy.mor@mail.huji.ac.il). Pronunciation model is specifically tailored to urban Palestinian Arabic. Text-to-speech uses Microsoft Azure's API and may provide different result from the transliterated pronunciation.") with gr.Column(): pal = gr.Textbox(lines=1, label="Palestinian", elem_id="trans") pal_translit = gr.Textbox(lines=1, label="Palestinian Pronunciation", elem_id="liter") sy = gr.Textbox(lines=1, label="Syrian", elem_id="trans") lb = gr.Textbox(lines=1, label="Lebanese", elem_id="trans") eg = gr.Textbox(lines=1, label="Egyptian", elem_id="trans") with gr.Row(): audio = gr.Audio(label="Audio - Palestinian", interactive=False) audio_button = gr.Button("Get Audio", label="Get Audio") audio_button.click(get_audio, inputs=[pal], outputs=[audio]) btn.click(translate_text, inputs=input_text, outputs=[pal, sy, lb, eg]) input_text.submit(translate_text, inputs=input_text, outputs=[pal, sy, lb, eg]) pal.change(get_transliteration, inputs=[pal], outputs=[pal_translit]) demo.launch()