abhisheky127 commited on
Commit
3ba7cbd
1 Parent(s): f85b6c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import MarianMTModel, MarianTokenizer
3
+
4
+ # Load English to Arabic translation model and tokenizer
5
+ en_ar_model_name = "Helsinki-NLP/opus-mt-en-ar"
6
+ en_ar_model = MarianMTModel.from_pretrained(en_ar_model_name)
7
+ en_ar_tokenizer = MarianTokenizer.from_pretrained(en_ar_model_name)
8
+
9
+ # Load Arabic to English translation model and tokenizer
10
+ ar_en_model_name = "Helsinki-NLP/opus-mt-ar-en"
11
+ ar_en_model = MarianMTModel.from_pretrained(ar_en_model_name)
12
+ ar_en_tokenizer = MarianTokenizer.from_pretrained(ar_en_model_name)
13
+
14
+ def translate_en_to_ar(text):
15
+ inputs = en_ar_tokenizer.encode(text, return_tensors="pt")
16
+ translation = en_ar_model.generate(inputs, max_length=128)
17
+ translated_text = en_ar_tokenizer.decode(translation[0], skip_special_tokens=True)
18
+ return translated_text
19
+
20
+ def translate_ar_to_en(text):
21
+ inputs = ar_en_tokenizer.encode(text, return_tensors="pt")
22
+ translation = ar_en_model.generate(inputs, max_length=128)
23
+ translated_text = ar_en_tokenizer.decode(translation[0], skip_special_tokens=True)
24
+ return translated_text
25
+
26
+ # Create Gradio interfaces for both translation directions
27
+ en_ar_interface = gr.Interface(
28
+ fn=translate_en_to_ar,
29
+ inputs="text",
30
+ outputs="text",
31
+ title="English to Arabic Translation",
32
+ description="Translate English text to Arabic."
33
+ )
34
+
35
+ ar_en_interface = gr.Interface(
36
+ fn=translate_ar_to_en,
37
+ inputs="text",
38
+ outputs="text",
39
+ title="Arabic to English Translation",
40
+ description="Translate Arabic text to English."
41
+ )
42
+
43
+ # Combine the interfaces in a single app
44
+ app = gr.Interface(
45
+ [en_ar_interface, ar_en_interface],
46
+ layout="horizontal",
47
+ title="Translation App",
48
+ description="Translate text between English and Arabic."
49
+ )
50
+
51
+ if __name__ == "__main__":
52
+ app.launch()