Spaces:
Runtime error
Runtime error
| # app.py | |
| import gradio as gr | |
| import torch | |
| import soundfile as sf | |
| import requests | |
| from openai import OpenAI | |
| # === Your small sample Quran data === | |
| QURAN = { | |
| ("Al-Fatiha", 1): { | |
| "arabic": "ุจูุณูู ู ุงูููููู ุงูุฑููุญูู ููฐูู ุงูุฑููุญููู ู", | |
| "translation": "In the name of Allah, the Most Gracious, the Most Merciful." | |
| }, | |
| ("Al-Fatiha", 2): { | |
| "arabic": "ุงููุญูู ูุฏู ููููููู ุฑูุจูู ุงููุนูุงููู ูููู", | |
| "translation": "All praise is for AllahโLord of all worlds." | |
| } | |
| } | |
| # === TTS Model === | |
| device = torch.device('cpu') | |
| local_file = torch.hub.load('snakers4/silero-models', | |
| 'download_models', | |
| language='ar', | |
| speaker='v3_ar') | |
| model = torch.hub.load('snakers4/silero-models', | |
| 'silero_tts', | |
| language='ar', | |
| speaker='v3_ar') | |
| # === OpenAI Client === | |
| client = OpenAI() # requires your OPENAI_API_KEY in your env vars | |
| # === Recite Ayah === | |
| def recite_ayah(text): | |
| audio = model.apply_tts(text=text, speaker='v3_ar') | |
| sf.write('recitation.wav', audio, 48000) | |
| return 'recitation.wav' | |
| # === Explain Ayah === | |
| def explain_ayah(arabic, translation): | |
| prompt = f"""You are an Islamic teacher. | |
| The Ayah is: {arabic} | |
| Translation: {translation} | |
| Please explain this Ayah in simple Urdu in 2-3 lines.""" | |
| response = client.chat.completions.create( | |
| model="gpt-4o", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful Islamic scholar."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return response.choices[0].message.content.strip() | |
| # === Gradio Interface === | |
| def qari_bot(surah, ayah): | |
| key = (surah, int(ayah)) | |
| if key in QURAN: | |
| arabic = QURAN[key]['arabic'] | |
| translation = QURAN[key]['translation'] | |
| recitation = recite_ayah(arabic) | |
| tafsir = explain_ayah(arabic, translation) | |
| return arabic, translation, recitation, tafsir | |
| else: | |
| return "Ayah not found", "", None, "" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ๐ AI Qari Bot") | |
| gr.Markdown("Select Surah & Ayah to listen, read translation, and get Tafsir in Urdu.") | |
| surah = gr.Dropdown(choices=["Al-Fatiha"], label="Surah", value="Al-Fatiha") | |
| ayah = gr.Number(value=1, label="Ayah Number") | |
| btn = gr.Button("Get Ayah") | |
| arabic = gr.Textbox(label="Arabic Text") | |
| transl | |