asad231 commited on
Commit
7cb324a
Β·
verified Β·
1 Parent(s): aa8db63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +247 -181
app.py CHANGED
@@ -400,199 +400,265 @@
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
 
 
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
+ import gradio as gr
601
+ import requests
602
+ import torch
603
+ from TTS.api import TTS
604
+ import TTS.tts.configs.xtts_config
605
+ import TTS.tts.models.xtts
606
 
607
+ # βœ… Allow safe globals for PyTorch 2.6+
608
+ torch.serialization.add_safe_globals([
609
+ TTS.tts.configs.xtts_config.XttsConfig,
610
+ TTS.tts.models.xtts.XttsAudioConfig
611
+ ])
612
 
613
+ # βœ… Load XTTS v2 Model (supports speaker cloning and Arabic)
614
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
 
 
615
 
616
+ # βœ… Fetch full surah by name
617
+ def get_surah_text(surah_name):
618
+ try:
619
+ url = f"https://api.alquran.cloud/v1/surah/{surah_name}/editions/quran-simple,en.asad"
620
+ response = requests.get(url)
621
+ response.raise_for_status()
622
+ data = response.json()
623
 
624
+ if 'data' in data and len(data['data']) == 2:
625
+ arabic = "\n".join([a['text'] for a in data['data'][0]['ayahs']])
626
+ translation = "\n".join([t['text'] for t in data['data'][1]['ayahs']])
627
+ return arabic, translation
628
+ return "Surah not found.", "Translation not found."
629
+ except Exception as e:
630
+ return f"API Error: {e}", ""
631
+
632
+ # βœ… Gradio Function
633
+ def qari_bot(surah_name, speaker_audio):
634
+ arabic_text, translation = get_surah_text(surah_name)
635
+ if "Error" in arabic_text:
636
+ return arabic_text, translation, None
637
+
638
+ # βœ… Generate TTS with speaker
639
+ tts.tts_to_file(
640
+ text=arabic_text,
641
+ speaker_wav=speaker_audio if speaker_audio else None,
642
+ language="ar",
643
+ file_path="output.wav"
644
+ )
645
+ return arabic_text, translation, "output.wav"
646
+
647
+ # βœ… Gradio Interface
648
+ interface = gr.Interface(
649
+ fn=qari_bot,
650
+ inputs=[
651
+ gr.Textbox(label="Surah Name (e.g., Al-Fatiha, An-Nas)", placeholder="Enter Surah name"),
652
+ gr.Audio(label="Upload Speaker Audio (Optional)", type="filepath", optional=True)
653
+ ],
654
+ outputs=[
655
+ gr.Textbox(label="πŸ“œ Arabic Text"),
656
+ gr.Textbox(label="🌐 English Translation"),
657
+ gr.Audio(label="πŸ”Š Quran Recitation")
658
+ ],
659
+ title="πŸ•Œ AI Qari Bot",
660
+ description="Enter a Surah name to hear full Arabic recitation with optional speaker voice."
661
+ )
662
+
663
+ interface.launch()
664