| import gradio as gr |
| import requests |
|
|
| API_BASE = "https://api.quran.com/v4" |
|
|
| TRANSLATORS = { |
| "English (Sahih International)": 20, |
| "Urdu (Jalandhry)": 131 |
| } |
|
|
| |
| def load_surahs(): |
| try: |
| response = requests.get(f"{API_BASE}/chapters?language=en") |
| chapters = response.json()["chapters"] |
| options = [f"{c['id']}. {c['name_simple']} ({c['translated_name']['name']})" for c in chapters] |
| mapping = {opt: c['id'] for opt, c in zip(options, chapters)} |
| return options, mapping, "" |
| except Exception as e: |
| return [], {}, f"β οΈ Error loading Surah list: {e}" |
|
|
| |
| def fetch_surah(surah_label, surah_map): |
| if surah_label not in surah_map: |
| return "β οΈ Invalid selection. Please choose a valid Surah." |
|
|
| surah_id = surah_map[surah_label] |
|
|
| try: |
| arabic_res = requests.get(f"{API_BASE}/quran/verses/uthmani?chapter_number={surah_id}") |
| eng_res = requests.get(f"{API_BASE}/quran/translations/{TRANSLATORS['English (Sahih International)']}?chapter_number={surah_id}") |
| urdu_res = requests.get(f"{API_BASE}/quran/translations/{TRANSLATORS['Urdu (Jalandhry)']}?chapter_number={surah_id}") |
|
|
| arabic_verses = arabic_res.json()["verses"] |
| eng_verses = eng_res.json()["translations"] |
| urdu_verses = urdu_res.json()["translations"] |
|
|
| output = f"π **Surah: {surah_label}**\n\n" |
| output += "Translator: **Sahih International (English)** | **Jalandhry (Urdu)**\n\n" |
|
|
| for i in range(len(arabic_verses)): |
| a = arabic_verses[i]["text_uthmani"] |
| e = eng_verses[i]["text"] |
| u = urdu_verses[i]["text"] |
| output += f"**{i+1}.** {a}\n" |
| output += f" π EN: {e}\n" |
| output += f" π UR: {u}\n\n" |
|
|
| return output.strip() |
|
|
| except Exception as e: |
| return f"β οΈ Failed to fetch surah data: {e}" |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("## π Quran Surah Viewer\nArabic + English + Urdu with Authentic Translations (Live from Quran.com)") |
|
|
| surah_dropdown = gr.Dropdown(label="Choose Surah", choices=[]) |
| output_text = gr.Textbox(label="Surah Output", lines=25) |
| view_button = gr.Button("π View Surah") |
| error_box = gr.Markdown("") |
|
|
| |
| surah_map_state = gr.State({}) |
|
|
| view_button.click( |
| fetch_surah, |
| inputs=[surah_dropdown, surah_map_state], |
| outputs=[output_text] |
| ) |
|
|
| |
| demo.load( |
| load_surahs, |
| inputs=[], |
| outputs=[surah_dropdown, surah_map_state, error_box] |
| ) |
|
|
| demo.launch() |