Spaces:
Runtime error
Runtime error
Created app.py
Browse files
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()
|