File size: 4,159 Bytes
3e25102
 
 
 
 
 
 
7da7e15
3e25102
 
 
 
15b11b4
964b4e8
 
 
3e25102
7da7e15
964b4e8
15b11b4
3e25102
7da7e15
964b4e8
 
 
7da7e15
 
 
3e25102
 
 
 
 
 
 
 
15b11b4
 
964b4e8
15b11b4
3e25102
 
 
 
 
 
 
 
 
7da7e15
 
89ccb88
7da7e15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b11b4
 
 
7da7e15
 
 
 
 
 
 
 
 
 
 
3e25102
15b11b4
 
 
3e25102
964b4e8
15b11b4
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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_en2ar = 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_english(input_text):
    if not input_text:
        return "", "", "", ""

    inputs = [f"{val} {input_text}" for val in dialects.values()]
    result = translator_en2ar(inputs)
    
    return result[0]["translation_text"], result[1]["translation_text"], result[2]["translation_text"], result[3]["translation_text"]

def translate_arabic(input_text):
    if not input_text:
        return ""

    result = translator_ar2en([input_text])
    return result[0]["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):
    if not input_text:
        return ""

    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, theme="default") as demo:
    gr.Markdown("# Levantine Arabic Translator")
    with gr.Tab('En -> Ar'):
        with gr.Row():
            with gr.Column():
                input_text = gr.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:[email protected]). 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="Click Here to Get Audio")
                    audio_button.click(get_audio, inputs=[pal], outputs=[audio])
        btn.click(translate_english,inputs=[input_text], outputs=[pal, sy, lb, eg])
        input_text.submit(translate_english, inputs=[input_text], outputs=[pal, sy, lb, eg])
        pal.change(get_transliteration, inputs=[pal,], outputs=[pal_translit])
    with gr.Tab("Transliterate"):
        with gr.Row():
            with gr.Column():
                input_text = gr.Textbox(label="Input", placeholder="Enter Levantine Arabic text", lines=1)
                gr.Examples(["خلينا ندور على مطعم تاني", "قديش حق البندورة؟"], input_text)
                btn = gr.Button("Transliterate", label="Transliterate")
                gr.Markdown("Built by [Guy Mor-Lan](mailto:[email protected])")
            with gr.Column():
                translit = gr.Textbox(label="Transliteration", lines=1, elem_id="liter")
        btn.click(get_transliteration, inputs=input_text, outputs=[translit])





demo.launch()