Spaces:
Running
Running
| # Importing required libraries | |
| from transformers import pipeline # Hugging Face transformers for translation | |
| import gradio as gr # For building the web interface | |
| # Defining available models with descriptive names | |
| MODELS = { | |
| "T5-small (Fast)": "t5-small", # Smaller, faster model | |
| "T5-base (Better Quality)": "t5-base" # Larger, more accurate model | |
| } | |
| def translate_text(text, model_name, max_length=50): | |
| """ | |
| Translates English text to French using the specified model. | |
| Args: | |
| text (str): Input English text | |
| model_name (str): Key from MODELS dictionary | |
| max_length (int): Maximum length of generated translation | |
| Returns: | |
| str: Translated French text | |
| """ | |
| # Initializing the translation pipeline | |
| translator = pipeline( | |
| "translation_en_to_fr", | |
| model=MODELS[model_name], # Select model based on user choice | |
| truncation=True # Ensure long texts are truncated properly | |
| ) | |
| # Performing translation with length control | |
| result = translator(text, max_length=max_length)[0]["translation_text"] | |
| return result | |
| # Creating Gradio interface using Blocks for advanced layout | |
| with gr.Blocks(title="Advanced English→French Translator") as demo: | |
| # ===== HEADER SECTION ===== | |
| gr.Markdown("## 🌍 English to French Translator") | |
| gr.Markdown("Powered by Hugging Face Transformers") | |
| # ===== MAIN LAYOUT (2 COLUMNS) ===== | |
| with gr.Row(): | |
| # === LEFT COLUMN (INPUTS) === | |
| with gr.Column(): | |
| # Text input box | |
| input_text = gr.Textbox( | |
| label="English Text", | |
| placeholder="Type something...", | |
| lines=3 # 3-line tall textbox | |
| ) | |
| # Model selection dropdown | |
| model_choice = gr.Dropdown( | |
| choices=list(MODELS.keys()), # Show model options | |
| value="T5-small (Fast)", # Default selection | |
| label="Model" | |
| ) | |
| # Translation length slider | |
| max_length = gr.Slider( | |
| minimum=10, # Min character length | |
| maximum=200, # Max character length | |
| value=50, # Default value | |
| label="Max Translation Length" | |
| ) | |
| # Submit button | |
| submit_btn = gr.Button("Translate", variant="primary") | |
| # === RIGHT COLUMN (OUTPUT) === | |
| with gr.Column(): | |
| # Output text box (non-editable) | |
| output_text = gr.Textbox( | |
| label="French Translation", | |
| interactive=False, # Users can't edit output | |
| lines=5 # 5-line tall textbox | |
| ) | |
| # ===== EXAMPLE SECTION ===== | |
| gr.Examples( | |
| examples=[ | |
| ["She walks in beauty, like the night Of cloudless climes and starry skies.", "T5-small (Fast)", 100], | |
| ["And all that’s best of dark and bright Meet in her aspect and her eyes.", "T5-base (Better Quality)", 110] | |
| ], | |
| inputs=[input_text, model_choice, max_length], # Auto-fill these inputs | |
| label="Try these examples!" # Section header | |
| ) | |
| # ===== EVENT HANDLING ===== | |
| submit_btn.click( | |
| fn=translate_text, # Function to call | |
| inputs=[input_text, model_choice, max_length], # Input components | |
| outputs=output_text, # Output component | |
| api_name="translate" # For API endpoint naming | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch() |