guymorlan commited on
Commit
3e25102
1 Parent(s): fa667f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import os
4
+ import azure.cognitiveservices.speech as speechsdk
5
+
6
+ dialects = {"Palestinian/Jordanian": "P", "Syrian": "S", "Lebanese": "L", "Egyptian": "E"}
7
+
8
+ translator = pipeline(task="translation", model="guymorlan/English2Dialect")
9
+ transliterator = pipeline(task="translation", model="guymorlan/DialectTransliterator")
10
+
11
+ speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
12
+
13
+ def translate_text(input_text):
14
+ inputs = [f"{val} {input_text}" for val in dialects.values()]
15
+ result = translator (inputs)
16
+
17
+ audio_files = []
18
+
19
+
20
+ return result[0]["translation_text"], result[1]["translation_text"], result[2]["translation_text"], result[3]["translation_text"]
21
+
22
+
23
+ def get_audio(input_text):
24
+ audio_config = speechsdk.audio.AudioOutputConfig(filename=f"{input_text}.wav")
25
+ speech_config.speech_synthesis_voice_name='ar-SY-AmanyNeural'
26
+ speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
27
+ speech_synthesis_result = speech_synthesizer.speak_text_async(input_text).get()
28
+ return f"{input_text}.wav"
29
+
30
+ def get_transliteration(input_text):
31
+ result = transliterator([input_text])
32
+ return result[0]["translation_text"]
33
+
34
+
35
+ css = """
36
+ #liter textarea, #trans textarea { font-size: 25px;}
37
+ #trans textarea { direction: rtl; };
38
+ """
39
+
40
+ with gr.Blocks(title = "English to Levantine Arabic", css=css) as demo:
41
+ with gr.Row():
42
+ with gr.Column():
43
+ input_text = gr.inputs.Textbox(label="Input", placeholder="Enter English text", lines=1)
44
+ 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)
45
+ btn = gr.Button("Translate", label="Translate")
46
+ 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.")
47
+
48
+ with gr.Column():
49
+ pal = gr.Textbox(lines=1, label="Palestinian", elem_id="trans")
50
+ pal_translit = gr.Textbox(lines=1, label="Palestinian Pronunciation", elem_id="liter")
51
+ sy = gr.Textbox(lines=1, label="Syrian", elem_id="trans")
52
+ lb = gr.Textbox(lines=1, label="Lebanese", elem_id="trans")
53
+ eg = gr.Textbox(lines=1, label="Egyptian", elem_id="trans")
54
+ with gr.Row():
55
+ audio = gr.Audio(label="Audio - Palestinian", interactive=False)
56
+ audio_button = gr.Button("Get Audio", label="Get Audio")
57
+ audio_button.click(get_audio, inputs=[pal], outputs=[audio])
58
+ btn.click(translate_text, inputs=input_text, outputs=[pal, sy, lb, eg])
59
+ input_text.submit(translate_text, inputs=input_text, outputs=[pal, sy, lb, eg])
60
+ pal.change(get_transliteration, inputs=[pal], outputs=[pal_translit])
61
+
62
+ demo.launch()