asad231 commited on
Commit
cbb61d9
Β·
verified Β·
1 Parent(s): 5f32203

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
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()