import gradio as gr from transformers import pipeline LANGUAGES = ["Spanish", "French", "German"] default_lang = "Spanish" title = "🦸TraveLingo" description = "Your AI-powered native translation buddy" more_description = "Give 🦸TraveLingo a try and get your English sentence translated to the destination language on the fly!" pipeline_1 = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es") pipeline_2 = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr") pipeline_3 = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de") def fn(text, lang_choice): if lang_choice=="Spanish": return pipeline_1(text)[0]["translation_text"] elif lang_choice=="French": return pipeline_2(text)[0]["translation_text"] elif lang_choice=="German": return pipeline_3(text)[0]["translation_text"] with gr.Blocks() as blocks: gr.Markdown("

" + title + "

") gr.Markdown("

" + description + "

") gr.Markdown("
" + more_description + "
") with gr.Row():# equal_height=False with gr.Column():# variant="panel" textbox = gr.Textbox( label="English Sentence", placeholder = "Hi, my name is Harish and I live in Bangalore.", max_lines=3, ) radio = gr.Radio( label="Destination Language", choices=LANGUAGES, value=default_lang ) with gr.Row():# mobile_collapse=False submit = gr.Button("Translate", variant="primary") output = gr.Text(label="Translated Sentence", interactive=False) submit.click( fn, [textbox, radio], output, ) blocks.launch()