Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,67 +7,51 @@ import gradio as gr
|
|
| 7 |
# ---- Load model and tokenizer ----
|
| 8 |
model_name = "abdulwaheed1/english-to-urdu-translation-mbart"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
inputs=input_box,
|
| 59 |
-
outputs=output_box,
|
| 60 |
-
examples=example_texts,
|
| 61 |
-
title="π English β Urdu Translator",
|
| 62 |
-
description="Built by Khurram Basharat β powered by mBART model fine-tuned for English to Urdu translation.",
|
| 63 |
-
theme="soft",
|
| 64 |
-
css="""
|
| 65 |
-
body {
|
| 66 |
-
background: linear-gradient(to bottom right, #dff9fb, #c7ecee);
|
| 67 |
-
font-family: 'Segoe UI', sans-serif;
|
| 68 |
-
}
|
| 69 |
-
.gr-button-primary {
|
| 70 |
-
background-color: #1e3799 !important;
|
| 71 |
-
}
|
| 72 |
-
""",
|
| 73 |
-
).launch()
|
|
|
|
| 7 |
# ---- Load model and tokenizer ----
|
| 8 |
model_name = "abdulwaheed1/english-to-urdu-translation-mbart"
|
| 9 |
|
| 10 |
+
tokenizer = MBart50TokenizerFast.from_pretrained(model_name, src_lang="en_XX", tgt_lang="ur_PK")
|
| 11 |
+
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
# ---- Translation function ----
|
| 14 |
+
def translate_to_urdu(text):
|
| 15 |
+
if not text.strip():
|
| 16 |
+
return "Please enter some English text."
|
| 17 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 18 |
+
translated_tokens = model.generate(**inputs)
|
| 19 |
+
urdu_output = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
| 20 |
+
return urdu_output
|
| 21 |
+
|
| 22 |
+
# ---- Gradio Interface ----
|
| 23 |
+
examples = [
|
| 24 |
+
["How are you?"],
|
| 25 |
+
["Today is a beautiful day."],
|
| 26 |
+
["Where are you going?"],
|
| 27 |
+
["I am learning Artificial Intelligence."],
|
| 28 |
+
["Thank you very much!"]
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
app = gr.Interface(
|
| 32 |
+
fn=translate_to_urdu,
|
| 33 |
+
inputs=gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here...", lines=2),
|
| 34 |
+
outputs=gr.Textbox(label="Urdu Translation", lines=2),
|
| 35 |
+
examples=examples,
|
| 36 |
+
title="π English β Urdu Translator",
|
| 37 |
+
description="""
|
| 38 |
+
<div style='text-align:center;'>
|
| 39 |
+
<h3>Translate English sentences into Urdu using a fine-tuned mBART model.</h3>
|
| 40 |
+
<p style='color:gray;'>Built by <b>Khurram Basharat</b> β powered by Transformers & Gradio.</p>
|
| 41 |
+
<p><i>β³ Please wait a few seconds for the model to load when you first open the app.</i></p>
|
| 42 |
+
</div>
|
| 43 |
+
""",
|
| 44 |
+
theme="soft",
|
| 45 |
+
css="""
|
| 46 |
+
body {
|
| 47 |
+
background: linear-gradient(to bottom right, #dff9fb, #c7ecee);
|
| 48 |
+
font-family: 'Segoe UI', sans-serif;
|
| 49 |
+
}
|
| 50 |
+
.gr-button-primary {
|
| 51 |
+
background-color: #1e3799 !important;
|
| 52 |
+
color: white !important;
|
| 53 |
+
}
|
| 54 |
+
""",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|