File size: 2,535 Bytes
38f0f14
8ff4e0b
38f0f14
 
8ff4e0b
38f0f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ff4e0b
38f0f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d99313e
1
2
3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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