asad231 commited on
Commit
d6eca0a
Β·
verified Β·
1 Parent(s): b401b14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -836
app.py CHANGED
@@ -1,839 +1,3 @@
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()
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,
140
- # "Aal-i-Imran": 3,
141
- # "An-Nisa": 4,
142
- # "Al-Ma'idah": 5,
143
- # "Al-An'am": 6,
144
- # "Al-A'raf": 7,
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"
164
- # response = requests.get(url)
165
- # response.raise_for_status()
166
- # data = response.json()
167
-
168
- # if 'data' in data and len(data['data']) == 2:
169
- # arabic_ayahs = data['data'][0]['ayahs']
170
- # english_ayahs = data['data'][1]['ayahs']
171
-
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
187
-
188
- # except Exception as e:
189
- # return f"❌ API Error: {e}", "", None
190
-
191
- # # πŸŽ›οΈ Gradio UI
192
- # interface = gr.Interface(
193
- # fn=fetch_surah,
194
- # inputs=gr.Dropdown(choices=list(surah_map.keys()), label="Select Surah"),
195
- # outputs=[
196
- # gr.Textbox(label="πŸ“œ Arabic Ayahs"),
197
- # gr.Textbox(label="🌐 English Translation"),
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()
205
-
206
- # import gradio as gr
207
- # import requests
208
- # from TTS.api import TTS
209
-
210
- # # πŸ“š Surah names and numbers (partial shown, full in full code)
211
- # surah_info = {
212
- # "Al-Fatiha": (1, 7),
213
- # "Al-Baqarah": (2, 286),
214
- # "Aal-i-Imran": (3, 200),
215
- # "An-Nisa": (4, 176),
216
- # "Al-Ma'idah": (5, 120),
217
- # "Al-An'am": (6, 165),
218
- # "Al-A'raf": (7, 206),
219
- # "Al-Anfal": (8, 75),
220
- # "At-Tawbah": (9, 129),
221
- # "Yunus": (10, 109),
222
- # # βž• Add all 114 surahs with total ayats here
223
- # }
224
-
225
- # # βœ… Load TTS model
226
- # tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
227
- # speaker = "random" # or "en_0", "ar_0"
228
-
229
- # # 🎧 Main Function
230
- # def ai_quran(surah_name, ayat_num, mode):
231
- # surah_num, total_ayats = surah_info.get(surah_name, (None, None))
232
- # if not surah_num:
233
- # return "Invalid Surah", "", None
234
-
235
- # if mode == "Full Surah":
236
- # url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
237
- # else: # Single Ayah
238
- # if ayat_num < 1 or ayat_num > total_ayats:
239
- # return "❌ Invalid Ayat number", "", None
240
- # url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
241
-
242
- # try:
243
- # res = requests.get(url)
244
- # res.raise_for_status()
245
- # data = res.json()
246
-
247
- # if mode == "Full Surah":
248
- # arabic_ayahs = data['data'][0]['ayahs']
249
- # english_ayahs = data['data'][1]['ayahs']
250
- # arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
251
- # english_text = "\n".join([e['text'] for e in english_ayahs])
252
- # else:
253
- # arabic_text = data['data'][0]['text']
254
- # english_text = data['data'][1]['text']
255
-
256
- # # πŸ”Š Generate TTS
257
- # audio_path = "ayat.wav"
258
- # tts.tts_to_file(
259
- # text=arabic_text,
260
- # speaker=speaker,
261
- # language="ar",
262
- # file_path=audio_path
263
- # )
264
-
265
- # return arabic_text, english_text, audio_path
266
-
267
- # except Exception as e:
268
- # return f"API Error: {e}", "", None
269
-
270
- # # 🧠 Mode Selector
271
- # def update_ayat_field(mode, surah):
272
- # if mode == "Full Surah":
273
- # return gr.update(visible=False)
274
- # return gr.update(visible=True, maximum=surah_info[surah][1])
275
-
276
- # # πŸŽ›οΈ Gradio Interface
277
- # with gr.Blocks(title="πŸ“– AI Quran Reciter") as demo:
278
- # gr.Markdown("### πŸ“– AI Quran Reciter with Voice | XTTSv2 Arabic TTS")
279
-
280
- # with gr.Row():
281
- # mode = gr.Radio(["Full Surah", "Single Ayat"], value="Full Surah", label="Mode")
282
- # surah = gr.Dropdown(choices=list(surah_info.keys()), label="Select Surah", value="Al-Fatiha")
283
- # ayat = gr.Number(label="Ayat Number", visible=False)
284
-
285
- # run = gr.Button("πŸ“₯ Recite")
286
- # arabic = gr.Textbox(label="πŸ“œ Arabic Text")
287
- # english = gr.Textbox(label="🌐 English Translation")
288
- # audio = gr.Audio(label="πŸ”Š Listen")
289
-
290
- # # Event handlers
291
- # mode.change(update_ayat_field, inputs=[mode, surah], outputs=ayat)
292
- # surah.change(update_ayat_field, inputs=[mode, surah], outputs=ayat)
293
- # run.click(fn=ai_quran, inputs=[surah, ayat, mode], outputs=[arabic, english, audio])
294
-
295
- # demo.launch()
296
-
297
- # import os
298
- # import gradio as gr
299
- # import requests
300
- # from TTS.api import TTS
301
-
302
- # # βœ… Accept Coqui TTS license
303
- # os.environ["COQUI_TOS_AGREED"] = "1"
304
-
305
- # # πŸ” Surah info: name -> (number, total ayats)
306
- # surah_info = {
307
- # "Al-Fatiha": (1, 7),
308
- # "Al-Baqarah": (2, 286),
309
- # "Aal-i-Imran": (3, 200),
310
- # "An-Nisa": (4, 176),
311
- # "Al-Ma'idah": (5, 120),
312
- # "Al-An'am": (6, 165),
313
- # "Al-A'raf": (7, 206),
314
- # "Al-Anfal": (8, 75),
315
- # "At-Tawbah": (9, 129),
316
- # "Yunus": (10, 109),
317
- # "Hud": (11, 123),
318
- # "Yusuf": (12, 111),
319
- # "Ar-Ra'd": (13, 43),
320
- # "Ibrahim": (14, 52),
321
- # "Al-Hijr": (15, 99),
322
- # "An-Nahl": (16, 128),
323
- # "Al-Isra": (17, 111),
324
- # "Al-Kahf": (18, 110),
325
- # "Maryam": (19, 98),
326
- # "Taha": (20, 135),
327
- # # πŸ‘‰ Add remaining up to Surah 114
328
- # "An-Nas": (114, 6),
329
- # }
330
-
331
- # # βœ… Load TTS with Arabic + speaker support
332
- # tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
333
- # speaker = "random" # or use "en_0", "ar_0" etc.
334
-
335
- # # πŸ“– Main function
336
- # def ai_quran(surah_name, ayat_num, mode):
337
- # surah_num, total_ayats = surah_info.get(surah_name, (None, None))
338
- # if not surah_num:
339
- # return "Invalid Surah", "", None
340
-
341
- # if mode == "Full Surah":
342
- # url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
343
- # else: # Single Ayah
344
- # if not ayat_num or ayat_num < 1 or ayat_num > total_ayats:
345
- # return f"❌ Ayat number must be between 1 and {total_ayats}", "", None
346
- # url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
347
-
348
- # try:
349
- # res = requests.get(url)
350
- # res.raise_for_status()
351
- # data = res.json()
352
-
353
- # if mode == "Full Surah":
354
- # arabic_ayahs = data['data'][0]['ayahs']
355
- # english_ayahs = data['data'][1]['ayahs']
356
- # arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
357
- # english_text = "\n".join([e['text'] for e in english_ayahs])
358
- # else:
359
- # arabic_text = data['data'][0]['text']
360
- # english_text = data['data'][1]['text']
361
-
362
- # # πŸ”Š Generate TTS
363
- # audio_path = "ayat.wav"
364
- # tts.tts_to_file(
365
- # text=arabic_text,
366
- # speaker=speaker,
367
- # language="ar",
368
- # file_path=audio_path
369
- # )
370
-
371
- # return arabic_text, english_text, audio_path
372
-
373
- # except Exception as e:
374
- # return f"API Error: {e}", "", None
375
-
376
- # # 🧠 Ayat field visibility toggle
377
- # def toggle_ayat(mode, surah):
378
- # if mode == "Full Surah":
379
- # return gr.update(visible=False)
380
- # return gr.update(visible=True, maximum=surah_info[surah][1])
381
-
382
- # # πŸŽ›οΈ Gradio UI
383
- # with gr.Blocks(title="πŸ“– AI Quran Reciter") as demo:
384
- # gr.Markdown("## πŸ“– AI Quran Reciter with Arabic Voice | XTTSv2 Model")
385
-
386
- # with gr.Row():
387
- # mode = gr.Radio(["Full Surah", "Single Ayat"], label="Mode", value="Full Surah")
388
- # surah = gr.Dropdown(choices=list(surah_info.keys()), label="Surah", value="Al-Fatiha")
389
- # ayat = gr.Number(label="Ayat Number", visible=False)
390
-
391
- # recite_btn = gr.Button("πŸ“₯ Recite")
392
- # arabic = gr.Textbox(label="πŸ“œ Arabic Text")
393
- # english = gr.Textbox(label="🌐 English Translation")
394
- # audio = gr.Audio(label="πŸ”Š Listen")
395
-
396
- # # Event hooks
397
- # mode.change(toggle_ayat, inputs=[mode, surah], outputs=ayat)
398
- # surah.change(toggle_ayat, inputs=[mode, surah], outputs=ayat)
399
- # recite_btn.click(ai_quran, inputs=[surah, ayat, mode], outputs=[arabic, english, audio])
400
-
401
- # demo.launch()
402
-
403
- # import os
404
- # import gradio as gr
405
- # import requests
406
- # from TTS.api import TTS
407
-
408
- # # βœ… Accept Coqui license for Hugging Face Space
409
- # os.environ["COQUI_TOS_AGREED"] = "1"
410
-
411
- # # βœ… Fix PyTorch 2.6 checkpoint loading issue
412
- # import torch
413
- # from torch.serialization import add_safe_globals
414
- # from TTS.tts.configs.xtts_config import XttsConfig
415
- # add_safe_globals([XttsConfig])
416
-
417
- # # βœ… Load multilingual XTTSv2 model (speaker supported)
418
- # tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
419
- # speaker_wav = None # Optional: path to your own .wav voice file
420
-
421
- # # Surah list with Ayah counts (can be extended)
422
- # surah_info = {
423
- # "Al-Fatiha": (1, 7),
424
- # "Al-Baqarah": (2, 286),
425
- # "Aal-i-Imran": (3, 200),
426
- # "An-Nisa": (4, 176),
427
- # "Al-Ma'idah": (5, 120),
428
- # "Al-An'am": (6, 165),
429
- # "Al-A'raf": (7, 206),
430
- # "Al-Anfal": (8, 75),
431
- # "At-Tawbah": (9, 129),
432
- # "Yunus": (10, 109),
433
- # "Hud": (11, 123),
434
- # "Yusuf": (12, 111),
435
- # "Ar-Ra'd": (13, 43),
436
- # "Ibrahim": (14, 52),
437
- # "Al-Hijr": (15, 99),
438
- # "An-Nahl": (16, 128),
439
- # "Al-Isra": (17, 111),
440
- # "Al-Kahf": (18, 110),
441
- # "Maryam": (19, 98),
442
- # "Taha": (20, 135),
443
- # "Al-Anbiya": (21, 112),
444
- # "Al-Hajj": (22, 78),
445
- # "Al-Mu'minun": (23, 118),
446
- # "An-Nur": (24, 64),
447
- # "Al-Furqan": (25, 77),
448
- # "Ash-Shu'ara": (26, 227),
449
- # "An-Naml": (27, 93),
450
- # "Al-Qasas": (28, 88),
451
- # "Al-Ankabut": (29, 69),
452
- # "Ar-Rum": (30, 60),
453
- # "Luqman": (31, 34),
454
- # "As-Sajda": (32, 30),
455
- # "Al-Ahzab": (33, 73),
456
- # "Saba": (34, 54),
457
- # "Fatir": (35, 45),
458
- # "Ya-Sin": (36, 83),
459
- # "As-Saffat": (37, 182),
460
- # "Sad": (38, 88),
461
- # "Az-Zumar": (39, 75),
462
- # "Ghafir": (40, 85),
463
- # "Fussilat": (41, 54),
464
- # "Ash-Shura": (42, 53),
465
- # "Az-Zukhruf": (43, 89),
466
- # "Ad-Dukhan": (44, 59),
467
- # "Al-Jathiya": (45, 37),
468
- # "Al-Ahqaf": (46, 35),
469
- # "Muhammad": (47, 38),
470
- # "Al-Fath": (48, 29),
471
- # "Al-Hujurat": (49, 18),
472
- # "Qaf": (50, 45),
473
- # "Adh-Dhariyat": (51, 60),
474
- # "At-Tur": (52, 49),
475
- # "An-Najm": (53, 62),
476
- # "Al-Qamar": (54, 55),
477
- # "Ar-Rahman": (55, 78),
478
- # "Al-Waqia": (56, 96),
479
- # "Al-Hadid": (57, 29),
480
- # "Al-Mujadila": (58, 22),
481
- # "Al-Hashr": (59, 24),
482
- # "Al-Mumtahina": (60, 13),
483
- # "As-Saff": (61, 14),
484
- # "Al-Jumu'a": (62, 11),
485
- # "Al-Munafiqun": (63, 11),
486
- # "At-Taghabun": (64, 18),
487
- # "At-Talaq": (65, 12),
488
- # "At-Tahrim": (66, 12),
489
- # "Al-Mulk": (67, 30),
490
- # "Al-Qalam": (68, 52),
491
- # "Al-Haqqa": (69, 52),
492
- # "Al-Ma'arij": (70, 44),
493
- # "Nuh": (71, 28),
494
- # "Al-Jinn": (72, 28),
495
- # "Al-Muzzammil": (73, 20),
496
- # "Al-Muddaththir": (74, 56),
497
- # "Al-Qiyamah": (75, 40),
498
- # "Al-Insan": (76, 31),
499
- # "Al-Mursalat": (77, 50),
500
- # "An-Naba": (78, 40),
501
- # "An-Nazi'at": (79, 46),
502
- # "Abasa": (80, 42),
503
- # "At-Takwir": (81, 29),
504
- # "Al-Infitar": (82, 19),
505
- # "Al-Mutaffifin": (83, 36),
506
- # "Al-Inshiqaq": (84, 25),
507
- # "Al-Buruj": (85, 22),
508
- # "At-Tariq": (86, 17),
509
- # "Al-A'la": (87, 19),
510
- # "Al-Ghashiyah": (88, 26),
511
- # "Al-Fajr": (89, 30),
512
- # "Al-Balad": (90, 20),
513
- # "Ash-Shams": (91, 15),
514
- # "Al-Layl": (92, 21),
515
- # "Ad-Duha": (93, 11),
516
- # "Ash-Sharh": (94, 8),
517
- # "At-Tin": (95, 8),
518
- # "Al-Alaq": (96, 19),
519
- # "Al-Qadr": (97, 5),
520
- # "Al-Bayyina": (98, 8),
521
- # "Az-Zalzalah": (99, 8),
522
- # "Al-Adiyat": (100, 11),
523
- # "Al-Qari'a": (101, 11),
524
- # "At-Takathur": (102, 8),
525
- # "Al-Asr": (103, 3),
526
- # "Al-Humazah": (104, 9),
527
- # "Al-Fil": (105, 5),
528
- # "Quraysh": (106, 4),
529
- # "Al-Ma'un": (107, 7),
530
- # "Al-Kawthar": (108, 3),
531
- # "Al-Kafirun": (109, 6),
532
- # "An-Nasr": (110, 3),
533
- # "Al-Masad": (111, 5),
534
- # "Al-Ikhlas": (112, 4),
535
- # "Al-Falaq": (113, 5),
536
- # "An-Nas": (114, 6)
537
- # }
538
-
539
-
540
- # def recite_quran(surah_name, ayah_number, mode):
541
- # surah_num, total_ayahs = surah_info[surah_name]
542
- # if mode == "Full Surah":
543
- # url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
544
- # else:
545
- # if not (1 <= ayah_number <= total_ayahs):
546
- # return f"Ayat must be between 1 and {total_ayahs}", "", None
547
- # url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_number}/editions/quran-simple,en.asad"
548
-
549
- # try:
550
- # res = requests.get(url)
551
- # res.raise_for_status()
552
- # data = res.json()
553
-
554
- # if mode == "Full Surah":
555
- # arabic = "\n".join([a['text'] for a in data['data'][0]['ayahs']])
556
- # english = "\n".join([a['text'] for a in data['data'][1]['ayahs']])
557
- # else:
558
- # arabic = data['data'][0]['text']
559
- # english = data['data'][1]['text']
560
-
561
- # audio_path = "output.wav"
562
- # tts.tts_to_file(
563
- # text=arabic,
564
- # speaker_wav=speaker_wav,
565
- # language="ar",
566
- # file_path=audio_path
567
- # )
568
-
569
- # return arabic, english, audio_path
570
-
571
- # except Exception as e:
572
- # return f"❌ Error: {e}", "", None
573
-
574
- # def toggle_ayah_visibility(mode, surah):
575
- # if mode == "Full Surah":
576
- # return gr.update(visible=False)
577
- # return gr.update(visible=True, maximum=surah_info[surah][1])
578
-
579
- # # Gradio Interface
580
- # with gr.Blocks(title="πŸ“– Quran AI Reader (with Speaker)") as demo:
581
- # gr.Markdown("## πŸ•Œ AI Quran Reciter using XTTSv2 (Arabic Voice with Speaker)")
582
-
583
- # with gr.Row():
584
- # mode = gr.Radio(["Full Surah", "Single Ayat"], label="Mode", value="Full Surah")
585
- # surah = gr.Dropdown(choices=list(surah_info.keys()), label="Surah", value="Al-Fatiha")
586
- # ayah = gr.Number(label="Ayat Number", visible=False)
587
-
588
- # recite_btn = gr.Button("🎧 Recite")
589
- # arabic_box = gr.Textbox(label="πŸ“œ Arabic Text")
590
- # english_box = gr.Textbox(label="🌐 English Translation")
591
- # audio_output = gr.Audio(label="πŸ”Š Listen")
592
-
593
- # mode.change(toggle_ayah_visibility, inputs=[mode, surah], outputs=ayah)
594
- # surah.change(toggle_ayah_visibility, inputs=[mode, surah], outputs=ayah)
595
- # recite_btn.click(recite_quran, inputs=[surah, ayah, mode], outputs=[arabic_box, english_box, audio_output])
596
-
597
- # demo.launch()
598
-
599
- # app.py
600
- # app.py
601
- # import gradio as gr
602
- # import requests
603
- # import torch
604
- # from TTS.api import TTS # βœ… This is the correct class
605
- # import TTS.tts.configs.xtts_config
606
- # import TTS.tts.models.xtts
607
-
608
- # # βœ… Required for PyTorch 2.6+ for XTTS models
609
- # torch.serialization.add_safe_globals([
610
- # TTS.tts.configs.xtts_config.XttsConfig,
611
- # TTS.tts.models.xtts.XttsAudioConfig
612
- # ])
613
-
614
- # # βœ… Load XTTS v2 Model (Arabic + Speaker Cloning Support)
615
- # tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
616
-
617
- # # βœ… Fetch Full Surah in Arabic & English
618
- # def get_surah_text(surah_name):
619
- # try:
620
- # url = f"https://api.alquran.cloud/v1/surah/{surah_name}/editions/quran-simple,en.asad"
621
- # response = requests.get(url)
622
- # response.raise_for_status()
623
- # data = response.json()
624
-
625
- # if 'data' in data and len(data['data']) == 2:
626
- # arabic = "\n".join([a['text'] for a in data['data'][0]['ayahs']])
627
- # translation = "\n".join([t['text'] for t in data['data'][1]['ayahs']])
628
- # return arabic, translation
629
- # return "Surah not found.", "Translation not found."
630
- # except Exception as e:
631
- # return f"API Error: {e}", ""
632
-
633
- # # βœ… Gradio Inference Function
634
- # def qari_bot(surah_name, speaker_audio):
635
- # arabic_text, translation = get_surah_text(surah_name)
636
- # if "Error" in arabic_text or "not found" in arabic_text:
637
- # return arabic_text, translation, None
638
-
639
- # # βœ… Generate TTS with optional speaker audio
640
- # tts.tts_to_file(
641
- # text=arabic_text,
642
- # speaker_wav=speaker_audio if speaker_audio else None,
643
- # language="ar",
644
- # file_path="output.wav"
645
- # )
646
- # return arabic_text, translation, "output.wav"
647
-
648
- # # βœ… Gradio Interface
649
- # interface = gr.Interface(
650
- # fn=qari_bot,
651
- # inputs=[
652
- # gr.Textbox(label="Surah Name (e.g., Al-Fatiha, An-Nas)", placeholder="Enter Surah name"),
653
- # gr.Audio(label="Upload Speaker Audio (Optional)", type="filepath")
654
- # ],
655
- # outputs=[
656
- # gr.Textbox(label="πŸ“œ Arabic Text"),
657
- # gr.Textbox(label="🌐 English Translation"),
658
- # gr.Audio(label="πŸ”Š Quran Recitation")
659
- # ],
660
- # title="πŸ•Œ AI Qari Bot",
661
- # description="Enter a Surah name to hear full Arabic recitation with optional speaker voice."
662
- # )
663
-
664
- # interface.launch()
665
-
666
-
667
- # import gradio as gr
668
- # import requests
669
-
670
- # # βœ… Fetch Surah data
671
- # def get_surah(surah_name):
672
- # try:
673
- # url = f"https://api.alquran.cloud/v1/surah/{surah_name}/editions/quran-simple,en.asad"
674
- # response = requests.get(url)
675
- # response.raise_for_status()
676
- # data = response.json()
677
-
678
- # if 'data' in data and len(data['data']) == 2:
679
- # arabic = "\n\n".join(
680
- # [f"{i+1}. {a['text']}" for i, a in enumerate(data['data'][0]['ayahs'])]
681
- # )
682
- # translation = "\n\n".join(
683
- # [f"{i+1}. {t['text']}" for i, t in enumerate(data['data'][1]['ayahs'])]
684
- # )
685
- # return arabic, translation
686
- # return "Surah not found.", "Translation not found."
687
- # except Exception as e:
688
- # return f"API Error: {e}", ""
689
-
690
- # # βœ… Gradio UI
691
- # gr.Interface(
692
- # fn=get_surah,
693
- # inputs=gr.Textbox(label="Enter Surah Name (e.g., Al-Fatiha, An-Nas)", placeholder="Al-Fatiha"),
694
- # outputs=[
695
- # gr.Textbox(label="πŸ“œ Arabic Text", lines=15),
696
- # gr.Textbox(label="🌐 English Translation", lines=15)
697
- # ],
698
- # title="πŸ“– Surah Viewer",
699
- # description="View full Surah from Quran with Arabic and English translation (No Audio)"
700
- # ).launch()
701
-
702
- # import gradio as gr
703
- # import requests
704
-
705
- # # Get surah text by number
706
- # def get_surah(surah_number):
707
- # try:
708
- # url = f"https://api.alquran.cloud/v1/surah/{surah_number}/editions/quran-simple,en.asad"
709
- # response = requests.get(url)
710
- # response.raise_for_status()
711
- # data = response.json()
712
-
713
- # if 'data' in data and len(data['data']) == 2:
714
- # arabic = "\n\n".join(
715
- # [f"{i+1}. {a['text']}" for i, a in enumerate(data['data'][0]['ayahs'])]
716
- # )
717
- # translation = "\n\n".join(
718
- # [f"{i+1}. {t['text']}" for i, t in enumerate(data['data'][1]['ayahs'])]
719
- # )
720
- # return arabic, translation
721
- # return "❌ Surah not found.", "❌ Translation not found."
722
- # except Exception as e:
723
- # return f"❌ API Error: {e}", ""
724
-
725
- # # Surah List (1-114)
726
- # surah_names = [
727
- # "1 - Al-Fatiha", "2 - Al-Baqarah", "3 - Aal-E-Imran", "4 - An-Nisa", "5 - Al-Ma'idah",
728
- # "6 - Al-An'am", "7 - Al-A'raf", "8 - Al-Anfal", "9 - At-Tawbah", "10 - Yunus",
729
- # "11 - Hud", "12 - Yusuf", "13 - Ar-Ra'd", "14 - Ibrahim", "15 - Al-Hijr",
730
- # "16 - An-Nahl", "17 - Al-Isra", "18 - Al-Kahf", "19 - Maryam", "20 - Ta-Ha",
731
- # "21 - Al-Anbiya", "22 - Al-Hajj", "23 - Al-Mu'minun", "24 - An-Nur", "25 - Al-Furqan",
732
- # "26 - Ash-Shu'ara", "27 - An-Naml", "28 - Al-Qasas", "29 - Al-Ankabut", "30 - Ar-Rum",
733
- # "31 - Luqman", "32 - As-Sajda", "33 - Al-Ahzab", "34 - Saba", "35 - Fatir",
734
- # "36 - Ya-Sin", "37 - As-Saffat", "38 - Sad", "39 - Az-Zumar", "40 - Ghafir",
735
- # "41 - Fussilat", "42 - Ash-Shura", "43 - Az-Zukhruf", "44 - Ad-Dukhan", "45 - Al-Jathiya",
736
- # "46 - Al-Ahqaf", "47 - Muhammad", "48 - Al-Fath", "49 - Al-Hujurat", "50 - Qaf",
737
- # "51 - Adh-Dhariyat", "52 - At-Tur", "53 - An-Najm", "54 - Al-Qamar", "55 - Ar-Rahman",
738
- # "56 - Al-Waqi'a", "57 - Al-Hadid", "58 - Al-Mujadila", "59 - Al-Hashr", "60 - Al-Mumtahina",
739
- # "61 - As-Saff", "62 - Al-Jumua", "63 - Al-Munafiqun", "64 - At-Taghabun", "65 - At-Talaq",
740
- # "66 - At-Tahrim", "67 - Al-Mulk", "68 - Al-Qalam", "69 - Al-Haqqah", "70 - Al-Ma'arij",
741
- # "71 - Nuh", "72 - Al-Jinn", "73 - Al-Muzzammil", "74 - Al-Muddaththir", "75 - Al-Qiyama",
742
- # "76 - Al-Insan", "77 - Al-Mursalat", "78 - An-Naba", "79 - An-Nazi'at", "80 - Abasa",
743
- # "81 - At-Takwir", "82 - Al-Infitar", "83 - Al-Mutaffifin", "84 - Al-Inshiqaq", "85 - Al-Buruj",
744
- # "86 - At-Tariq", "87 - Al-Ala", "88 - Al-Ghashiyah", "89 - Al-Fajr", "90 - Al-Balad",
745
- # "91 - Ash-Shams", "92 - Al-Lail", "93 - Ad-Duhaa", "94 - Ash-Sharh", "95 - At-Tin",
746
- # "96 - Al-Alaq", "97 - Al-Qadr", "98 - Al-Bayyina", "99 - Az-Zalzala", "100 - Al-Adiyat",
747
- # "101 - Al-Qari'a", "102 - At-Takathur", "103 - Al-Asr", "104 - Al-Humaza", "105 - Al-Fil",
748
- # "106 - Quraish", "107 - Al-Ma'un", "108 - Al-Kawthar", "109 - Al-Kafiroon", "110 - An-Nasr",
749
- # "111 - Al-Masad", "112 - Al-Ikhlas", "113 - Al-Falaq", "114 - An-Nas"
750
- # ]
751
-
752
- # # Extract surah number
753
- # def extract_number(surah_name):
754
- # return int(surah_name.split(" - ")[0])
755
-
756
- # # Gradio App
757
- # gr.Interface(
758
- # fn=lambda selected: get_surah(extract_number(selected)),
759
- # inputs=gr.Dropdown(choices=surah_names, label="πŸ“– Select a Surah"),
760
- # outputs=[
761
- # gr.Textbox(label="πŸ“œ Arabic Text", lines=15),
762
- # gr.Textbox(label="🌐 English Translation", lines=15)
763
- # ],
764
- # title="πŸ“– Quran Surah Viewer (Arabic + English)",
765
- # description="Select a Surah to see its Arabic text and English translation."
766
- # ).launch()
767
-
768
- # import gradio as gr
769
- # import requests
770
-
771
- # # βœ… Get Arabic, English, and Urdu text
772
- # def get_surah(surah_number):
773
- # try:
774
- # url = f"https://api.alquran.cloud/v1/surah/{surah_number}/editions/quran-simple,en.asad,ur.jalandhry"
775
- # response = requests.get(url)
776
- # response.raise_for_status()
777
- # data = response.json()
778
-
779
- # if 'data' in data and len(data['data']) == 3:
780
- # arabic = "\n\n".join(
781
- # [f"{i+1}. {a['text']}" for i, a in enumerate(data['data'][0]['ayahs'])]
782
- # )
783
- # english = "\n\n".join(
784
- # [f"{i+1}. {t['text']}" for i, t in enumerate(data['data'][1]['ayahs'])]
785
- # )
786
- # urdu = "\n\n".join(
787
- # [f"{i+1}. {u['text']}" for i, u in enumerate(data['data'][2]['ayahs'])]
788
- # )
789
- # return arabic, english, urdu
790
- # return "❌ Surah not found.", "❌ English not found.", "❌ Urdu not found."
791
- # except Exception as e:
792
- # return f"❌ API Error: {e}", "", ""
793
-
794
- # # βœ… Surah List
795
- # surah_names = [
796
- # "1 - Al-Fatiha", "2 - Al-Baqarah", "3 - Aal-E-Imran", "4 - An-Nisa", "5 - Al-Ma'idah",
797
- # "6 - Al-An'am", "7 - Al-A'raf", "8 - Al-Anfal", "9 - At-Tawbah", "10 - Yunus",
798
- # "11 - Hud", "12 - Yusuf", "13 - Ar-Ra'd", "14 - Ibrahim", "15 - Al-Hijr",
799
- # "16 - An-Nahl", "17 - Al-Isra", "18 - Al-Kahf", "19 - Maryam", "20 - Ta-Ha",
800
- # "21 - Al-Anbiya", "22 - Al-Hajj", "23 - Al-Mu'minun", "24 - An-Nur", "25 - Al-Furqan",
801
- # "26 - Ash-Shu'ara", "27 - An-Naml", "28 - Al-Qasas", "29 - Al-Ankabut", "30 - Ar-Rum",
802
- # "31 - Luqman", "32 - As-Sajda", "33 - Al-Ahzab", "34 - Saba", "35 - Fatir",
803
- # "36 - Ya-Sin", "37 - As-Saffat", "38 - Sad", "39 - Az-Zumar", "40 - Ghafir",
804
- # "41 - Fussilat", "42 - Ash-Shura", "43 - Az-Zukhruf", "44 - Ad-Dukhan", "45 - Al-Jathiya",
805
- # "46 - Al-Ahqaf", "47 - Muhammad", "48 - Al-Fath", "49 - Al-Hujurat", "50 - Qaf",
806
- # "51 - Adh-Dhariyat", "52 - At-Tur", "53 - An-Najm", "54 - Al-Qamar", "55 - Ar-Rahman",
807
- # "56 - Al-Waqi'a", "57 - Al-Hadid", "58 - Al-Mujadila", "59 - Al-Hashr", "60 - Al-Mumtahina",
808
- # "61 - As-Saff", "62 - Al-Jumua", "63 - Al-Munafiqun", "64 - At-Taghabun", "65 - At-Talaq",
809
- # "66 - At-Tahrim", "67 - Al-Mulk", "68 - Al-Qalam", "69 - Al-Haqqah", "70 - Al-Ma'arij",
810
- # "71 - Nuh", "72 - Al-Jinn", "73 - Al-Muzzammil", "74 - Al-Muddaththir", "75 - Al-Qiyama",
811
- # "76 - Al-Insan", "77 - Al-Mursalat", "78 - An-Naba", "79 - An-Nazi'at", "80 - Abasa",
812
- # "81 - At-Takwir", "82 - Al-Infitar", "83 - Al-Mutaffifin", "84 - Al-Inshiqaq", "85 - Al-Buruj",
813
- # "86 - At-Tariq", "87 - Al-Ala", "88 - Al-Ghashiyah", "89 - Al-Fajr", "90 - Al-Balad",
814
- # "91 - Ash-Shams", "92 - Al-Lail", "93 - Ad-Duhaa", "94 - Ash-Sharh", "95 - At-Tin",
815
- # "96 - Al-Alaq", "97 - Al-Qadr", "98 - Al-Bayyina", "99 - Az-Zalzala", "100 - Al-Adiyat",
816
- # "101 - Al-Qari'a", "102 - At-Takathur", "103 - Al-Asr", "104 - Al-Humaza", "105 - Al-Fil",
817
- # "106 - Quraish", "107 - Al-Ma'un", "108 - Al-Kawthar", "109 - Al-Kafiroon", "110 - An-Nasr",
818
- # "111 - Al-Masad", "112 - Al-Ikhlas", "113 - Al-Falaq", "114 - An-Nas"
819
- # ]
820
-
821
- # # βœ… Extract number from dropdown
822
- # def extract_number(surah_name):
823
- # return int(surah_name.split(" - ")[0])
824
-
825
- # # βœ… Gradio App (3 outputs now: Arabic, English, Urdu)
826
- # gr.Interface(
827
- # fn=lambda selected: get_surah(extract_number(selected)),
828
- # inputs=gr.Dropdown(choices=surah_names, label="πŸ“– Select a Surah"),
829
- # outputs=[
830
- # gr.Textbox(label="πŸ“œ Arabic Text", lines=15),
831
- # gr.Textbox(label="🌐 English Translation", lines=15),
832
- # gr.Textbox(label="πŸŒ™ Urdu Translation", lines=15)
833
- # ],
834
- # title="πŸ“– Quran Surah Viewer (Arabic + English + Urdu)",
835
- # description="Select a Surah to view Arabic text, English translation, and Urdu translation."
836
- # ).launch()
837
 
838
  import gradio as gr
839
  import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  import gradio as gr
3
  import requests