Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import MarianMTModel, MarianTokenizer | |
| # Load Hugging Face translation model | |
| model_name = "Helsinki-NLP/opus-mt-en-ur" # ✅ Pretrained English to Urdu Model | |
| tokenizer = MarianTokenizer.from_pretrained(model_name) | |
| model = MarianMTModel.from_pretrained(model_name) | |
| # Define the translation function | |
| def translate_to_urdu(text): | |
| # Tokenize input text | |
| inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True) | |
| # Generate translation | |
| translated = model.generate(**inputs) | |
| # Decode the translation | |
| output_text = tokenizer.decode(translated[0], skip_special_tokens=True) | |
| return output_text | |
| # Create a Gradio interface for the translator | |
| translator = gr.Interface( | |
| fn=translate_to_urdu, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter English text..."), | |
| outputs="text", | |
| title="English to Urdu Translator", | |
| description="Enter English text and get the Urdu translation instantly.", | |
| theme="soft", | |
| ) | |
| # Launch the Gradio app | |
| translator.launch() | |