File size: 903 Bytes
fe44a86
b45218c
 
2f80280
fe44a86
 
 
b58b2d6
2f80280
b45218c
fe44a86
 
 
 
b45218c
2f80280
b45218c
 
 
 
 
 
 
 
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
from transformers import MT5ForConditionalGeneration, MT5Tokenizer
import gradio as gr

# بارگذاری مدل و توکنایزر
model_name = "persiannlp/mt5-small-parsinlu-translation_en_fa"
tokenizer = MT5Tokenizer.from_pretrained(model_name)
model = MT5ForConditionalGeneration.from_pretrained(model_name)

# تابع ترجمه
def translate(text):
    input_ids = tokenizer.encode(text, return_tensors="pt")
    outputs = model.generate(input_ids)
    translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return translated_text

# رابط کاربری Gradio
interface = gr.Interface(
    fn=translate,
    inputs=gr.Textbox(lines=5, placeholder="Enter English text here..."),
    outputs=gr.Textbox(label="Translated to Persian"),
    title="English to Persian Translator",
    description="Type text in English and get the Persian translation."
)
interface.launch()