urdu-translate / app.py
Peace-Network's picture
Update app.py
0da0633 verified
Raw
History Blame Contribute Delete
4.54 kB
import gradio as gr
import requests
from deep_translator import GoogleTranslator
def translate_to_urdu(text):
if not text or not text.strip():
raise gr.Error("Please enter some English text first.")
try:
lines = [line for line in text.split("\n") if line.strip()]
translated_lines = []
translator = GoogleTranslator(source="en", target="ur")
for line in lines:
output = translator.translate(line)
translated_lines.append(output)
return "\n".join(translated_lines)
except Exception as e:
import traceback
traceback.print_exc()
raise gr.Error(f"Translation failed: {e}")
def get_quran_verse(reference):
"""
Looks up a verified Urdu translation (Fateh Muhammad Jalandhri) for a
Quranic verse, instead of running it through a generic translator.
Reference format: surah:ayah, e.g. 9:36
"""
if not reference or not reference.strip():
raise gr.Error("Please enter a verse reference, e.g. 9:36")
ref = reference.strip()
try:
url = f"https://api.alquran.cloud/v1/ayah/{ref}/editions/quran-uthmani,ur.jalandhry"
response = requests.get(url, timeout=15)
response.raise_for_status()
data = response.json()
if data.get("code") != 200:
raise gr.Error(f"Couldn't find verse '{ref}'. Use the format surah:ayah, e.g. 9:36")
editions = data["data"]
arabic = next(e["text"] for e in editions if e["edition"]["identifier"] == "quran-uthmani")
urdu = next(e["text"] for e in editions if e["edition"]["identifier"] == "ur.jalandhry")
surah_name = editions[0]["surah"]["englishName"]
ayah_num = editions[0]["numberInSurah"]
citation = f"Surah {surah_name}, Ayah {ayah_num}"
return arabic, urdu, citation
except gr.Error:
raise
except Exception as e:
import traceback
traceback.print_exc()
raise gr.Error(f"Lookup failed: {e}")
css = """
#header {
text-align: center;
padding: 24px 0 8px;
}
#header h1 {
font-size: 32px;
font-weight: 700;
background: linear-gradient(135deg, #6366f1, #06b6d4);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 4px;
}
#header p {
color: #888;
font-size: 14px;
}
#run-btn {
background: linear-gradient(135deg, #6366f1, #06b6d4) !important;
color: white !important;
font-weight: 600 !important;
border: none !important;
}
#urdu-output textarea {
font-size: 20px !important;
direction: rtl;
text-align: right;
}
"""
with gr.Blocks(title="Peace Network Translator", css=css) as demo:
gr.HTML(
"""
<div id="header">
<h1>🌐 Peace Network Translator</h1>
<p>English text daaliye — Urdu translation instantly milega.</p>
</div>
"""
)
with gr.Tabs():
with gr.Tab("General Text"):
with gr.Row():
inp = gr.Textbox(
label="English text",
placeholder="Type or paste English text here...",
lines=8,
)
out = gr.Textbox(
label="اردو ترجمہ (Urdu translation)",
lines=8,
elem_id="urdu-output",
)
btn = gr.Button("🔄 Translate to Urdu", variant="primary", elem_id="run-btn")
btn.click(translate_to_urdu, inputs=inp, outputs=out)
with gr.Tab("Quran Verse (Verified)"):
gr.Markdown(
"Verse reference daaliye (surah:ayah format, jaise `9:36`). "
"Yeh generic AI translation nahi hai — Fateh Muhammad Jalandhri "
"ki verified, established Urdu translation database se seedha "
"nikalta hai."
)
ref_input = gr.Textbox(label="Verse reference (surah:ayah)", placeholder="e.g. 9:36")
quran_btn = gr.Button("📖 Get Verse", variant="primary", elem_id="run-btn")
arabic_out = gr.Textbox(label="Arabic (Uthmani)", lines=3)
urdu_out = gr.Textbox(label="اردو ترجمہ (جالندھری)", lines=3, elem_id="urdu-output")
citation_out = gr.Textbox(label="Reference")
quran_btn.click(
get_quran_verse,
inputs=ref_input,
outputs=[arabic_out, urdu_out, citation_out],
)
demo.queue().launch()