Bushra346 commited on
Commit
38f0f14
ยท
verified ยท
1 Parent(s): d99313e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -55
app.py CHANGED
@@ -1,59 +1,79 @@
 
1
  import gradio as gr
 
 
2
  import requests
3
- from TTS.api import TTS
4
-
5
- # โœ… Load multilingual TTS model (Coqui or Hugging Face compatible)
6
- tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
7
-
8
- # โœ… Use correct API endpoint
9
- def get_quran_ayat(surah_num, ayat_num):
10
- try:
11
- url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
12
- response = requests.get(url)
13
- response.raise_for_status() # Raise HTTPError for bad response
14
- data = response.json()
15
-
16
- # Check if data is valid
17
- if 'data' in data and len(data['data']) >= 2:
18
- arabic_text = data['data'][0]['text']
19
- translation = data['data'][1]['text']
20
- return arabic_text, translation
21
- else:
22
- return "Ayat not found.", "Translation not found."
23
-
24
- except Exception as e:
25
- return f"API Error: {e}", ""
26
-
27
- # โœ… Main function for Gradio
28
- def qari_bot(surah, ayat):
29
- arabic_text, translation = get_quran_ayat(surah, ayat)
30
- if "Error" in arabic_text or "not found" in arabic_text:
31
- return arabic_text, translation, None
32
-
33
- # Generate TTS
34
- tts.tts_to_file(
35
- text=arabic_text,
36
- speaker_wav=None,
37
- language="ar",
38
- file_path="ayat.wav"
 
 
 
 
 
 
 
 
 
 
 
39
  )
40
- return arabic_text, translation, "ayat.wav"
41
-
42
- # โœ… Gradio Interface
43
- interface = gr.Interface(
44
- fn=qari_bot,
45
- inputs=[
46
- gr.Number(label="Surah Number", value=1),
47
- gr.Number(label="Ayat Number", value=1)
48
- ],
49
- outputs=[
50
- gr.Textbox(label="๐Ÿ“œ Arabic Ayat"),
51
- gr.Textbox(label="๐ŸŒ Translation (English)"),
52
- gr.Audio(label="๐Ÿ”Š Listen to Ayat")
53
- ],
54
- title="๐Ÿ“– AI Qari Bot",
55
- description="Enter Surah & Ayat number to hear the Quran Ayat with AI voice and translation. Example: Surah 1, Ayat 1 (Al-Fatiha)"
56
- )
57
-
58
- interface.launch()
 
 
 
 
 
 
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