Spaces:
Running
on
Zero
Running
on
Zero
import gradio as gr | |
model = None | |
tokenizer = None | |
# device = 0 if torch.cuda.is_available() else -1 | |
LANGUAGES = { | |
"Hindi": "hin_Deva", | |
"Bengali": "ben_Beng", | |
"Telugu": "tel_Telu", | |
"Marathi": "mar_Deva", | |
"Tamil": "tam_Taml", | |
"Urdu": "urd_Arab", | |
"Gujarati": "guj_Gujr", | |
"Kannada": "kan_Knda", | |
"Odia": "ori_Orya", | |
"Malayalam": "mal_Mlym", | |
"Punjabi": "pan_Guru", | |
"Assamese": "asm_Beng", | |
"Maithili": "mai_Mith", | |
"Santali": "sat_Olck", | |
"Kashmiri": "kas_Arab", | |
"Nepali": "nep_Deva", | |
"Sindhi": "snd_Arab", | |
"Konkani": "kok_Deva", | |
"Dogri": "dgo_Deva", | |
"Manipuri": "mni_Beng", | |
"Bodo": "brx_Deva" | |
} | |
def translate(src_lang, text, tgt_lang): | |
return "Translation output will appear here..." | |
def store_feedback(rating, feedback_text): | |
if not rating: | |
gr.Warning("Please select a rating before submitting feedback.", duration=5) | |
return None | |
if not feedback_text or feedback_text.strip() == "": | |
gr.Warning("Please provide some feedback before submitting.", duration=5) | |
return None | |
gr.Info("Feedback submitted successfully!") | |
return "Thank you for your feedback!" | |
css = """ | |
#col-container {max-width: 80%; margin-left: auto; margin-right: auto;} | |
#header {text-align: center;} | |
.message { font-size: 1.2em; } | |
#feedback-section { margin-top: 30px; border-top: 1px solid #ddd; padding-top: 20px; } | |
""" | |
with gr.Blocks(theme=gr.themes.Default(), css=css) as demo: | |
gr.Markdown("# IndicTrans3 Demo π", elem_id="header") | |
gr.Markdown("Translate text between multiple Indic languages using the latest IndicTrans3 model from AI4Bharat. This model is trained on the --- dataset and supports translation to 22 Indic languages. Setting a state-of-the-art benchmark on multiple translation tasks, IndicTrans3 is a powerful model that can handle complex translation tasks with ease.", elem_id="description") | |
with gr.Column(elem_id="col-container"): | |
with gr.Row(): | |
with gr.Column(): | |
src_lang = gr.Dropdown( | |
["English"], | |
value="English", | |
label="Translate From", | |
elem_id="translate-from" | |
) | |
text_input = gr.Textbox( | |
placeholder="Enter text to translate...", | |
label="", | |
lines=10, | |
max_lines=100, | |
elem_id="input-text" | |
) | |
with gr.Column(): | |
tgt_lang = gr.Dropdown( | |
list(LANGUAGES.keys()), | |
value="Hindi", | |
label="Translate To", | |
elem_id="translate-to" | |
) | |
text_output = gr.Textbox( | |
label="", | |
lines=10, | |
max_lines=100, | |
elem_id="output-text" | |
) | |
btn_submit = gr.Button("Translate") | |
btn_submit.click(fn=translate, inputs=[src_lang, text_input, tgt_lang], outputs=text_output) | |
gr.Examples( | |
examples=[ | |
["English", "Hello, how are you today? I hope you're doing well.", "Telugu"], | |
["English", "Hello, how are you today? I hope you're doing well.", "Punjabi"], | |
["English", "Hello, how are you today? I hope you're doing well.", "Hindi"], | |
["English", "Hello, how are you today? I hope you're doing well.", "Marathi"], | |
["English", "Hello, how are you today? I hope you're doing well.", "Malayalam"] | |
], | |
inputs=[src_lang, text_input, tgt_lang], | |
outputs=text_output, | |
fn=translate, | |
cache_examples=True, | |
examples_per_page=5 | |
) | |
with gr.Column(elem_id="feedback-section"): | |
gr.Markdown("## Rate Translation & Provide Feedback π") | |
gr.Markdown("Help us improve the translation quality by providing your feedback and rating.") | |
with gr.Row(): | |
rating = gr.Radio( | |
["1", "2", "3", "4", "5"], | |
label="Translation Rating (1-5)" | |
) | |
feedback_text = gr.Textbox( | |
placeholder="Share your feedback about the translation...", | |
label="Feedback", | |
lines=3 | |
) | |
feedback_submit = gr.Button("Submit Feedback") | |
feedback_result = gr.Textbox(label="", visible=False) | |
feedback_submit.click( | |
fn=store_feedback, | |
inputs=[rating, feedback_text], | |
outputs=feedback_result | |
) | |
demo.launch() | |