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() | |