Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from TTS.api import TTS
|
4 |
+
import os
|
5 |
+
|
6 |
+
# β
Load TTS model (Make sure model is downloaded or install TTS first)
|
7 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
8 |
+
|
9 |
+
# β
Get Ayah from AlQuran API
|
10 |
+
def get_quran_ayat(surah_num, ayat_num):
|
11 |
+
try:
|
12 |
+
url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
|
13 |
+
response = requests.get(url)
|
14 |
+
response.raise_for_status()
|
15 |
+
data = response.json()
|
16 |
+
|
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 "β Ayah not found.", "β Translation not found."
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return f"β API Error: {e}", ""
|
26 |
+
|
27 |
+
# β
Gradio main function
|
28 |
+
def qari_bot(surah, ayah):
|
29 |
+
arabic_text, translation = get_quran_ayat(surah, ayah)
|
30 |
+
|
31 |
+
# Return early if error
|
32 |
+
if "β" in arabic_text:
|
33 |
+
return arabic_text, translation, None
|
34 |
+
|
35 |
+
# Generate audio and save to file
|
36 |
+
output_path = "ayat.wav"
|
37 |
+
try:
|
38 |
+
tts.tts_to_file(
|
39 |
+
text=arabic_text,
|
40 |
+
file_path=output_path,
|
41 |
+
language="ar"
|
42 |
+
)
|
43 |
+
return arabic_text, translation, output_path
|
44 |
+
except Exception as e:
|
45 |
+
return arabic_text, translation, f"β TTS Error: {e}"
|
46 |
+
|
47 |
+
# β
Gradio Interface
|
48 |
+
interface = gr.Interface(
|
49 |
+
fn=qari_bot,
|
50 |
+
inputs=[
|
51 |
+
gr.Number(label="Surah Number", value=1),
|
52 |
+
gr.Number(label="Ayah Number", value=1)
|
53 |
+
],
|
54 |
+
outputs=[
|
55 |
+
gr.Textbox(label="π Arabic Ayah"),
|
56 |
+
gr.Textbox(label="π Translation (English)"),
|
57 |
+
gr.Audio(label="π Listen to Ayah")
|
58 |
+
],
|
59 |
+
title="π AI Qari Bot",
|
60 |
+
description="Enter Surah & Ayah number to hear the Quran Ayah with AI voice and English translation. Example: Surah 1, Ayah 1 = Al-Fatiha"
|
61 |
+
)
|
62 |
+
|
63 |
+
interface.launch()
|