Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -203,11 +203,106 @@
|
|
| 203 |
|
| 204 |
# interface.launch()
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
import gradio as gr
|
| 207 |
import requests
|
| 208 |
from TTS.api import TTS
|
| 209 |
|
| 210 |
-
#
|
|
|
|
|
|
|
|
|
|
| 211 |
surah_info = {
|
| 212 |
"Al-Fatiha": (1, 7),
|
| 213 |
"Al-Baqarah": (2, 286),
|
|
@@ -219,14 +314,25 @@ surah_info = {
|
|
| 219 |
"Al-Anfal": (8, 75),
|
| 220 |
"At-Tawbah": (9, 129),
|
| 221 |
"Yunus": (10, 109),
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
}
|
| 224 |
|
| 225 |
-
# β
Load TTS
|
| 226 |
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
|
| 227 |
-
speaker = "random" # or "en_0", "ar_0"
|
| 228 |
|
| 229 |
-
#
|
| 230 |
def ai_quran(surah_name, ayat_num, mode):
|
| 231 |
surah_num, total_ayats = surah_info.get(surah_name, (None, None))
|
| 232 |
if not surah_num:
|
|
@@ -235,8 +341,8 @@ def ai_quran(surah_name, ayat_num, mode):
|
|
| 235 |
if mode == "Full Surah":
|
| 236 |
url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
|
| 237 |
else: # Single Ayah
|
| 238 |
-
if ayat_num < 1 or ayat_num > total_ayats:
|
| 239 |
-
return "β
|
| 240 |
url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
|
| 241 |
|
| 242 |
try:
|
|
@@ -267,29 +373,29 @@ def ai_quran(surah_name, ayat_num, mode):
|
|
| 267 |
except Exception as e:
|
| 268 |
return f"API Error: {e}", "", None
|
| 269 |
|
| 270 |
-
# π§
|
| 271 |
-
def
|
| 272 |
if mode == "Full Surah":
|
| 273 |
return gr.update(visible=False)
|
| 274 |
return gr.update(visible=True, maximum=surah_info[surah][1])
|
| 275 |
|
| 276 |
-
# ποΈ Gradio
|
| 277 |
with gr.Blocks(title="π AI Quran Reciter") as demo:
|
| 278 |
-
gr.Markdown("
|
| 279 |
|
| 280 |
with gr.Row():
|
| 281 |
-
mode = gr.Radio(["Full Surah", "Single Ayat"],
|
| 282 |
-
surah = gr.Dropdown(choices=list(surah_info.keys()), label="
|
| 283 |
ayat = gr.Number(label="Ayat Number", visible=False)
|
| 284 |
|
| 285 |
-
|
| 286 |
arabic = gr.Textbox(label="π Arabic Text")
|
| 287 |
english = gr.Textbox(label="π English Translation")
|
| 288 |
audio = gr.Audio(label="π Listen")
|
| 289 |
|
| 290 |
-
# Event
|
| 291 |
-
mode.change(
|
| 292 |
-
surah.change(
|
| 293 |
-
|
| 294 |
|
| 295 |
-
demo.launch()
|
|
|
|
| 203 |
|
| 204 |
# interface.launch()
|
| 205 |
|
| 206 |
+
# import gradio as gr
|
| 207 |
+
# import requests
|
| 208 |
+
# from TTS.api import TTS
|
| 209 |
+
|
| 210 |
+
# # π Surah names and numbers (partial shown, full in full code)
|
| 211 |
+
# surah_info = {
|
| 212 |
+
# "Al-Fatiha": (1, 7),
|
| 213 |
+
# "Al-Baqarah": (2, 286),
|
| 214 |
+
# "Aal-i-Imran": (3, 200),
|
| 215 |
+
# "An-Nisa": (4, 176),
|
| 216 |
+
# "Al-Ma'idah": (5, 120),
|
| 217 |
+
# "Al-An'am": (6, 165),
|
| 218 |
+
# "Al-A'raf": (7, 206),
|
| 219 |
+
# "Al-Anfal": (8, 75),
|
| 220 |
+
# "At-Tawbah": (9, 129),
|
| 221 |
+
# "Yunus": (10, 109),
|
| 222 |
+
# # β Add all 114 surahs with total ayats here
|
| 223 |
+
# }
|
| 224 |
+
|
| 225 |
+
# # β
Load TTS model
|
| 226 |
+
# tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
|
| 227 |
+
# speaker = "random" # or "en_0", "ar_0"
|
| 228 |
+
|
| 229 |
+
# # π§ Main Function
|
| 230 |
+
# def ai_quran(surah_name, ayat_num, mode):
|
| 231 |
+
# surah_num, total_ayats = surah_info.get(surah_name, (None, None))
|
| 232 |
+
# if not surah_num:
|
| 233 |
+
# return "Invalid Surah", "", None
|
| 234 |
+
|
| 235 |
+
# if mode == "Full Surah":
|
| 236 |
+
# url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
|
| 237 |
+
# else: # Single Ayah
|
| 238 |
+
# if ayat_num < 1 or ayat_num > total_ayats:
|
| 239 |
+
# return "β Invalid Ayat number", "", None
|
| 240 |
+
# url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
|
| 241 |
+
|
| 242 |
+
# try:
|
| 243 |
+
# res = requests.get(url)
|
| 244 |
+
# res.raise_for_status()
|
| 245 |
+
# data = res.json()
|
| 246 |
+
|
| 247 |
+
# if mode == "Full Surah":
|
| 248 |
+
# arabic_ayahs = data['data'][0]['ayahs']
|
| 249 |
+
# english_ayahs = data['data'][1]['ayahs']
|
| 250 |
+
# arabic_text = "\n".join([a['text'] for a in arabic_ayahs])
|
| 251 |
+
# english_text = "\n".join([e['text'] for e in english_ayahs])
|
| 252 |
+
# else:
|
| 253 |
+
# arabic_text = data['data'][0]['text']
|
| 254 |
+
# english_text = data['data'][1]['text']
|
| 255 |
+
|
| 256 |
+
# # π Generate TTS
|
| 257 |
+
# audio_path = "ayat.wav"
|
| 258 |
+
# tts.tts_to_file(
|
| 259 |
+
# text=arabic_text,
|
| 260 |
+
# speaker=speaker,
|
| 261 |
+
# language="ar",
|
| 262 |
+
# file_path=audio_path
|
| 263 |
+
# )
|
| 264 |
+
|
| 265 |
+
# return arabic_text, english_text, audio_path
|
| 266 |
+
|
| 267 |
+
# except Exception as e:
|
| 268 |
+
# return f"API Error: {e}", "", None
|
| 269 |
+
|
| 270 |
+
# # π§ Mode Selector
|
| 271 |
+
# def update_ayat_field(mode, surah):
|
| 272 |
+
# if mode == "Full Surah":
|
| 273 |
+
# return gr.update(visible=False)
|
| 274 |
+
# return gr.update(visible=True, maximum=surah_info[surah][1])
|
| 275 |
+
|
| 276 |
+
# # ποΈ Gradio Interface
|
| 277 |
+
# with gr.Blocks(title="π AI Quran Reciter") as demo:
|
| 278 |
+
# gr.Markdown("### π AI Quran Reciter with Voice | XTTSv2 Arabic TTS")
|
| 279 |
+
|
| 280 |
+
# with gr.Row():
|
| 281 |
+
# mode = gr.Radio(["Full Surah", "Single Ayat"], value="Full Surah", label="Mode")
|
| 282 |
+
# surah = gr.Dropdown(choices=list(surah_info.keys()), label="Select Surah", value="Al-Fatiha")
|
| 283 |
+
# ayat = gr.Number(label="Ayat Number", visible=False)
|
| 284 |
+
|
| 285 |
+
# run = gr.Button("π₯ Recite")
|
| 286 |
+
# arabic = gr.Textbox(label="π Arabic Text")
|
| 287 |
+
# english = gr.Textbox(label="π English Translation")
|
| 288 |
+
# audio = gr.Audio(label="π Listen")
|
| 289 |
+
|
| 290 |
+
# # Event handlers
|
| 291 |
+
# mode.change(update_ayat_field, inputs=[mode, surah], outputs=ayat)
|
| 292 |
+
# surah.change(update_ayat_field, inputs=[mode, surah], outputs=ayat)
|
| 293 |
+
# run.click(fn=ai_quran, inputs=[surah, ayat, mode], outputs=[arabic, english, audio])
|
| 294 |
+
|
| 295 |
+
# demo.launch()
|
| 296 |
+
|
| 297 |
+
import os
|
| 298 |
import gradio as gr
|
| 299 |
import requests
|
| 300 |
from TTS.api import TTS
|
| 301 |
|
| 302 |
+
# β
Accept Coqui TTS license
|
| 303 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 304 |
+
|
| 305 |
+
# π Surah info: name -> (number, total ayats)
|
| 306 |
surah_info = {
|
| 307 |
"Al-Fatiha": (1, 7),
|
| 308 |
"Al-Baqarah": (2, 286),
|
|
|
|
| 314 |
"Al-Anfal": (8, 75),
|
| 315 |
"At-Tawbah": (9, 129),
|
| 316 |
"Yunus": (10, 109),
|
| 317 |
+
"Hud": (11, 123),
|
| 318 |
+
"Yusuf": (12, 111),
|
| 319 |
+
"Ar-Ra'd": (13, 43),
|
| 320 |
+
"Ibrahim": (14, 52),
|
| 321 |
+
"Al-Hijr": (15, 99),
|
| 322 |
+
"An-Nahl": (16, 128),
|
| 323 |
+
"Al-Isra": (17, 111),
|
| 324 |
+
"Al-Kahf": (18, 110),
|
| 325 |
+
"Maryam": (19, 98),
|
| 326 |
+
"Taha": (20, 135),
|
| 327 |
+
# π Add remaining up to Surah 114
|
| 328 |
+
"An-Nas": (114, 6),
|
| 329 |
}
|
| 330 |
|
| 331 |
+
# β
Load TTS with Arabic + speaker support
|
| 332 |
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
|
| 333 |
+
speaker = "random" # or use "en_0", "ar_0" etc.
|
| 334 |
|
| 335 |
+
# π Main function
|
| 336 |
def ai_quran(surah_name, ayat_num, mode):
|
| 337 |
surah_num, total_ayats = surah_info.get(surah_name, (None, None))
|
| 338 |
if not surah_num:
|
|
|
|
| 341 |
if mode == "Full Surah":
|
| 342 |
url = f"https://api.alquran.cloud/v1/surah/{surah_num}/editions/quran-simple,en.asad"
|
| 343 |
else: # Single Ayah
|
| 344 |
+
if not ayat_num or ayat_num < 1 or ayat_num > total_ayats:
|
| 345 |
+
return f"β Ayat number must be between 1 and {total_ayats}", "", None
|
| 346 |
url = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayat_num}/editions/quran-simple,en.asad"
|
| 347 |
|
| 348 |
try:
|
|
|
|
| 373 |
except Exception as e:
|
| 374 |
return f"API Error: {e}", "", None
|
| 375 |
|
| 376 |
+
# π§ Ayat field visibility toggle
|
| 377 |
+
def toggle_ayat(mode, surah):
|
| 378 |
if mode == "Full Surah":
|
| 379 |
return gr.update(visible=False)
|
| 380 |
return gr.update(visible=True, maximum=surah_info[surah][1])
|
| 381 |
|
| 382 |
+
# ποΈ Gradio UI
|
| 383 |
with gr.Blocks(title="π AI Quran Reciter") as demo:
|
| 384 |
+
gr.Markdown("## π AI Quran Reciter with Arabic Voice | XTTSv2 Model")
|
| 385 |
|
| 386 |
with gr.Row():
|
| 387 |
+
mode = gr.Radio(["Full Surah", "Single Ayat"], label="Mode", value="Full Surah")
|
| 388 |
+
surah = gr.Dropdown(choices=list(surah_info.keys()), label="Surah", value="Al-Fatiha")
|
| 389 |
ayat = gr.Number(label="Ayat Number", visible=False)
|
| 390 |
|
| 391 |
+
recite_btn = gr.Button("π₯ Recite")
|
| 392 |
arabic = gr.Textbox(label="π Arabic Text")
|
| 393 |
english = gr.Textbox(label="π English Translation")
|
| 394 |
audio = gr.Audio(label="π Listen")
|
| 395 |
|
| 396 |
+
# Event hooks
|
| 397 |
+
mode.change(toggle_ayat, inputs=[mode, surah], outputs=ayat)
|
| 398 |
+
surah.change(toggle_ayat, inputs=[mode, surah], outputs=ayat)
|
| 399 |
+
recite_btn.click(ai_quran, inputs=[surah, ayat, mode], outputs=[arabic, english, audio])
|
| 400 |
|
| 401 |
+
demo.launch()
|