asad231 commited on
Commit
62e9e47
Β·
verified Β·
1 Parent(s): 7560983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -41
app.py CHANGED
@@ -1,63 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
  from TTS.api import TTS
4
- import os
5
 
6
- # βœ… Load TTS model (Make sure model is downloaded or install TTS first)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
8
 
9
- # βœ… Get Ayah from AlQuran API
10
- def get_quran_ayat(surah_num, ayat_num):
 
 
 
 
11
  try:
12
- url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
13
  response = requests.get(url)
14
  response.raise_for_status()
15
  data = response.json()
16
 
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 "❌ Ayah not found.", "❌ Translation not found."
23
 
24
- except Exception as e:
25
- return f"❌ API Error: {e}", ""
26
 
27
- # βœ… Gradio main function
28
- def qari_bot(surah, ayah):
29
- arabic_text, translation = get_quran_ayat(surah, ayah)
30
-
31
- # Return early if error
32
- if "❌" in arabic_text:
33
- return arabic_text, translation, None
34
 
35
- # Generate audio and save to file
36
- output_path = "ayat.wav"
37
- try:
38
- tts.tts_to_file(
39
- text=arabic_text,
40
- file_path=output_path,
41
- language="ar"
42
- )
43
- return arabic_text, translation, output_path
44
  except Exception as e:
45
- return arabic_text, translation, f"❌ TTS Error: {e}"
46
 
47
- # βœ… Gradio Interface
48
  interface = gr.Interface(
49
- fn=qari_bot,
50
- inputs=[
51
- gr.Number(label="Surah Number", value=1),
52
- gr.Number(label="Ayah Number", value=1)
53
- ],
54
  outputs=[
55
- gr.Textbox(label="πŸ“œ Arabic Ayah"),
56
- gr.Textbox(label="🌐 Translation (English)"),
57
- gr.Audio(label="πŸ”Š Listen to Ayah")
58
  ],
59
  title="πŸ“– AI Qari Bot",
60
- description="Enter Surah & Ayah number to hear the Quran Ayah with AI voice and English translation. Example: Surah 1, Ayah 1 = Al-Fatiha"
61
  )
62
 
63
- interface.launch()
 
1
+ # import gradio as gr
2
+ # import requests
3
+ # from TTS.api import TTS
4
+ # import os
5
+
6
+ # # βœ… Load TTS model (Make sure model is downloaded or install TTS first)
7
+ # tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
8
+
9
+ # # βœ… Get Ayah from AlQuran API
10
+ # def get_quran_ayat(surah_num, ayat_num):
11
+ # try:
12
+ # url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
13
+ # response = requests.get(url)
14
+ # response.raise_for_status()
15
+ # data = response.json()
16
+
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 "❌ Ayah not found.", "❌ Translation not found."
23
+
24
+ # except Exception as e:
25
+ # return f"❌ API Error: {e}", ""
26
+
27
+ # # βœ… Gradio main function
28
+ # def qari_bot(surah, ayah):
29
+ # arabic_text, translation = get_quran_ayat(surah, ayah)
30
+
31
+ # # Return early if error
32
+ # if "❌" in arabic_text:
33
+ # return arabic_text, translation, None
34
+
35
+ # # Generate audio and save to file
36
+ # output_path = "ayat.wav"
37
+ # try:
38
+ # tts.tts_to_file(
39
+ # text=arabic_text,
40
+ # file_path=output_path,
41
+ # language="ar"
42
+ # )
43
+ # return arabic_text, translation, output_path
44
+ # except Exception as e:
45
+ # return arabic_text, translation, f"❌ TTS Error: {e}"
46
+
47
+ # # βœ… Gradio Interface
48
+ # interface = gr.Interface(
49
+ # fn=qari_bot,
50
+ # inputs=[
51
+ # gr.Number(label="Surah Number", value=1),
52
+ # gr.Number(label="Ayah Number", value=1)
53
+ # ],
54
+ # outputs=[
55
+ # gr.Textbox(label="πŸ“œ Arabic Ayah"),
56
+ # gr.Textbox(label="🌐 Translation (English)"),
57
+ # gr.Audio(label="πŸ”Š Listen to Ayah")
58
+ # ],
59
+ # title="πŸ“– AI Qari Bot",
60
+ # description="Enter Surah & Ayah number to hear the Quran Ayah with AI voice and English translation. Example: Surah 1, Ayah 1 = Al-Fatiha"
61
+ # )
62
+
63
+ # interface.launch()
64
+
65
  import gradio as gr
66
  import requests
67
  from TTS.api import TTS
 
68
 
69
+ # πŸ” Map Surah names to numbers
70
+ surah_map = {
71
+ "Al-Fatiha": 1,
72
+ "Al-Baqarah": 2,
73
+ "Aal-i-Imran": 3,
74
+ "An-Nisa": 4,
75
+ "Al-Ma'idah": 5,
76
+ "Al-An'am": 6,
77
+ "Al-A'raf": 7,
78
+ "Al-Anfal": 8,
79
+ "At-Tawbah": 9,
80
+ "Yunus": 10,
81
+ # βž• Add more if needed...
82
+ }
83
+
84
+ # βœ… Load multilingual TTS model
85
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
86
 
87
+ # πŸ“– Fetch full Surah (Arabic + English)
88
+ def fetch_surah(surah_name):
89
+ surah_num = surah_map.get(surah_name)
90
+ if not surah_num:
91
+ return "Invalid Surah name.", "", None
92
+
93
  try:
94
+ url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
95
  response = requests.get(url)
96
  response.raise_for_status()
97
  data = response.json()
98
 
99
+ if 'data' in data and len(data['data']) == 2:
100
+ arabic_ayahs = data['data'][0]['ayahs']
101
+ english_ayahs = data['data'][1]['ayahs']
 
 
 
102
 
103
+ arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
104
+ english_text = "\n".join([e['text'] for e in english_ayahs])
105
 
106
+ # Generate audio
107
+ audio_path = "surah.wav"
108
+ tts.tts_to_file(text=arabic_text, file_path=audio_path, language="ar")
109
+ return arabic_text, english_text, audio_path
110
+ else:
111
+ return "❌ Surah not found.", "", None
 
112
 
 
 
 
 
 
 
 
 
 
113
  except Exception as e:
114
+ return f"❌ API Error: {e}", "", None
115
 
116
+ # πŸŽ›οΈ Gradio UI
117
  interface = gr.Interface(
118
+ fn=fetch_surah,
119
+ inputs=gr.Dropdown(choices=list(surah_map.keys()), label="Select Surah"),
 
 
 
120
  outputs=[
121
+ gr.Textbox(label="πŸ“œ Arabic Ayahs"),
122
+ gr.Textbox(label="🌐 English Translation"),
123
+ gr.Audio(label="πŸ”Š Arabic Voice Recitation")
124
  ],
125
  title="πŸ“– AI Qari Bot",
126
+ description="Select a Surah name to listen to Arabic AI recitation with English translation."
127
  )
128
 
129
+ interface.launch()