Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,35 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
from TTS.api import TTS
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
# Note: This is an example, you can use other Arabic-capable TTS models from Hugging Face or Coqui.
|
| 7 |
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def get_quran_ayat(surah_num, ayat_num):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def qari_bot(surah, ayat):
|
| 23 |
arabic_text, translation = get_quran_ayat(surah, ayat)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
tts.tts_to_file(
|
| 26 |
text=arabic_text,
|
| 27 |
speaker_wav=None,
|
|
@@ -30,7 +39,7 @@ def qari_bot(surah, ayat):
|
|
| 30 |
)
|
| 31 |
return arabic_text, translation, "ayat.wav"
|
| 32 |
|
| 33 |
-
#
|
| 34 |
interface = gr.Interface(
|
| 35 |
fn=qari_bot,
|
| 36 |
inputs=[
|
|
@@ -43,12 +52,8 @@ interface = gr.Interface(
|
|
| 43 |
gr.Audio(label="🔊 Listen to Ayat")
|
| 44 |
],
|
| 45 |
title="📖 AI Qari Bot",
|
| 46 |
-
description=(
|
| 47 |
-
"Enter any Surah & Ayat number to hear the Quran Ayat in Arabic "
|
| 48 |
-
"with AI voice and see its translation.\n"
|
| 49 |
-
"Example: Surah 1, Ayat 1 (Al-Fatiha)"
|
| 50 |
-
),
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# 5️⃣ Run on Spaces
|
| 54 |
interface.launch()
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
from TTS.api import TTS
|
| 4 |
|
| 5 |
+
# ✅ Load multilingual TTS model (Coqui or Hugging Face compatible)
|
|
|
|
| 6 |
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
| 7 |
|
| 8 |
+
# ✅ Use correct API endpoint
|
| 9 |
def get_quran_ayat(surah_num, ayat_num):
|
| 10 |
+
try:
|
| 11 |
+
url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
|
| 12 |
+
response = requests.get(url)
|
| 13 |
+
response.raise_for_status() # Raise HTTPError for bad response
|
| 14 |
+
data = response.json()
|
| 15 |
+
|
| 16 |
+
# Check if data is valid
|
| 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 "Ayat not found.", "Translation not found."
|
| 23 |
+
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"API Error: {e}", ""
|
| 26 |
+
|
| 27 |
+
# ✅ Main function for Gradio
|
| 28 |
def qari_bot(surah, ayat):
|
| 29 |
arabic_text, translation = get_quran_ayat(surah, ayat)
|
| 30 |
+
if "Error" in arabic_text or "not found" in arabic_text:
|
| 31 |
+
return arabic_text, translation, None
|
| 32 |
+
|
| 33 |
+
# Generate TTS
|
| 34 |
tts.tts_to_file(
|
| 35 |
text=arabic_text,
|
| 36 |
speaker_wav=None,
|
|
|
|
| 39 |
)
|
| 40 |
return arabic_text, translation, "ayat.wav"
|
| 41 |
|
| 42 |
+
# ✅ Gradio Interface
|
| 43 |
interface = gr.Interface(
|
| 44 |
fn=qari_bot,
|
| 45 |
inputs=[
|
|
|
|
| 52 |
gr.Audio(label="🔊 Listen to Ayat")
|
| 53 |
],
|
| 54 |
title="📖 AI Qari Bot",
|
| 55 |
+
description="Enter Surah & Ayat number to hear the Quran Ayat with AI voice and translation. Example: Surah 1, Ayat 1 (Al-Fatiha)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
interface.launch()
|
| 59 |
+
|