Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,79 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
return
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import soundfile as sf
|
| 5 |
import requests
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
|
| 8 |
+
# === Your small sample Quran data ===
|
| 9 |
+
QURAN = {
|
| 10 |
+
("Al-Fatiha", 1): {
|
| 11 |
+
"arabic": "ุจูุณูู
ู ุงูููููู ุงูุฑููุญูู
ููฐูู ุงูุฑููุญููู
ู",
|
| 12 |
+
"translation": "In the name of Allah, the Most Gracious, the Most Merciful."
|
| 13 |
+
},
|
| 14 |
+
("Al-Fatiha", 2): {
|
| 15 |
+
"arabic": "ุงููุญูู
ูุฏู ููููููู ุฑูุจูู ุงููุนูุงููู
ูููู",
|
| 16 |
+
"translation": "All praise is for AllahโLord of all worlds."
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
# === TTS Model ===
|
| 21 |
+
device = torch.device('cpu')
|
| 22 |
+
local_file = torch.hub.load('snakers4/silero-models',
|
| 23 |
+
'download_models',
|
| 24 |
+
language='ar',
|
| 25 |
+
speaker='v3_ar')
|
| 26 |
+
model = torch.hub.load('snakers4/silero-models',
|
| 27 |
+
'silero_tts',
|
| 28 |
+
language='ar',
|
| 29 |
+
speaker='v3_ar')
|
| 30 |
+
|
| 31 |
+
# === OpenAI Client ===
|
| 32 |
+
client = OpenAI() # requires your OPENAI_API_KEY in your env vars
|
| 33 |
+
|
| 34 |
+
# === Recite Ayah ===
|
| 35 |
+
def recite_ayah(text):
|
| 36 |
+
audio = model.apply_tts(text=text, speaker='v3_ar')
|
| 37 |
+
sf.write('recitation.wav', audio, 48000)
|
| 38 |
+
return 'recitation.wav'
|
| 39 |
+
|
| 40 |
+
# === Explain Ayah ===
|
| 41 |
+
def explain_ayah(arabic, translation):
|
| 42 |
+
prompt = f"""You are an Islamic teacher.
|
| 43 |
+
The Ayah is: {arabic}
|
| 44 |
+
Translation: {translation}
|
| 45 |
+
Please explain this Ayah in simple Urdu in 2-3 lines."""
|
| 46 |
+
|
| 47 |
+
response = client.chat.completions.create(
|
| 48 |
+
model="gpt-4o",
|
| 49 |
+
messages=[
|
| 50 |
+
{"role": "system", "content": "You are a helpful Islamic scholar."},
|
| 51 |
+
{"role": "user", "content": prompt}
|
| 52 |
+
]
|
| 53 |
)
|
| 54 |
+
return response.choices[0].message.content.strip()
|
| 55 |
+
|
| 56 |
+
# === Gradio Interface ===
|
| 57 |
+
def qari_bot(surah, ayah):
|
| 58 |
+
key = (surah, int(ayah))
|
| 59 |
+
if key in QURAN:
|
| 60 |
+
arabic = QURAN[key]['arabic']
|
| 61 |
+
translation = QURAN[key]['translation']
|
| 62 |
+
recitation = recite_ayah(arabic)
|
| 63 |
+
tafsir = explain_ayah(arabic, translation)
|
| 64 |
+
return arabic, translation, recitation, tafsir
|
| 65 |
+
else:
|
| 66 |
+
return "Ayah not found", "", None, ""
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("# ๐ AI Qari Bot")
|
| 70 |
+
gr.Markdown("Select Surah & Ayah to listen, read translation, and get Tafsir in Urdu.")
|
| 71 |
+
|
| 72 |
+
surah = gr.Dropdown(choices=["Al-Fatiha"], label="Surah", value="Al-Fatiha")
|
| 73 |
+
ayah = gr.Number(value=1, label="Ayah Number")
|
| 74 |
+
|
| 75 |
+
btn = gr.Button("Get Ayah")
|
| 76 |
+
|
| 77 |
+
arabic = gr.Textbox(label="Arabic Text")
|
| 78 |
+
transl
|
| 79 |
|