Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,19 +6,10 @@ import requests
|
|
6 |
from datetime import datetime
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
-
# ✅ MUST be the first Streamlit command
|
10 |
st.set_page_config(page_title="Kalam Comfort", page_icon="📚")
|
11 |
-
|
12 |
-
# --------------------------
|
13 |
-
# 🔐 Load environment variables (optional)
|
14 |
-
# --------------------------
|
15 |
load_dotenv()
|
16 |
|
17 |
-
#
|
18 |
-
# 📁 Create or load files
|
19 |
-
# --------------------------
|
20 |
-
|
21 |
-
# 1. moods.json
|
22 |
default_moods = {
|
23 |
"anxious": ["13:28", "2:286", "10:62"],
|
24 |
"sad": ["94:5", "93:6", "65:7"],
|
@@ -37,34 +28,30 @@ if not os.path.exists("moods.json"):
|
|
37 |
with open("moods.json", "r") as f:
|
38 |
moods = json.load(f)
|
39 |
|
40 |
-
# 2. reflections.json
|
41 |
if not os.path.exists("reflections.json"):
|
42 |
with open("reflections.json", "w") as f:
|
43 |
json.dump({}, f)
|
44 |
with open("reflections.json", "r") as f:
|
45 |
reflections = json.load(f)
|
46 |
|
47 |
-
# 3. Optional: warn if .env doesn't exist
|
48 |
if not os.path.exists(".env"):
|
49 |
-
st.warning("⚠️ Optional: .env file not found. It's not required unless
|
50 |
|
51 |
-
#
|
52 |
-
# 🌙 Streamlit UI
|
53 |
-
# --------------------------
|
54 |
st.title("📚 Kalam Comfort")
|
55 |
st.subheader("Find Quranic comfort by emotion 💖")
|
56 |
|
57 |
selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
|
58 |
|
59 |
if st.button("🔁 Show another verse"):
|
60 |
-
st.
|
61 |
|
62 |
if selected_mood:
|
63 |
verse_list = moods[selected_mood]
|
64 |
verse_ref = random.choice(verse_list)
|
65 |
surah_num, ayah_num = verse_ref.split(":")
|
66 |
|
67 |
-
#
|
68 |
quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad,ur.jalandhry"
|
69 |
response = requests.get(quran_api)
|
70 |
|
@@ -80,54 +67,53 @@ if selected_mood:
|
|
80 |
st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
81 |
st.markdown(f"**📖 English:** *{english}*")
|
82 |
st.markdown(f"**📖 Urdu:** {urdu}")
|
83 |
-
|
84 |
-
# Tafsir and Dua
|
85 |
-
tafsir_map = {
|
86 |
-
"anxious": "The heart finds true peace in the remembrance of Allah.",
|
87 |
-
"sad": "Ease always follows hardship — hold on to hope.",
|
88 |
-
"hopeless": "Allah’s mercy is vast. Never give up.",
|
89 |
-
"grateful": "Gratitude brings increase — in peace, health, and blessings.",
|
90 |
-
"lonely": "Allah is always near. Call upon Him.",
|
91 |
-
"angry": "Restrain your anger and be among the righteous.",
|
92 |
-
"lost": "He guided you when you were unaware.",
|
93 |
-
"tired": "After difficulty comes ease — stay patient.",
|
94 |
-
"afraid": "Allah never burdens a soul more than it can bear."
|
95 |
-
}
|
96 |
-
|
97 |
-
dua_map = {
|
98 |
-
"anxious": "اللهم اجعل قلبي مطمئنًا بذكرك\n*O Allah, make my heart tranquil with Your remembrance.*",
|
99 |
-
"sad": "اللهم اجبر قلبي جبرا يتعجب له أهل السماوات والأرض\n*O Allah, mend my heart in a way that amazes the heavens and earth.*",
|
100 |
-
"hopeless": "رب لا تذرني فردا وأنت خير الوارثين\n*My Lord, do not leave me alone — You are the Best Inheritor.*",
|
101 |
-
"grateful": "اللهم اجعلني لك شَكُورًا\n*O Allah, make me deeply grateful to You.*",
|
102 |
-
"lonely": "اللهم كن معي حين لا يكون أحد بجانبي\n*O Allah, be with me when no one else is.*",
|
103 |
-
"angry": "اللهم ارزقني الحلم عند الغضب\n*O Allah, grant me forbearance when I’m angry.*",
|
104 |
-
"lost": "اللهم دلّني على صراطك المستقيم\n*O Allah, guide me to Your straight path.*",
|
105 |
-
"tired": "اللهم جدد طاقتي، وارزقني راحة البال\n*O Allah, renew my energy and grant me peace of mind.*",
|
106 |
-
"afraid": "ربِّ أعني ولا تعن عليّ\n*My Lord, support me and not against me.*"
|
107 |
-
}
|
108 |
-
|
109 |
-
st.markdown("### ✨ Tafsir")
|
110 |
-
st.info(tafsir_map.get(selected_mood, "Let the verse speak to your heart."))
|
111 |
-
|
112 |
-
st.markdown("### 🤲 Dua")
|
113 |
-
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation."))
|
114 |
-
|
115 |
-
# Reflection Input
|
116 |
-
st.markdown("### 📝 Your Reflection")
|
117 |
-
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
|
118 |
-
|
119 |
-
if st.button("💾 Save Reflection"):
|
120 |
-
reflections[selected_mood] = {
|
121 |
-
"text": reflection_input,
|
122 |
-
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
|
123 |
-
}
|
124 |
-
with open("reflections.json", "w") as f:
|
125 |
-
json.dump(reflections, f, indent=4)
|
126 |
-
st.success("Reflection saved.")
|
127 |
-
|
128 |
-
if selected_mood in reflections:
|
129 |
-
last = reflections[selected_mood]
|
130 |
-
st.markdown("#### 📜 Last Saved Reflection:")
|
131 |
-
st.markdown(f"**{last['timestamp']}** — {last['text']}")
|
132 |
else:
|
133 |
-
st.error("❌
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from datetime import datetime
|
7 |
from dotenv import load_dotenv
|
8 |
|
|
|
9 |
st.set_page_config(page_title="Kalam Comfort", page_icon="📚")
|
|
|
|
|
|
|
|
|
10 |
load_dotenv()
|
11 |
|
12 |
+
# ---------- File Setup ----------
|
|
|
|
|
|
|
|
|
13 |
default_moods = {
|
14 |
"anxious": ["13:28", "2:286", "10:62"],
|
15 |
"sad": ["94:5", "93:6", "65:7"],
|
|
|
28 |
with open("moods.json", "r") as f:
|
29 |
moods = json.load(f)
|
30 |
|
|
|
31 |
if not os.path.exists("reflections.json"):
|
32 |
with open("reflections.json", "w") as f:
|
33 |
json.dump({}, f)
|
34 |
with open("reflections.json", "r") as f:
|
35 |
reflections = json.load(f)
|
36 |
|
|
|
37 |
if not os.path.exists(".env"):
|
38 |
+
st.warning("⚠️ Optional: .env file not found. It's not required unless using secrets.")
|
39 |
|
40 |
+
# ---------- UI ----------
|
|
|
|
|
41 |
st.title("📚 Kalam Comfort")
|
42 |
st.subheader("Find Quranic comfort by emotion 💖")
|
43 |
|
44 |
selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
|
45 |
|
46 |
if st.button("🔁 Show another verse"):
|
47 |
+
st.rerun()
|
48 |
|
49 |
if selected_mood:
|
50 |
verse_list = moods[selected_mood]
|
51 |
verse_ref = random.choice(verse_list)
|
52 |
surah_num, ayah_num = verse_ref.split(":")
|
53 |
|
54 |
+
# 1. Quran Arabic, English, Urdu
|
55 |
quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad,ur.jalandhry"
|
56 |
response = requests.get(quran_api)
|
57 |
|
|
|
67 |
st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
68 |
st.markdown(f"**📖 English:** *{english}*")
|
69 |
st.markdown(f"**📖 Urdu:** {urdu}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
else:
|
71 |
+
st.error("❌ Failed to fetch Quranic text.")
|
72 |
+
st.stop()
|
73 |
+
|
74 |
+
# 2. Tafsir (Urdu)
|
75 |
+
tafsir_api = f"https://api.quranenc.com/v1/translation/tafsir?language=ur&verse_id={surah_num}:{ayah_num}"
|
76 |
+
tafsir_text = "Tafsir not available."
|
77 |
+
try:
|
78 |
+
tafsir_response = requests.get(tafsir_api)
|
79 |
+
if tafsir_response.status_code == 200:
|
80 |
+
tafsir_text = tafsir_response.json().get("result", {}).get("tafsir", "Tafsir not found.")
|
81 |
+
except Exception as e:
|
82 |
+
tafsir_text = "Could not fetch tafsir."
|
83 |
+
|
84 |
+
st.markdown("### 📘 Tafsir (Urdu)")
|
85 |
+
st.info(tafsir_text)
|
86 |
+
|
87 |
+
# 3. Dua (Mood-based, bilingual)
|
88 |
+
dua_map = {
|
89 |
+
"anxious": "اللهم اجعل قلبي مطمئنًا بذكرك\n**O Allah, make my heart tranquil with Your remembrance.**\n**یا اللہ! میرے دل کو اپنے ذکر سے اطمینان دے۔**",
|
90 |
+
"sad": "اللهم اجبر قلبي جبرا يتعجب له أهل السماوات والأرض\n**O Allah, mend my heart in a way that amazes the heavens and earth.**\n**یا اللہ! میرے دل کو ایسا سہارا دے جو زمین و آسمان کو حیران کر دے۔**",
|
91 |
+
"hopeless": "رب لا تذرني فردا وأنت خير الوارثين\n**My Lord, do not leave me alone — You are the Best Inheritor.**\n**یا رب! مجھے تنہا نہ چھوڑ، تو بہترین وارث ہے۔**",
|
92 |
+
"grateful": "اللهم اجعلني لك شَكُورًا\n**O Allah, make me deeply grateful to You.**\n**یا اللہ! مجھے شکر گزار بنا دے۔**",
|
93 |
+
"lonely": "اللهم كن معي حين لا يكون أحد بجانبي\n**O Allah, be with me when no one else is.**\n**یا اللہ! جب کوئی ساتھ نہ ہو، تو میرے ساتھ ہو۔**",
|
94 |
+
"angry": "اللهم ارزقني الحلم عند الغضب\n**O Allah, grant me forbearance when I’m angry.**\n**یا اللہ! غصے میں مجھے برداشت عطا فرما۔**",
|
95 |
+
"lost": "اللهم دلّني على صراطك المستقيم\n**O Allah, guide me to Your straight path.**\n**یا اللہ! مجھے اپنے سیدھے راستے پر چلا۔**",
|
96 |
+
"tired": "اللهم جدد طاقتي، وارزقني راحة البال\n**O Allah, renew my energy and grant me peace of mind.**\n**یا اللہ! میری طاقت کو تازہ کر اور مجھے ذہنی سکون عطا فرما۔**",
|
97 |
+
"afraid": "ربِّ أعني ولا تعن عليّ\n**My Lord, support me and not against me.**\n**میرے رب! میری مدد فرما، میرے خلاف نہ ہو۔**"
|
98 |
+
}
|
99 |
+
|
100 |
+
st.markdown("### 🤲 Dua (English + Urdu)")
|
101 |
+
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation."))
|
102 |
+
|
103 |
+
# 4. Reflections
|
104 |
+
st.markdown("### 📝 Your Reflection")
|
105 |
+
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
|
106 |
+
|
107 |
+
if st.button("💾 Save Reflection"):
|
108 |
+
reflections[selected_mood] = {
|
109 |
+
"text": reflection_input,
|
110 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
|
111 |
+
}
|
112 |
+
with open("reflections.json", "w") as f:
|
113 |
+
json.dump(reflections, f, indent=4)
|
114 |
+
st.success("Reflection saved.")
|
115 |
+
|
116 |
+
if selected_mood in reflections:
|
117 |
+
last = reflections[selected_mood]
|
118 |
+
st.markdown("#### 📜 Last Saved Reflection:")
|
119 |
+
st.markdown(f"**{last['timestamp']}** — {last['text']}")
|