Spaces:
Sleeping
Sleeping
| import os | |
| os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0" | |
| from transformers import MBart50TokenizerFast, MBartForConditionalGeneration | |
| import gradio as gr | |
| # ---- Load model and tokenizer ---- | |
| model_name = "Mudasir692/mbart-eng-ur" | |
| tokenizer = MBart50TokenizerFast.from_pretrained(model_name, src_lang="en_XX", tgt_lang="ur_PK") | |
| model = MBartForConditionalGeneration.from_pretrained(model_name) | |
| # ---- Translation function ---- | |
| def translate_to_urdu(text): | |
| if not text.strip(): | |
| return "Please enter some English text." | |
| inputs = tokenizer(text, return_tensors="pt", padding=True) | |
| translated_tokens = model.generate(**inputs) | |
| urdu_output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True) | |
| return urdu_output | |
| # ---- Gradio Interface ---- | |
| examples = [ | |
| ["How are you?"], | |
| ["Today is a beautiful day."], | |
| ["Where are you going?"], | |
| ["I am learning Artificial Intelligence."], | |
| ["Thank you very much!"] | |
| ] | |
| app = gr.Interface( | |
| fn=translate_to_urdu, | |
| inputs=gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here...", lines=2), | |
| outputs=gr.Textbox(label="Urdu Translation", lines=2), | |
| examples=examples, | |
| title="π English β Urdu Translator", | |
| description=""" | |
| <div style='text-align:center;'> | |
| <h3>Translate English sentences into Urdu using a fine-tuned mBART model.</h3> | |
| <p style='color:gray;'>Built by <b>Khurram Basharat</b> β powered by Transformers & Gradio.</p> | |
| <p><i>β³ Please wait a few seconds for the model to load when you first open the app.</i></p> | |
| </div> | |
| """, | |
| theme="soft", | |
| css=""" | |
| body { | |
| background: linear-gradient(to bottom right, #dff9fb, #c7ecee); | |
| font-family: 'Segoe UI', sans-serif; | |
| } | |
| .gr-button-primary { | |
| background-color: #1e3799 !important; | |
| color: white !important; | |
| } | |
| """, | |
| ) | |
| app.launch() | |