File size: 922 Bytes
d7ee302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
import gradio as gr


# Load translation pipelines
translator_en_to_ar = pipeline("text2text-generation", model="BoghdadyJR/en-ar-model")
translator_ar_to_en = pipeline("text2text-generation", model="BoghdadyJR/ar-en-model")

def translate(text, direction):
    if direction == "en_to_ar":
        return translator_en_to_ar(text)[0]['generated_text']
    else:  # direction == "ar_to_en"
        return translator_ar_to_en(text)[0]['generated_text']


def translate_text(text, direction):
    return translate(text, direction)

# Define Gradio interface with a switch button
iface = gr.Interface(
    fn=translate_text,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter text here..."),
        gr.Radio(choices=["en_to_ar", "ar_to_en"], label="Translation Direction", value="en_to_ar")
    ],
    outputs="text",
    title="Translation"
)

# Launch the interface
iface.launch()