File size: 2,089 Bytes
6fbb505
 
 
 
 
 
 
 
 
 
856bb78
 
6fbb505
856bb78
 
 
 
 
6fbb505
d1060c8
 
 
 
 
 
6fbb505
856bb78
 
 
 
 
 
 
 
 
 
d1060c8
 
856bb78
6fbb505
d1060c8
 
6fbb505
 
d1060c8
 
 
 
 
 
856bb78
 
 
 
 
 
6fbb505
 
 
 
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




from translator import Translator
import gradio as gr
import os

# Load the API key from an environment variable or a file
fanar_api_key = os.getenv("FANAR_API_KEY")
translator_en_to_ar = Translator(api_key=fanar_api_key, langpair="en-ar", model="Fanar-Shaheen-MT-1")
translator_ar_to_en = Translator(api_key=fanar_api_key, langpair="ar-en", model="Fanar-Shaheen-MT-1")

def translate_text(text, direction): 
    if direction == "English to Arabic":
        return translator_en_to_ar.translate(text)
    else:
        return translator_ar_to_en.translate(text)


def update_output_alignment(direction): 
    if direction == "English to Arabic": 
        return gr.Textbox(label="Output Text", placeholder="النص المترجم سيظهر هنا...", lines=4, interactive=False, text_align="right")
    else:
        return gr.Textbox(label="Output Text", placeholder="The translated text will appear here...", lines=4, interactive=False, text_align="left")
with gr.Blocks() as demo:
    gr.Markdown("# 🌍 Fanar Translation Demo")
    gr.Markdown("## Select the translation direction, enter your text, and get the translation !") 
    
    direction_dropdown = gr.Dropdown(
        choices = ["English to Arabic", "Arabic to English"], 
        label = "Translation Direction",
        value = "English to Arabic",
        interactive=True,
    )
    
    # I want to change the directionality of the output text depending on the direction selected
    
    input_box = gr.Textbox(label="Input Text", placeholder="Enter text to translate here...", lines=4)
    
    output_box = gr.Textbox(label="Output Text", placeholder="النص المترجم سيظهر هنا...", lines=4, interactive=False, text_align="right")

    translate_button = gr.Button("Translate")
    
    
    direction_dropdown.change(
        fn = update_output_alignment,
        inputs=direction_dropdown, 
        outputs = output_box
    )
    translate_button.click(
        fn=translate_text, 
        inputs=[input_box, direction_dropdown],
        outputs=output_box,
    )
    
demo.launch()