Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load translation pipelines | |
| en2ur_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur") | |
| ur2en_translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en") | |
| # Translation function | |
| def translate_text(direction, input_text): | |
| if not input_text.strip(): | |
| return "Please enter some text." | |
| if direction == "English to Urdu": | |
| result = en2ur_translator(input_text, max_length=400) | |
| else: # Urdu to English | |
| result = ur2en_translator(input_text, max_length=400) | |
| return result[0]['translation_text'] | |
| # Build Gradio App with Multi-line input & Loading spinner | |
| def app(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🌐 English ⇄ Urdu Translator") | |
| with gr.Row(): | |
| direction = gr.Radio(["English to Urdu", "Urdu to English"], label="Select Translation Direction", value="English to Urdu") | |
| # Multi-line text box for input | |
| input_text = gr.Textbox(placeholder="Type your text here...", lines=6, label="Input Text") | |
| # Multi-line text box for output (show translated text) | |
| output_text = gr.Textbox(label="Translated Text", lines=6) | |
| # Button to trigger translation | |
| translate_btn = gr.Button("Translate") | |
| # Asynchronous translation and updating output | |
| translate_btn.click( | |
| fn=translate_text, | |
| inputs=[direction, input_text], | |
| outputs=output_text, | |
| show_progress=True # This shows the progress indicator | |
| ) | |
| return demo | |
| # Launch when script runs | |
| if __name__ == "__main__": | |
| app().launch() | |