translator / app.py
LovnishVerma's picture
Update app.py
47f249d
raw
history blame contribute delete
No virus
941 Bytes
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer
# Load pre-trained MarianMT model and tokenizer for translation
model_name = "Helsinki-NLP/opus-mt-en-hi"
model = MarianMTModel.from_pretrained(model_name)
tokenizer = MarianTokenizer.from_pretrained(model_name)
# Define the translation function
def translate_text(text):
# Tokenize the input text
inputs = tokenizer(text, return_tensors="pt")
# Perform translation
translation = model.generate(**inputs)
# Decode the translated text
translated_text = tokenizer.decode(translation[0], skip_special_tokens=True)
return translated_text
# Create Gradio interface
iface = gr.Interface(
fn=translate_text,
inputs=gr.Textbox(),
outputs=gr.Textbox(),
live=True,
title="English to Hindi Translation App",
description="Translate text from English to Hindi using MarianMT.",
)
# Launch the Gradio app
iface.launch()