Spaces:
Sleeping
Sleeping
# 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() | |