asad231 commited on
Commit
2399b21
Β·
verified Β·
1 Parent(s): 0047024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -8
app.py CHANGED
@@ -62,11 +62,78 @@
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,
@@ -78,17 +145,19 @@ surah_map = {
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"
@@ -103,9 +172,15 @@ def fetch_surah(surah_name):
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
@@ -123,7 +198,7 @@ interface = gr.Interface(
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()
 
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()
130
+
131
+
132
  import gradio as gr
133
  import requests
134
  from TTS.api import TTS
135
 
136
+ # πŸ” Surah name mapping
137
  surah_map = {
138
  "Al-Fatiha": 1,
139
  "Al-Baqarah": 2,
 
145
  "Al-Anfal": 8,
146
  "At-Tawbah": 9,
147
  "Yunus": 10,
148
+ # Add more if needed
149
  }
150
 
151
+ # βœ… Load TTS model with speaker
152
  tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
153
+ available_speakers = tts.speakers
154
+ default_speaker = available_speakers[0] # Pick the first available speaker
155
 
156
+ # πŸ“– Fetch Surah
157
  def fetch_surah(surah_name):
158
  surah_num = surah_map.get(surah_name)
159
  if not surah_num:
160
+ return "❌ Invalid Surah name.", "", None
161
 
162
  try:
163
  url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
 
172
  arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
173
  english_text = "\n".join([e['text'] for e in english_ayahs])
174
 
175
+ # πŸ”Š Generate voice
176
  audio_path = "surah.wav"
177
+ tts.tts_to_file(
178
+ text=arabic_text,
179
+ speaker=default_speaker,
180
+ language="ar",
181
+ file_path=audio_path
182
+ )
183
+
184
  return arabic_text, english_text, audio_path
185
  else:
186
  return "❌ Surah not found.", "", None
 
198
  gr.Audio(label="πŸ”Š Arabic Voice Recitation")
199
  ],
200
  title="πŸ“– AI Qari Bot",
201
+ description=f"Select a Surah to hear AI recitation. Model speaker used: {default_speaker}"
202
  )
203
 
204
  interface.launch()