Spaces:
Sleeping
Sleeping
File size: 1,178 Bytes
bb5e34d 85796ab bb5e34d 1463b49 bb5e34d | 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 26 27 28 29 30 31 32 33 34 35 |
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
|