Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from TTS.api import TTS
|
4 |
-
import os
|
5 |
|
6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
8 |
|
9 |
-
#
|
10 |
-
def
|
|
|
|
|
|
|
|
|
11 |
try:
|
12 |
-
url = f"https://api.alquran.cloud/v1/
|
13 |
response = requests.get(url)
|
14 |
response.raise_for_status()
|
15 |
data = response.json()
|
16 |
|
17 |
-
if
|
18 |
-
|
19 |
-
|
20 |
-
return arabic_text, translation
|
21 |
-
else:
|
22 |
-
return "β Ayah not found.", "β Translation not found."
|
23 |
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
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
|
46 |
|
47 |
-
#
|
48 |
interface = gr.Interface(
|
49 |
-
fn=
|
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
|
56 |
-
gr.Textbox(label="π Translation
|
57 |
-
gr.Audio(label="π
|
58 |
],
|
59 |
title="π AI Qari Bot",
|
60 |
-
description="
|
61 |
)
|
62 |
|
63 |
-
interface.launch()
|
|
|
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()
|
64 |
+
|
65 |
import gradio as gr
|
66 |
import requests
|
67 |
from TTS.api import TTS
|
|
|
68 |
|
69 |
+
# π Map Surah names to numbers
|
70 |
+
surah_map = {
|
71 |
+
"Al-Fatiha": 1,
|
72 |
+
"Al-Baqarah": 2,
|
73 |
+
"Aal-i-Imran": 3,
|
74 |
+
"An-Nisa": 4,
|
75 |
+
"Al-Ma'idah": 5,
|
76 |
+
"Al-An'am": 6,
|
77 |
+
"Al-A'raf": 7,
|
78 |
+
"Al-Anfal": 8,
|
79 |
+
"At-Tawbah": 9,
|
80 |
+
"Yunus": 10,
|
81 |
+
# β Add more if needed...
|
82 |
+
}
|
83 |
+
|
84 |
+
# β
Load multilingual TTS model
|
85 |
tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
86 |
|
87 |
+
# π Fetch full Surah (Arabic + English)
|
88 |
+
def fetch_surah(surah_name):
|
89 |
+
surah_num = surah_map.get(surah_name)
|
90 |
+
if not surah_num:
|
91 |
+
return "Invalid Surah name.", "", None
|
92 |
+
|
93 |
try:
|
94 |
+
url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
|
95 |
response = requests.get(url)
|
96 |
response.raise_for_status()
|
97 |
data = response.json()
|
98 |
|
99 |
+
if 'data' in data and len(data['data']) == 2:
|
100 |
+
arabic_ayahs = data['data'][0]['ayahs']
|
101 |
+
english_ayahs = data['data'][1]['ayahs']
|
|
|
|
|
|
|
102 |
|
103 |
+
arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
|
104 |
+
english_text = "\n".join([e['text'] for e in english_ayahs])
|
105 |
|
106 |
+
# Generate audio
|
107 |
+
audio_path = "surah.wav"
|
108 |
+
tts.tts_to_file(text=arabic_text, file_path=audio_path, language="ar")
|
109 |
+
return arabic_text, english_text, audio_path
|
110 |
+
else:
|
111 |
+
return "β Surah not found.", "", None
|
|
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
except Exception as e:
|
114 |
+
return f"β API Error: {e}", "", None
|
115 |
|
116 |
+
# ποΈ Gradio UI
|
117 |
interface = gr.Interface(
|
118 |
+
fn=fetch_surah,
|
119 |
+
inputs=gr.Dropdown(choices=list(surah_map.keys()), label="Select Surah"),
|
|
|
|
|
|
|
120 |
outputs=[
|
121 |
+
gr.Textbox(label="π Arabic Ayahs"),
|
122 |
+
gr.Textbox(label="π English Translation"),
|
123 |
+
gr.Audio(label="π Arabic Voice Recitation")
|
124 |
],
|
125 |
title="π AI Qari Bot",
|
126 |
+
description="Select a Surah name to listen to Arabic AI recitation with English translation."
|
127 |
)
|
128 |
|
129 |
+
interface.launch()
|