Bushra346 commited on
Commit
d99313e
·
verified ·
1 Parent(s): 557bf23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -2,26 +2,35 @@ import gradio as gr
2
  import requests
3
  from TTS.api import TTS
4
 
5
- # 1️⃣ Load Multilingual TTS Model
6
- # Note: This is an example, you can use other Arabic-capable TTS models from Hugging Face or Coqui.
7
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
8
 
9
- # 2️⃣ Function to fetch Ayat + translation
10
  def get_quran_ayat(surah_num, ayat_num):
11
- url = f"http://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
12
- response = requests.get(url)
13
- if response.status_code == 200:
14
- data = response.json()['data']
15
- arabic_text = data[0]['text']
16
- translation = data[1]['text']
17
- return arabic_text, translation
18
- else:
19
- return "Error fetching Ayat. Please check the Surah/Ayat number.", ""
20
-
21
- # 3️⃣ Main function for Gradio
 
 
 
 
 
 
 
22
  def qari_bot(surah, ayat):
23
  arabic_text, translation = get_quran_ayat(surah, ayat)
24
- # Generate TTS audio file
 
 
 
25
  tts.tts_to_file(
26
  text=arabic_text,
27
  speaker_wav=None,
@@ -30,7 +39,7 @@ def qari_bot(surah, ayat):
30
  )
31
  return arabic_text, translation, "ayat.wav"
32
 
33
- # 4️⃣ Gradio UI
34
  interface = gr.Interface(
35
  fn=qari_bot,
36
  inputs=[
@@ -43,12 +52,8 @@ interface = gr.Interface(
43
  gr.Audio(label="🔊 Listen to Ayat")
44
  ],
45
  title="📖 AI Qari Bot",
46
- description=(
47
- "Enter any Surah & Ayat number to hear the Quran Ayat in Arabic "
48
- "with AI voice and see its translation.\n"
49
- "Example: Surah 1, Ayat 1 (Al-Fatiha)"
50
- ),
51
  )
52
 
53
- # 5️⃣ Run on Spaces
54
  interface.launch()
 
 
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,
 
39
  )
40
  return arabic_text, translation, "ayat.wav"
41
 
42
+ # Gradio Interface
43
  interface = gr.Interface(
44
  fn=qari_bot,
45
  inputs=[
 
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
+