Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ==============================
|
| 2 |
+
# 🟢 Step 2: Import Required Packages
|
| 3 |
+
# ==============================
|
| 4 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# ==============================
|
| 8 |
+
# 🟢 Step 3: Load Translation Model
|
| 9 |
+
# ==============================
|
| 10 |
+
model_name = "Helsinki-NLP/opus-mt-en-ur" # English → Urdu model
|
| 11 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 12 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
# ==============================
|
| 15 |
+
# 🟢 Step 4: Define Translation Function
|
| 16 |
+
# ==============================
|
| 17 |
+
def translate_to_urdu(text):
|
| 18 |
+
if not text.strip():
|
| 19 |
+
return "Please enter some English text."
|
| 20 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 21 |
+
translated = model.generate(**inputs, max_length=100)
|
| 22 |
+
urdu_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 23 |
+
return urdu_text
|
| 24 |
+
|
| 25 |
+
# ==============================
|
| 26 |
+
# 🟢 Step 5: Create Gradio Interface
|
| 27 |
+
# ==============================
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=translate_to_urdu,
|
| 30 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter English text here..."),
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="🌍 English to Urdu Translator",
|
| 33 |
+
description="Translate English sentences into Urdu using a pretrained Hugging Face model."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# ==============================
|
| 37 |
+
# 🟢 Step 6: Launch App
|
| 38 |
+
# ==============================
|
| 39 |
+
interface.launch(share=True)
|