Akhil Koduri
Create app.py
b826719 verified
raw
history blame
1.62 kB
# app.py
# Use a pipeline as a high-level helper
from transformers import pipeline
import gradio as gr
# Initialize the translation pipeline with the google-t5/t5-base model
text_translator = pipeline("translation", model="google/t5-base")
# Dictionary mapping destination languages to their T5-compatible names
language_mapping = {
"German": "German",
"Eastern Panjabi": "Punjabi",
"Sanskrit": "Sanskrit",
"Urdu": "Urdu",
"Tamil": "Tamil",
"Telugu": "Telugu",
"Yue Chinese": "Chinese",
"Chinese (Simplified)": "Chinese",
"Chinese (Traditional)": "Chinese",
"Hindi": "Hindi",
"French": "French",
"Spanish": "Spanish"
}
def translate_text(text, destination_language):
dest_language = language_mapping.get(destination_language)
if dest_language is None:
return "Unsupported language selected."
# T5 model requires a specific format for the translation task
translation_prompt = f"translate English to {dest_language}: {text}"
translation = text_translator(translation_prompt)
return translation[0]["translation_text"]
gr.close_all()
demo = gr.Interface(
fn=translate_text,
inputs=[
gr.Textbox(label="Input text to translate", lines=6),
gr.Dropdown(list(language_mapping.keys()), label="Select destination language")
],
outputs=[gr.Textbox(label="Translated text", lines=4)],
title="Language translator (model- Google T5 Base)",
description="THIS APPLICATION WILL BE USED TO TRANSLATE ENGLISH TO MULTIPLE LANGUAGES",
theme=gr.themes.Soft(),
concurrency_limit=16
)
demo.launch()