My_Translator / app.py
superbsaeed's picture
Update app.py
85796ab verified
from transformers import MarianTokenizer, MarianMTModel
import gradio as gr
# Specify the model name for English to Urdu translation
model_name = 'Helsinki-NLP/opus-mt-en-ur'
# Load the tokenizer and model
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
def translate_en_to_ur(text):
# Tokenize the input text
inputs = tokenizer(text, return_tensors='pt', padding=True)
# Generate translation
translated = model.generate(**inputs)
# Decode the generated tokens back to text
translated_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
return translated_text[0]
# Define the Gradio interface
iface = gr.Interface(
fn=translate_en_to_ur,
inputs=gr.Textbox(lines=2, placeholder="Enter English text here..."),
outputs="text",
title="English to Urdu Translator",
description="Hello, I am a newbie in Model Deployment. I was trying to design a language translator. I am using hugging face's Marian MT model "
)
# Launch the interface (this will be run by Hugging Face Spaces)
iface.launch(share=False) # share=False is important for deployment