Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,14 +4,31 @@ import json
|
|
4 |
import random
|
5 |
import requests
|
6 |
from datetime import datetime
|
7 |
-
from dotenv import load_dotenv
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
"anxious": ["13:28", "2:286", "10:62"],
|
16 |
"sad": ["94:5", "93:6", "65:7"],
|
17 |
"hopeless": ["39:53", "12:87", "3:139"],
|
@@ -21,8 +38,10 @@ default_moods = {
|
|
21 |
"lost": ["93:7", "6:122"],
|
22 |
"tired": ["94:6", "3:200"],
|
23 |
"afraid": ["8:46", "33:3"],
|
24 |
-
"happy": ["
|
25 |
-
}
|
|
|
|
|
26 |
|
27 |
default_duas = {
|
28 |
"anxious": [
|
@@ -129,71 +148,81 @@ default_hadiths = {
|
|
129 |
("إِنَّ اللَّهَ يُحِبُّ الْفَرَحِينَ بِطَاعَتِهِ", "Indeed, Allah loves those who rejoice in His obedience.", "بیشک اللہ ان لوگوں سے محبت کرتا ہے جو اس کی اطاعت میں خوش ہوتے ہیں۔")
|
130 |
]
|
131 |
}
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
st.
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
st.markdown("### 📜 Hadith")
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
4 |
import random
|
5 |
import requests
|
6 |
from datetime import datetime
|
|
|
7 |
|
8 |
+
st.set_page_config(page_title="Kalam Comfort", page_icon="📚", layout="centered")
|
9 |
+
|
10 |
+
# Hide Streamlit UI elements
|
11 |
+
st.markdown("""
|
12 |
+
<style>
|
13 |
+
#MainMenu, footer {visibility: hidden;}
|
14 |
+
button {font-size: 16px !important;}
|
15 |
+
</style>
|
16 |
+
""", unsafe_allow_html=True)
|
17 |
+
|
18 |
+
# Session state to manage page switching
|
19 |
+
if "page" not in st.session_state:
|
20 |
+
st.session_state.page = "welcome"
|
21 |
+
|
22 |
+
# Load or create required files
|
23 |
+
def load_or_create_json(filename, default_data):
|
24 |
+
if not os.path.exists(filename):
|
25 |
+
with open(filename, "w") as f:
|
26 |
+
json.dump(default_data, f, indent=4, ensure_ascii=False)
|
27 |
+
with open(filename, "r") as f:
|
28 |
+
return json.load(f)
|
29 |
+
|
30 |
+
# Mood-to-ayah mapping
|
31 |
+
moods = load_or_create_json("moods.json", {
|
32 |
"anxious": ["13:28", "2:286", "10:62"],
|
33 |
"sad": ["94:5", "93:6", "65:7"],
|
34 |
"hopeless": ["39:53", "12:87", "3:139"],
|
|
|
38 |
"lost": ["93:7", "6:122"],
|
39 |
"tired": ["94:6", "3:200"],
|
40 |
"afraid": ["8:46", "33:3"],
|
41 |
+
"happy": ["10:58", "28:70", "55:13"]
|
42 |
+
})
|
43 |
+
|
44 |
+
reflections = load_or_create_json("reflections.json", {})
|
45 |
|
46 |
default_duas = {
|
47 |
"anxious": [
|
|
|
148 |
("إِنَّ اللَّهَ يُحِبُّ الْفَرَحِينَ بِطَاعَتِهِ", "Indeed, Allah loves those who rejoice in His obedience.", "بیشک اللہ ان لوگوں سے محبت کرتا ہے جو اس کی اطاعت میں خوش ہوتے ہیں۔")
|
149 |
]
|
150 |
}
|
151 |
+
# ---------------- Page 1: Welcome ----------------
|
152 |
+
if st.session_state.page == "welcome":
|
153 |
+
st.title("📚 Kalam Comfort")
|
154 |
+
st.subheader("Let the Qur'an speak to your heart 🤍")
|
155 |
+
st.markdown("""
|
156 |
+
<div style='text-align:center; margin-top: 30px;'>
|
157 |
+
<img src='https://i.ibb.co/0n7MpKx/quran-light.png' width='200'>
|
158 |
+
</div>
|
159 |
+
""", unsafe_allow_html=True)
|
160 |
+
if st.button("✨ Let's Dive In"):
|
161 |
+
st.session_state.page = "main"
|
162 |
+
st.experimental_rerun()
|
163 |
+
|
164 |
+
# ---------------- Page 2: Main App ----------------
|
165 |
+
if st.session_state.page == "main":
|
166 |
+
st.title("📖 Kalam Comfort")
|
167 |
+
st.subheader("Select your mood to receive comfort from the Qur'an")
|
168 |
+
|
169 |
+
selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
|
170 |
+
|
171 |
+
if selected_mood:
|
172 |
+
verse_ref = random.choice(moods[selected_mood])
|
173 |
+
surah_num, ayah_num = verse_ref.split(":")
|
174 |
+
|
175 |
+
# Get Quranic text
|
176 |
+
quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad,ur.jalandhry"
|
177 |
+
response = requests.get(quran_api)
|
178 |
+
|
179 |
+
if response.status_code == 200:
|
180 |
+
data = response.json()["data"]
|
181 |
+
arabic = data[0]["text"]
|
182 |
+
english = data[1]["text"]
|
183 |
+
urdu = data[2]["text"]
|
184 |
+
surah_info = f"{data[0]['surah']['englishName']} ({data[0]['surah']['name']})"
|
185 |
+
|
186 |
+
st.markdown("### 🌙 Quranic Verse")
|
187 |
+
st.markdown(f"**Surah:** {surah_info} — Ayah {ayah_num}")
|
188 |
+
st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
189 |
+
st.markdown(f"**📖 English:** *{english}*")
|
190 |
+
st.markdown(f"**📖 Urdu:** {urdu}")
|
191 |
+
else:
|
192 |
+
st.error("❌ Could not fetch verse.")
|
193 |
+
|
194 |
+
# Dua Section
|
195 |
+
st.markdown("### 🤲 Duas")
|
196 |
+
duas = random.sample(default_duas.get(selected_mood, []), k=min(2, len(default_duas.get(selected_mood, []))))
|
197 |
+
for arabic, eng, ur in duas:
|
198 |
+
st.markdown(f"<div style='font-size:22px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
199 |
+
st.markdown(f"**English:** {eng}")
|
200 |
+
st.markdown(f"**Urdu:** {ur}")
|
201 |
+
st.markdown("---")
|
202 |
+
|
203 |
+
# Hadith Section
|
204 |
st.markdown("### 📜 Hadith")
|
205 |
+
mood_hadiths = default_hadiths.get(selected_mood, [])
|
206 |
+
for arabic, eng, ur, src in mood_hadiths:
|
207 |
+
st.markdown(f"<div style='font-size:22px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
208 |
+
st.markdown(f"**English:** {eng}")
|
209 |
+
st.markdown(f"**Urdu:** {ur}")
|
210 |
+
st.markdown(f"_Source: {src}_")
|
211 |
+
st.markdown("---")
|
212 |
+
|
213 |
+
# Reflection Section
|
214 |
+
st.markdown("### 📝 Your Reflection")
|
215 |
+
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
|
216 |
+
if st.button("💾 Save Reflection"):
|
217 |
+
reflections[selected_mood] = {
|
218 |
+
"text": reflection_input,
|
219 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
|
220 |
+
}
|
221 |
+
with open("reflections.json", "w") as f:
|
222 |
+
json.dump(reflections, f, indent=4, ensure_ascii=False)
|
223 |
+
st.success("Reflection saved.")
|
224 |
+
|
225 |
+
if selected_mood in reflections:
|
226 |
+
last = reflections[selected_mood]
|
227 |
+
st.markdown("#### 📜 Last Saved Reflection:")
|
228 |
+
st.markdown(f"**{last['timestamp']}** — {last['text']}")
|