Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install dependencies (run this once in Colab or your terminal)
|
| 2 |
+
!pip install transformers gradio torch
|
| 3 |
+
|
| 4 |
+
# ---- English → Urdu Translator App ----
|
| 5 |
+
from transformers import MBart50TokenizerFast, MBartForConditionalGeneration
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Load model and tokenizer
|
| 9 |
+
model_name = "abdulwaheed1/english-to-urdu-translation-mbart"
|
| 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 |
+
# Create a simple dashboard with Gradio
|
| 23 |
+
app = gr.Interface(
|
| 24 |
+
fn=translate_to_urdu,
|
| 25 |
+
inputs=gr.Textbox(label="Enter English Text", placeholder="Type your English sentence here..."),
|
| 26 |
+
outputs=gr.Textbox(label="Urdu Translation"),
|
| 27 |
+
title="🕌 English → Urdu Translator",
|
| 28 |
+
description="This is my First fine-tuned mBART model to translate English sentences into Urdu.",
|
| 29 |
+
theme="soft"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch app
|
| 33 |
+
app.launch(share=True) # use share=True to get a public link accessible in Chrome
|
| 34 |
+
|