reza200200 commited on
Commit
b45218c
·
verified ·
1 Parent(s): d4d32b9

Created app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import MarianMTModel, MarianTokenizer
2
+ import gradio as gr
3
+
4
+ # Load model and tokenizer
5
+ model_name = "Helsinki-NLP/opus-mt-en-fa"
6
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
7
+ model = MarianMTModel.from_pretrained(model_name)
8
+
9
+ # Translation function
10
+ def translate(text):
11
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
12
+ translated = model.generate(**inputs)
13
+ return tokenizer.decode(translated[0], skip_special_tokens=True)
14
+
15
+ # Gradio Interface
16
+ interface = gr.Interface(
17
+ fn=translate,
18
+ inputs=gr.Textbox(lines=5, placeholder="Enter English text here..."),
19
+ outputs=gr.Textbox(label="Translated to Persian"),
20
+ title="English to Persian Translator",
21
+ description="Type text in English and get the Persian translation."
22
+ )
23
+ interface.launch()