guymorlan commited on
Commit
dbacc9b
1 Parent(s): 89ccb88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -6,18 +6,34 @@ import azure.cognitiveservices.speech as speechsdk
6
  dialects = {"Palestinian/Jordanian": "P", "Syrian": "S", "Lebanese": "L", "Egyptian": "E"}
7
 
8
  translator_en2ar = 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_english(input_text):
14
  if not input_text:
15
  return "", "", "", ""
16
 
17
  inputs = [f"{val} {input_text}" for val in dialects.values()]
 
 
 
 
 
 
 
 
 
 
18
  result = translator_en2ar(inputs)
19
 
20
- return result[0]["translation_text"], result[1]["translation_text"], result[2]["translation_text"], result[3]["translation_text"]
 
 
 
 
 
21
 
22
  def translate_arabic(input_text):
23
  if not input_text:
@@ -34,10 +50,9 @@ def get_audio(input_text):
34
  speech_synthesis_result = speech_synthesizer.speak_text_async(input_text).get()
35
  return f"{input_text}.wav"
36
 
37
- def get_transliteration(input_text):
38
- if not input_text:
39
  return ""
40
-
41
  result = transliterator([input_text])
42
  return result[0]["translation_text"]
43
 
@@ -55,6 +70,10 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
55
  input_text = gr.Textbox(label="Input", placeholder="Enter English text", lines=1)
56
  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)
57
  btn = gr.Button("Translate", label="Translate")
 
 
 
 
58
  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.")
59
 
60
  with gr.Column():
@@ -67,9 +86,19 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
67
  audio = gr.Audio(label="Audio - Palestinian", interactive=False)
68
  audio_button = gr.Button("Get Audio", label="Click Here to Get Audio")
69
  audio_button.click(get_audio, inputs=[pal], outputs=[audio])
70
- btn.click(translate_english,inputs=[input_text], outputs=[pal, sy, lb, eg])
71
- input_text.submit(translate_english, inputs=[input_text], outputs=[pal, sy, lb, eg])
72
- pal.change(get_transliteration, inputs=[pal,], outputs=[pal_translit])
 
 
 
 
 
 
 
 
 
 
73
  with gr.Tab("Transliterate"):
74
  with gr.Row():
75
  with gr.Column():
@@ -81,10 +110,4 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
81
  translit = gr.Textbox(label="Transliteration", lines=1, elem_id="liter")
82
  btn.click(get_transliteration, inputs=input_text, outputs=[translit])
83
 
84
-
85
-
86
-
87
-
88
  demo.launch()
89
-
90
-
 
6
  dialects = {"Palestinian/Jordanian": "P", "Syrian": "S", "Lebanese": "L", "Egyptian": "E"}
7
 
8
  translator_en2ar = pipeline(task="translation", model="guymorlan/English2Dialect")
9
+ translator_ar2en = pipeline(task="translation", model="guymorlan/Shami2English")
10
  transliterator = pipeline(task="translation", model="guymorlan/DialectTransliterator")
11
 
12
  speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
13
 
14
+ def translate_english(input_text, include):
15
  if not input_text:
16
  return "", "", "", ""
17
 
18
  inputs = [f"{val} {input_text}" for val in dialects.values()]
19
+
20
+ sy, lb, eg = "Syrian" in include, "Lebanese" in include, "Egyptian" in include
21
+ # remove 2nd element if sy is false
22
+ if not eg:
23
+ inputs.pop()
24
+ if not lb:
25
+ inputs.pop()
26
+ if not sy:
27
+ inputs.pop()
28
+
29
  result = translator_en2ar(inputs)
30
 
31
+ pal_out = result[0]["translation_text"]
32
+ sy_out = result[1]["translation_text"] if sy else ""
33
+ lb_out = result[1 + sy]["translation_text"] if lb else ""
34
+ eg_out = result[1 + sy + lb]["translation_text"] if eg else ""
35
+
36
+ return pal_out, sy_out, lb_out, eg_out
37
 
38
  def translate_arabic(input_text):
39
  if not input_text:
 
50
  speech_synthesis_result = speech_synthesizer.speak_text_async(input_text).get()
51
  return f"{input_text}.wav"
52
 
53
+ def get_transliteration(input_text, include=["Transliteration"]):
54
+ if "Transliteration" not in include:
55
  return ""
 
56
  result = transliterator([input_text])
57
  return result[0]["translation_text"]
58
 
 
70
  input_text = gr.Textbox(label="Input", placeholder="Enter English text", lines=1)
71
  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)
72
  btn = gr.Button("Translate", label="Translate")
73
+ with gr.Row():
74
+ include = gr.CheckboxGroup(["Transliteration", "Syrian", "Lebanese", "Egyptian"],
75
+ label="Disable features to speed up translation",
76
+ value=["Transliteration", "Syrian", "Lebanese", "Egyptian"])
77
  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.")
78
 
79
  with gr.Column():
 
86
  audio = gr.Audio(label="Audio - Palestinian", interactive=False)
87
  audio_button = gr.Button("Get Audio", label="Click Here to Get Audio")
88
  audio_button.click(get_audio, inputs=[pal], outputs=[audio])
89
+ btn.click(translate_english,inputs=[input_text, include], outputs=[pal, sy, lb, eg])
90
+ input_text.submit(translate_english, inputs=[input_text, include], outputs=[pal, sy, lb, eg])
91
+ pal.change(get_transliteration, inputs=[pal, include], outputs=[pal_translit])
92
+ with gr.Tab('Ar -> En'):
93
+ with gr.Row():
94
+ with gr.Column():
95
+ input_text = gr.Textbox(label="Input", placeholder="Enter Levantine Arabic text", lines=1, elem_id="trans")
96
+ gr.Examples(["خلينا ندور على مطعم تاني", "قديش حق البندورة؟"], input_text)
97
+ btn = gr.Button("Translate", label="Translate")
98
+ gr.Markdown("Built by [Guy Mor-Lan](mailto:guy.mor@mail.huji.ac.il).")
99
+ with gr.Column():
100
+ eng = gr.Textbox(label="English", lines=1, elem_id="liter")
101
+ btn.click(translate_arabic,inputs=input_text, outputs=[eng])
102
  with gr.Tab("Transliterate"):
103
  with gr.Row():
104
  with gr.Column():
 
110
  translit = gr.Textbox(label="Transliteration", lines=1, elem_id="liter")
111
  btn.click(get_transliteration, inputs=input_text, outputs=[translit])
112
 
 
 
 
 
113
  demo.launch()