BaselMousi commited on
Commit
856bb78
·
1 Parent(s): a9f2612

Added support for translation direction

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -6,31 +6,39 @@ from translator import Translator
6
  import gradio as gr
7
  import os
8
 
9
-
10
  # Load the API key from an environment variable or a file
11
  fanar_api_key = os.getenv("FANAR_API_KEY")
12
- langpair = "en-ar" # also pass this as an option to the interface. The user should be able to choose the language pair
13
-
14
- translator = Translator(api_key=fanar_api_key, langpair=langpair, model="Fanar-Shaheen-MT-1")
15
 
16
- def translate_text(text):
17
- return translator.translate(text)
 
 
 
18
 
19
  with gr.Blocks() as demo:
20
- gr.Markdown("# Fanar Translator")
21
- gr.Markdown("## Translate text into Arabic Using the State of the Art Fanar Shaheen MT Model")
 
 
 
 
 
 
 
 
 
 
22
 
23
- with gr.Row():
24
- input_box = gr.Textbox(label="Input Text", placeholder="Enter text to translate here...", lines=4)
25
- with gr.Row():
26
- output_box = gr.Textbox(label="Output Text", placeholder="Translated text will appear here...", lines=4, interactive=False)
27
-
28
  translate_button = gr.Button("Translate")
29
- translate_button.click(translate_text, inputs=input_box, outputs=output_box)
30
 
31
-
32
-
33
-
 
 
 
34
  demo.launch()
35
 
36
 
 
6
  import gradio as gr
7
  import os
8
 
 
9
  # Load the API key from an environment variable or a file
10
  fanar_api_key = os.getenv("FANAR_API_KEY")
11
+ translator_en_to_ar = Translator(api_key=fanar_api_key, langpair="en-ar", model="Fanar-Shaheen-MT-1")
12
+ translator_ar_to_en = Translator(api_key=fanar_api_key, langpair="ar-en", model="Fanar-Shaheen-MT-1")
 
13
 
14
+ def translate_text(text, direction):
15
+ if direction == "English to Arabic":
16
+ return translator_en_to_ar.translate(text)
17
+ else:
18
+ return translator_ar_to_en.translate(text)
19
 
20
  with gr.Blocks() as demo:
21
+ gr.Markdown("# 🌍 Fanar Translation Demo")
22
+ gr.Markdown("## Select the translation direction, enter your text, and get the translation !")
23
+
24
+ direction_dropdown = gr.Dropdown(
25
+ choices = ["English to Arabic", "Arabic to English"],
26
+ label = "Translation Direction",
27
+ value = "English to Arabic",
28
+ interactive=True,
29
+ )
30
+
31
+ input_box = gr.Textbox(label="Input Text", placeholder="Enter text to translate here...", lines=4)
32
+ output_box = gr.Textbox(label="Output Text", placeholder="Translated text will appear here...", lines=4, interactive=False)
33
 
 
 
 
 
 
34
  translate_button = gr.Button("Translate")
 
35
 
36
+ translate_button.click(
37
+ fn=translate_text,
38
+ inputs=[input_box, direction_dropdown],
39
+ outputs=output_box,
40
+ )
41
+
42
  demo.launch()
43
 
44