farsi / app.py
reza200200's picture
Update app.py
fe44a86 verified
raw
history blame contribute delete
903 Bytes
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()