Divison / app.py
Rekham1110's picture
Create app.py
d8f7fac verified
raw
history blame contribute delete
963 Bytes
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer
# Load the model and tokenizer
model_name = 'Helsinki-NLP/opus-mt-en-hi' # Model for English to Hindi
model = MarianMTModel.from_pretrained(model_name)
tokenizer = MarianTokenizer.from_pretrained(model_name)
# Function to translate English to Hindi
def translate_to_hindi(text):
# Tokenize input text
tokens = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
# Perform translation
translated_tokens = model.generate(**tokens)
# Decode the translation
translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
return translated_text
# Gradio interface
iface = gr.Interface(fn=translate_to_hindi, inputs="text", outputs="text",
title="English to Hindi Translator",
description="Enter English text and get the Hindi translation.")
# Launch the interface
iface.launch()