guymorlan commited on
Commit
964b4e8
1 Parent(s): 7da7e15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -8
app.py CHANGED
@@ -11,12 +11,34 @@ transliterator = pipeline(task="translation", model="guymorlan/DialectTransliter
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):
 
 
 
15
  inputs = [f"{val} {input_text}" for val in dialects.values()]
 
 
 
 
 
 
 
 
 
 
16
  result = translator_en2ar(inputs)
17
- return result[0]["translation_text"], result[1]["translation_text"], result[2]["translation_text"], result[3]["translation_text"]
 
 
 
 
 
 
18
 
19
  def translate_arabic(input_text):
 
 
 
20
  result = translator_ar2en([input_text])
21
  return result[0]["translation_text"]
22
 
@@ -28,7 +50,9 @@ def get_audio(input_text):
28
  speech_synthesis_result = speech_synthesizer.speak_text_async(input_text).get()
29
  return f"{input_text}.wav"
30
 
31
- def get_transliteration(input_text):
 
 
32
  result = transliterator([input_text])
33
  return result[0]["translation_text"]
34
 
@@ -46,6 +70,10 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
46
  input_text = gr.Textbox(label="Input", placeholder="Enter English text", lines=1)
47
  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)
48
  btn = gr.Button("Translate", label="Translate")
 
 
 
 
49
  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.")
50
 
51
  with gr.Column():
@@ -58,10 +86,9 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
58
  audio = gr.Audio(label="Audio - Palestinian", interactive=False)
59
  audio_button = gr.Button("Get Audio", label="Click Here to Get Audio")
60
  audio_button.click(get_audio, inputs=[pal], outputs=[audio])
61
- btn.click(translate_english,inputs=input_text, outputs=[pal, sy, lb, eg])
62
- input_text.submit(translate_english, inputs=input_text, outputs=[pal, sy, lb, eg])
63
- pal.change(get_transliteration, inputs=[pal], outputs=[pal_translit])
64
-
65
  with gr.Tab('From Levantine Arabic to English'):
66
  with gr.Row():
67
  with gr.Column():
@@ -72,7 +99,6 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
72
  with gr.Column():
73
  eng = gr.Textbox(label="English", lines=1, elem_id="liter")
74
  btn.click(translate_arabic,inputs=input_text, outputs=[eng])
75
-
76
  with gr.Tab("Transliterate"):
77
  with gr.Row():
78
  with gr.Column():
@@ -86,3 +112,4 @@ with gr.Blocks(title = "English to Levantine Arabic", css=css, theme="default")
86
 
87
 
88
  demo.launch()
 
 
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:
40
+ return ""
41
+
42
  result = translator_ar2en([input_text])
43
  return result[0]["translation_text"]
44
 
 
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('From Levantine Arabic to English'):
93
  with gr.Row():
94
  with gr.Column():
 
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():
 
112
 
113
 
114
  demo.launch()
115
+