Spaces:
Sleeping
Sleeping
File size: 19,139 Bytes
62e9e47 2399b21 0975f8f be0cef3 aa8db63 be0cef3 cbb61d9 aa8db63 be0cef3 aa8db63 0975f8f be0cef3 aa8db63 62e9e47 aa8db63 0975f8f 62e9e47 aa8db63 0975f8f cbb61d9 0975f8f aa8db63 0975f8f aa8db63 cbb61d9 aa8db63 0975f8f aa8db63 0975f8f 2399b21 aa8db63 cbb61d9 aa8db63 0975f8f aa8db63 0975f8f aa8db63 0975f8f be0cef3 aa8db63 0975f8f aa8db63 0975f8f aa8db63 0975f8f be0cef3 aa8db63 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
# import gradio as gr
# import requests
# from TTS.api import TTS
# import os
# # β
Load TTS model (Make sure model is downloaded or install TTS first)
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
# # β
Get Ayah from AlQuran API
# def get_quran_ayat(surah_num, ayat_num):
# try:
# url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
# response = requests.get(url)
# response.raise_for_status()
# data = response.json()
# if "data" in data and len(data["data"]) >= 2:
# arabic_text = data["data"][0]["text"]
# translation = data["data"][1]["text"]
# return arabic_text, translation
# else:
# return "β Ayah not found.", "β Translation not found."
# except Exception as e:
# return f"β API Error: {e}", ""
# # β
Gradio main function
# def qari_bot(surah, ayah):
# arabic_text, translation = get_quran_ayat(surah, ayah)
# # Return early if error
# if "β" in arabic_text:
# return arabic_text, translation, None
# # Generate audio and save to file
# output_path = "ayat.wav"
# try:
# tts.tts_to_file(
# text=arabic_text,
# file_path=output_path,
# language="ar"
# )
# return arabic_text, translation, output_path
# except Exception as e:
# return arabic_text, translation, f"β TTS Error: {e}"
# # β
Gradio Interface
# interface = gr.Interface(
# fn=qari_bot,
# inputs=[
# gr.Number(label="Surah Number", value=1),
# gr.Number(label="Ayah Number", value=1)
# ],
# outputs=[
# gr.Textbox(label="π Arabic Ayah"),
# gr.Textbox(label="π Translation (English)"),
# gr.Audio(label="π Listen to Ayah")
# ],
# title="π AI Qari Bot",
# description="Enter Surah & Ayah number to hear the Quran Ayah with AI voice and English translation. Example: Surah 1, Ayah 1 = Al-Fatiha"
# )
# interface.launch()
# import gradio as gr
# import requests
# from TTS.api import TTS
# # π Map Surah names to numbers
# surah_map = {
# "Al-Fatiha": 1,
# "Al-Baqarah": 2,
# "Aal-i-Imran": 3,
# "An-Nisa": 4,
# "Al-Ma'idah": 5,
# "Al-An'am": 6,
# "Al-A'raf": 7,
# "Al-Anfal": 8,
# "At-Tawbah": 9,
# "Yunus": 10,
# # β Add more if needed...
# }
# # β
Load multilingual TTS model
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
# # π Fetch full Surah (Arabic + English)
# def fetch_surah(surah_name):
# surah_num = surah_map.get(surah_name)
# if not surah_num:
# return "Invalid Surah name.", "", None
# try:
# url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
# response = requests.get(url)
# response.raise_for_status()
# data = response.json()
# if 'data' in data and len(data['data']) == 2:
# arabic_ayahs = data['data'][0]['ayahs']
# english_ayahs = data['data'][1]['ayahs']
# arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
# english_text = "\n".join([e['text'] for e in english_ayahs])
# # Generate audio
# audio_path = "surah.wav"
# tts.tts_to_file(text=arabic_text, file_path=audio_path, language="ar")
# return arabic_text, english_text, audio_path
# else:
# return "β Surah not found.", "", None
# except Exception as e:
# return f"β API Error: {e}", "", None
# # ποΈ Gradio UI
# interface = gr.Interface(
# fn=fetch_surah,
# inputs=gr.Dropdown(choices=list(surah_map.keys()), label="Select Surah"),
# outputs=[
# gr.Textbox(label="π Arabic Ayahs"),
# gr.Textbox(label="π English Translation"),
# gr.Audio(label="π Arabic Voice Recitation")
# ],
# title="π AI Qari Bot",
# description="Select a Surah name to listen to Arabic AI recitation with English translation."
# )
# interface.launch()
# import gradio as gr
# import requests
# from TTS.api import TTS
# # π Surah name mapping
# surah_map = {
# "Al-Fatiha": 1,
# "Al-Baqarah": 2,
# "Aal-i-Imran": 3,
# "An-Nisa": 4,
# "Al-Ma'idah": 5,
# "Al-An'am": 6,
# "Al-A'raf": 7,
# "Al-Anfal": 8,
# "At-Tawbah": 9,
# "Yunus": 10,
# # Add more if needed
# }
# # β
Load TTS model with speaker
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
# available_speakers = tts.speakers
# default_speaker = available_speakers[0] # Pick the first available speaker
# # π Fetch Surah
# def fetch_surah(surah_name):
# surah_num = surah_map.get(surah_name)
# if not surah_num:
# return "β Invalid Surah name.", "", None
# try:
# url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
# response = requests.get(url)
# response.raise_for_status()
# data = response.json()
# if 'data' in data and len(data['data']) == 2:
# arabic_ayahs = data['data'][0]['ayahs']
# english_ayahs = data['data'][1]['ayahs']
# arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
# english_text = "\n".join([e['text'] for e in english_ayahs])
# # π Generate voice
# audio_path = "surah.wav"
# tts.tts_to_file(
# text=arabic_text,
# speaker=default_speaker,
# language="ar",
# file_path=audio_path
# )
# return arabic_text, english_text, audio_path
# else:
# return "β Surah not found.", "", None
# except Exception as e:
# return f"β API Error: {e}", "", None
# # ποΈ Gradio UI
# interface = gr.Interface(
# fn=fetch_surah,
# inputs=gr.Dropdown(choices=list(surah_map.keys()), label="Select Surah"),
# outputs=[
# gr.Textbox(label="π Arabic Ayahs"),
# gr.Textbox(label="π English Translation"),
# gr.Audio(label="π Arabic Voice Recitation")
# ],
# title="π AI Qari Bot",
# description=f"Select a Surah to hear AI recitation. Model speaker used: {default_speaker}"
# )
# interface.launch()
# import gradio as gr
# import requests
# from TTS.api import TTS
# # π Surah names and numbers (partial shown, full in full code)
# surah_info = {
# "Al-Fatiha": (1, 7),
# "Al-Baqarah": (2, 286),
# "Aal-i-Imran": (3, 200),
# "An-Nisa": (4, 176),
# "Al-Ma'idah": (5, 120),
# "Al-An'am": (6, 165),
# "Al-A'raf": (7, 206),
# "Al-Anfal": (8, 75),
# "At-Tawbah": (9, 129),
# "Yunus": (10, 109),
# # β Add all 114 surahs with total ayats here
# }
# # β
Load TTS model
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
# speaker = "random" # or "en_0", "ar_0"
# # π§ Main Function
# def ai_quran(surah_name, ayat_num, mode):
# surah_num, total_ayats = surah_info.get(surah_name, (None, None))
# if not surah_num:
# return "Invalid Surah", "", None
# if mode == "Full Surah":
# url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
# else: # Single Ayah
# if ayat_num < 1 or ayat_num > total_ayats:
# return "β Invalid Ayat number", "", None
# url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
# try:
# res = requests.get(url)
# res.raise_for_status()
# data = res.json()
# if mode == "Full Surah":
# arabic_ayahs = data['data'][0]['ayahs']
# english_ayahs = data['data'][1]['ayahs']
# arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
# english_text = "\n".join([e['text'] for e in english_ayahs])
# else:
# arabic_text = data['data'][0]['text']
# english_text = data['data'][1]['text']
# # π Generate TTS
# audio_path = "ayat.wav"
# tts.tts_to_file(
# text=arabic_text,
# speaker=speaker,
# language="ar",
# file_path=audio_path
# )
# return arabic_text, english_text, audio_path
# except Exception as e:
# return f"API Error: {e}", "", None
# # π§ Mode Selector
# def update_ayat_field(mode, surah):
# if mode == "Full Surah":
# return gr.update(visible=False)
# return gr.update(visible=True, maximum=surah_info[surah][1])
# # ποΈ Gradio Interface
# with gr.Blocks(title="π AI Quran Reciter") as demo:
# gr.Markdown("### π AI Quran Reciter with Voice | XTTSv2 Arabic TTS")
# with gr.Row():
# mode = gr.Radio(["Full Surah", "Single Ayat"], value="Full Surah", label="Mode")
# surah = gr.Dropdown(choices=list(surah_info.keys()), label="Select Surah", value="Al-Fatiha")
# ayat = gr.Number(label="Ayat Number", visible=False)
# run = gr.Button("π₯ Recite")
# arabic = gr.Textbox(label="π Arabic Text")
# english = gr.Textbox(label="π English Translation")
# audio = gr.Audio(label="π Listen")
# # Event handlers
# mode.change(update_ayat_field, inputs=[mode, surah], outputs=ayat)
# surah.change(update_ayat_field, inputs=[mode, surah], outputs=ayat)
# run.click(fn=ai_quran, inputs=[surah, ayat, mode], outputs=[arabic, english, audio])
# demo.launch()
# import os
# import gradio as gr
# import requests
# from TTS.api import TTS
# # β
Accept Coqui TTS license
# os.environ["COQUI_TOS_AGREED"] = "1"
# # π Surah info: name -> (number, total ayats)
# surah_info = {
# "Al-Fatiha": (1, 7),
# "Al-Baqarah": (2, 286),
# "Aal-i-Imran": (3, 200),
# "An-Nisa": (4, 176),
# "Al-Ma'idah": (5, 120),
# "Al-An'am": (6, 165),
# "Al-A'raf": (7, 206),
# "Al-Anfal": (8, 75),
# "At-Tawbah": (9, 129),
# "Yunus": (10, 109),
# "Hud": (11, 123),
# "Yusuf": (12, 111),
# "Ar-Ra'd": (13, 43),
# "Ibrahim": (14, 52),
# "Al-Hijr": (15, 99),
# "An-Nahl": (16, 128),
# "Al-Isra": (17, 111),
# "Al-Kahf": (18, 110),
# "Maryam": (19, 98),
# "Taha": (20, 135),
# # π Add remaining up to Surah 114
# "An-Nas": (114, 6),
# }
# # β
Load TTS with Arabic + speaker support
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
# speaker = "random" # or use "en_0", "ar_0" etc.
# # π Main function
# def ai_quran(surah_name, ayat_num, mode):
# surah_num, total_ayats = surah_info.get(surah_name, (None, None))
# if not surah_num:
# return "Invalid Surah", "", None
# if mode == "Full Surah":
# url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
# else: # Single Ayah
# if not ayat_num or ayat_num < 1 or ayat_num > total_ayats:
# return f"β Ayat number must be between 1 and {total_ayats}", "", None
# url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
# try:
# res = requests.get(url)
# res.raise_for_status()
# data = res.json()
# if mode == "Full Surah":
# arabic_ayahs = data['data'][0]['ayahs']
# english_ayahs = data['data'][1]['ayahs']
# arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
# english_text = "\n".join([e['text'] for e in english_ayahs])
# else:
# arabic_text = data['data'][0]['text']
# english_text = data['data'][1]['text']
# # π Generate TTS
# audio_path = "ayat.wav"
# tts.tts_to_file(
# text=arabic_text,
# speaker=speaker,
# language="ar",
# file_path=audio_path
# )
# return arabic_text, english_text, audio_path
# except Exception as e:
# return f"API Error: {e}", "", None
# # π§ Ayat field visibility toggle
# def toggle_ayat(mode, surah):
# if mode == "Full Surah":
# return gr.update(visible=False)
# return gr.update(visible=True, maximum=surah_info[surah][1])
# # ποΈ Gradio UI
# with gr.Blocks(title="π AI Quran Reciter") as demo:
# gr.Markdown("## π AI Quran Reciter with Arabic Voice | XTTSv2 Model")
# with gr.Row():
# mode = gr.Radio(["Full Surah", "Single Ayat"], label="Mode", value="Full Surah")
# surah = gr.Dropdown(choices=list(surah_info.keys()), label="Surah", value="Al-Fatiha")
# ayat = gr.Number(label="Ayat Number", visible=False)
# recite_btn = gr.Button("π₯ Recite")
# arabic = gr.Textbox(label="π Arabic Text")
# english = gr.Textbox(label="π English Translation")
# audio = gr.Audio(label="π Listen")
# # Event hooks
# mode.change(toggle_ayat, inputs=[mode, surah], outputs=ayat)
# surah.change(toggle_ayat, inputs=[mode, surah], outputs=ayat)
# recite_btn.click(ai_quran, inputs=[surah, ayat, mode], outputs=[arabic, english, audio])
# demo.launch()
import os
import gradio as gr
import requests
from TTS.api import TTS
# β
Accept Coqui license for Hugging Face Space
os.environ["COQUI_TOS_AGREED"] = "1"
# β
Fix PyTorch 2.6 checkpoint loading issue
import torch
from torch.serialization import add_safe_globals
from TTS.tts.configs.xtts_config import XttsConfig
add_safe_globals([XttsConfig])
# β
Load multilingual XTTSv2 model (speaker supported)
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
speaker_wav = None # Optional: path to your own .wav voice file
# Surah list with Ayah counts (can be extended)
surah_info = {
"Al-Fatiha": (1, 7),
"Al-Baqarah": (2, 286),
"Aal-i-Imran": (3, 200),
"An-Nisa": (4, 176),
"Al-Ma'idah": (5, 120),
"Al-An'am": (6, 165),
"Al-A'raf": (7, 206),
"Al-Anfal": (8, 75),
"At-Tawbah": (9, 129),
"Yunus": (10, 109),
"Hud": (11, 123),
"Yusuf": (12, 111),
"Ar-Ra'd": (13, 43),
"Ibrahim": (14, 52),
"Al-Hijr": (15, 99),
"An-Nahl": (16, 128),
"Al-Isra": (17, 111),
"Al-Kahf": (18, 110),
"Maryam": (19, 98),
"Taha": (20, 135),
"Al-Anbiya": (21, 112),
"Al-Hajj": (22, 78),
"Al-Mu'minun": (23, 118),
"An-Nur": (24, 64),
"Al-Furqan": (25, 77),
"Ash-Shu'ara": (26, 227),
"An-Naml": (27, 93),
"Al-Qasas": (28, 88),
"Al-Ankabut": (29, 69),
"Ar-Rum": (30, 60),
"Luqman": (31, 34),
"As-Sajda": (32, 30),
"Al-Ahzab": (33, 73),
"Saba": (34, 54),
"Fatir": (35, 45),
"Ya-Sin": (36, 83),
"As-Saffat": (37, 182),
"Sad": (38, 88),
"Az-Zumar": (39, 75),
"Ghafir": (40, 85),
"Fussilat": (41, 54),
"Ash-Shura": (42, 53),
"Az-Zukhruf": (43, 89),
"Ad-Dukhan": (44, 59),
"Al-Jathiya": (45, 37),
"Al-Ahqaf": (46, 35),
"Muhammad": (47, 38),
"Al-Fath": (48, 29),
"Al-Hujurat": (49, 18),
"Qaf": (50, 45),
"Adh-Dhariyat": (51, 60),
"At-Tur": (52, 49),
"An-Najm": (53, 62),
"Al-Qamar": (54, 55),
"Ar-Rahman": (55, 78),
"Al-Waqia": (56, 96),
"Al-Hadid": (57, 29),
"Al-Mujadila": (58, 22),
"Al-Hashr": (59, 24),
"Al-Mumtahina": (60, 13),
"As-Saff": (61, 14),
"Al-Jumu'a": (62, 11),
"Al-Munafiqun": (63, 11),
"At-Taghabun": (64, 18),
"At-Talaq": (65, 12),
"At-Tahrim": (66, 12),
"Al-Mulk": (67, 30),
"Al-Qalam": (68, 52),
"Al-Haqqa": (69, 52),
"Al-Ma'arij": (70, 44),
"Nuh": (71, 28),
"Al-Jinn": (72, 28),
"Al-Muzzammil": (73, 20),
"Al-Muddaththir": (74, 56),
"Al-Qiyamah": (75, 40),
"Al-Insan": (76, 31),
"Al-Mursalat": (77, 50),
"An-Naba": (78, 40),
"An-Nazi'at": (79, 46),
"Abasa": (80, 42),
"At-Takwir": (81, 29),
"Al-Infitar": (82, 19),
"Al-Mutaffifin": (83, 36),
"Al-Inshiqaq": (84, 25),
"Al-Buruj": (85, 22),
"At-Tariq": (86, 17),
"Al-A'la": (87, 19),
"Al-Ghashiyah": (88, 26),
"Al-Fajr": (89, 30),
"Al-Balad": (90, 20),
"Ash-Shams": (91, 15),
"Al-Layl": (92, 21),
"Ad-Duha": (93, 11),
"Ash-Sharh": (94, 8),
"At-Tin": (95, 8),
"Al-Alaq": (96, 19),
"Al-Qadr": (97, 5),
"Al-Bayyina": (98, 8),
"Az-Zalzalah": (99, 8),
"Al-Adiyat": (100, 11),
"Al-Qari'a": (101, 11),
"At-Takathur": (102, 8),
"Al-Asr": (103, 3),
"Al-Humazah": (104, 9),
"Al-Fil": (105, 5),
"Quraysh": (106, 4),
"Al-Ma'un": (107, 7),
"Al-Kawthar": (108, 3),
"Al-Kafirun": (109, 6),
"An-Nasr": (110, 3),
"Al-Masad": (111, 5),
"Al-Ikhlas": (112, 4),
"Al-Falaq": (113, 5),
"An-Nas": (114, 6)
}
def recite_quran(surah_name, ayah_number, mode):
surah_num, total_ayahs = surah_info[surah_name]
if mode == "Full Surah":
url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
else:
if not (1 <= ayah_number <= total_ayahs):
return f"Ayat must be between 1 and {total_ayahs}", "", None
url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_number}/editions/quran-simple,en.asad"
try:
res = requests.get(url)
res.raise_for_status()
data = res.json()
if mode == "Full Surah":
arabic = "\n".join([a['text'] for a in data['data'][0]['ayahs']])
english = "\n".join([a['text'] for a in data['data'][1]['ayahs']])
else:
arabic = data['data'][0]['text']
english = data['data'][1]['text']
audio_path = "output.wav"
tts.tts_to_file(
text=arabic,
speaker_wav=speaker_wav,
language="ar",
file_path=audio_path
)
return arabic, english, audio_path
except Exception as e:
return f"β Error: {e}", "", None
def toggle_ayah_visibility(mode, surah):
if mode == "Full Surah":
return gr.update(visible=False)
return gr.update(visible=True, maximum=surah_info[surah][1])
# Gradio Interface
with gr.Blocks(title="π Quran AI Reader (with Speaker)") as demo:
gr.Markdown("## π AI Quran Reciter using XTTSv2 (Arabic Voice with Speaker)")
with gr.Row():
mode = gr.Radio(["Full Surah", "Single Ayat"], label="Mode", value="Full Surah")
surah = gr.Dropdown(choices=list(surah_info.keys()), label="Surah", value="Al-Fatiha")
ayah = gr.Number(label="Ayat Number", visible=False)
recite_btn = gr.Button("π§ Recite")
arabic_box = gr.Textbox(label="π Arabic Text")
english_box = gr.Textbox(label="π English Translation")
audio_output = gr.Audio(label="π Listen")
mode.change(toggle_ayah_visibility, inputs=[mode, surah], outputs=ayah)
surah.change(toggle_ayah_visibility, inputs=[mode, surah], outputs=ayah)
recite_btn.click(recite_quran, inputs=[surah, ayah, mode], outputs=[arabic_box, english_box, audio_output])
demo.launch()
|