BoghdadyJR
commited on
Commit
•
d7ee302
1
Parent(s):
faf7b64
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
|
5 |
+
# Load translation pipelines
|
6 |
+
translator_en_to_ar = pipeline("text2text-generation", model="BoghdadyJR/en-ar-model")
|
7 |
+
translator_ar_to_en = pipeline("text2text-generation", model="BoghdadyJR/ar-en-model")
|
8 |
+
|
9 |
+
def translate(text, direction):
|
10 |
+
if direction == "en_to_ar":
|
11 |
+
return translator_en_to_ar(text)[0]['generated_text']
|
12 |
+
else: # direction == "ar_to_en"
|
13 |
+
return translator_ar_to_en(text)[0]['generated_text']
|
14 |
+
|
15 |
+
|
16 |
+
def translate_text(text, direction):
|
17 |
+
return translate(text, direction)
|
18 |
+
|
19 |
+
# Define Gradio interface with a switch button
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=translate_text,
|
22 |
+
inputs=[
|
23 |
+
gr.Textbox(lines=2, placeholder="Enter text here..."),
|
24 |
+
gr.Radio(choices=["en_to_ar", "ar_to_en"], label="Translation Direction", value="en_to_ar")
|
25 |
+
],
|
26 |
+
outputs="text",
|
27 |
+
title="Translation"
|
28 |
+
)
|
29 |
+
|
30 |
+
# Launch the interface
|
31 |
+
iface.launch()
|