ashhal commited on
Commit
ac6f9e3
Β·
verified Β·
1 Parent(s): 7152de4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -58
app.py CHANGED
@@ -171,67 +171,85 @@ if st.session_state.page == "welcome":
171
  st.session_state.page = "main"
172
  st.rerun()
173
 
174
- # ---------------- Page 2: Main App ----------------
175
  if st.session_state.page == "main":
176
  st.title("πŸ“– Kalam Comfort")
177
  st.subheader("Select your mood to receive comfort from the Qur'an")
178
 
179
  selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
180
 
181
- if selected_mood:
182
- verse_ref = random.choice(moods[selected_mood])
183
- surah_num, ayah_num = verse_ref.split(":")
184
-
185
- # Get Quranic text
186
- quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad,ur.jalandhry"
187
- response = requests.get(quran_api)
188
-
189
- if response.status_code == 200:
190
- data = response.json()["data"]
191
- arabic = data[0]["text"]
192
- english = data[1]["text"]
193
- urdu = data[2]["text"]
194
- surah_info = f"{data[0]['surah']['englishName']} ({data[0]['surah']['name']})"
195
-
196
- st.markdown("### πŸŒ™ Quranic Verse")
197
- st.markdown(f"**Surah:** {surah_info} β€” Ayah {ayah_num}")
198
- st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
199
- st.markdown(f"**πŸ“– English:** *{english}*")
200
- st.markdown(f"**πŸ“– Urdu:** {urdu}")
201
- else:
202
- st.error("❌ Could not fetch verse.")
203
-
204
- # Duas
205
- st.markdown("### 🀲 Duas")
206
- duas = random.sample(default_duas.get(selected_mood, []), k=min(2, len(default_duas.get(selected_mood, []))))
207
- for arabic, eng, ur in duas:
208
- st.markdown(f"<div style='font-size:22px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
209
- st.markdown(f"**English:** {eng}")
210
- st.markdown(f"**Urdu:** {ur}")
211
- st.markdown("---")
212
-
213
- # Hadiths
214
- st.markdown("### πŸ“œ Hadith")
215
- hadiths = random.sample(default_hadiths.get(selected_mood, []), k=min(2, len(default_hadiths.get(selected_mood, []))))
216
- for arabic, eng, ur in hadiths:
217
- st.markdown(f"<div style='font-size:22px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
218
- st.markdown(f"**English:** {eng}")
219
- st.markdown(f"**Urdu:** {ur}")
220
- st.markdown("---")
221
-
222
- # Reflection
223
- st.markdown("### πŸ“ Your Reflection")
224
- reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
225
- if st.button("πŸ’Ύ Save Reflection"):
226
- reflections[selected_mood] = {
227
- "text": reflection_input,
228
- "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
229
- }
230
- with open("reflections.json", "w") as f:
231
- json.dump(reflections, f, indent=4, ensure_ascii=False)
232
- st.success("Reflection saved.")
233
-
234
- if selected_mood in reflections:
235
- last = reflections[selected_mood]
236
- st.markdown("#### πŸ“œ Last Saved Reflection:")
237
- st.markdown(f"**{last['timestamp']}** β€” {last['text']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  st.session_state.page = "main"
172
  st.rerun()
173
 
174
+ #---------------- Page 2: Main App ----------------
175
  if st.session_state.page == "main":
176
  st.title("πŸ“– Kalam Comfort")
177
  st.subheader("Select your mood to receive comfort from the Qur'an")
178
 
179
  selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
180
 
181
+ # Reset index on mood change
182
+ if "current_mood" not in st.session_state:
183
+ st.session_state.current_mood = ""
184
+ if selected_mood != st.session_state.current_mood:
185
+ st.session_state.current_mood = selected_mood
186
+ st.session_state.response_index = 0
187
+
188
+ # Show Another Response button
189
+ if st.button("πŸ” Show Another Response"):
190
+ st.session_state.response_index += 1
191
+ st.rerun()
192
+
193
+ index = st.session_state.get("response_index", 0)
194
+
195
+ # Quranic Verse
196
+ verse_list = moods[selected_mood]
197
+ verse_ref = verse_list[index % len(verse_list)]
198
+ surah_num, ayah_num = verse_ref.split(":")
199
+
200
+ quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad,ur.jalandhry"
201
+ response = requests.get(quran_api)
202
+
203
+ if response.status_code == 200:
204
+ data = response.json()["data"]
205
+ arabic = data[0]["text"]
206
+ english = data[1]["text"]
207
+ urdu = data[2]["text"]
208
+ surah_info = f"{data[0]['surah']['englishName']} ({data[0]['surah']['name']})"
209
+
210
+ st.markdown("### πŸŒ™ Quranic Verse")
211
+ st.markdown(f"**Surah:** {surah_info} β€” Ayah {ayah_num}")
212
+ st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
213
+ st.markdown(f"**πŸ“– English:** *{english}*")
214
+ st.markdown(f"**πŸ“– Urdu:** {urdu}")
215
+ else:
216
+ st.error("❌ Could not fetch verse.")
217
+
218
+ # Dua
219
+ st.markdown("### 🀲 Dua")
220
+ duas = default_duas.get(selected_mood, [])
221
+ if duas:
222
+ dua = duas[index % len(duas)]
223
+ st.markdown(f"<div style='font-size:22px; direction: rtl'>{dua[0]}</div>", unsafe_allow_html=True)
224
+ st.markdown(f"**English:** {dua[1]}")
225
+ st.markdown(f"**Urdu:** {dua[2]}")
226
+ else:
227
+ st.warning("No duas available for this mood.")
228
+
229
+ # Hadith
230
+ st.markdown("### πŸ“œ Hadith")
231
+ hadiths = default_hadiths.get(selected_mood, [])
232
+ if hadiths:
233
+ hadith = hadiths[index % len(hadiths)]
234
+ st.markdown(f"<div style='font-size:22px; direction: rtl'>{hadith[0]}</div>", unsafe_allow_html=True)
235
+ st.markdown(f"**English:** {hadith[1]}")
236
+ st.markdown(f"**Urdu:** {hadith[2]}")
237
+ else:
238
+ st.warning("No hadiths available for this mood.")
239
+
240
+ # Reflection Section
241
+ st.markdown("### πŸ“ Your Reflection")
242
+ reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
243
+ if st.button("πŸ’Ύ Save Reflection"):
244
+ reflections[selected_mood] = {
245
+ "text": reflection_input,
246
+ "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
247
+ }
248
+ with open("reflections.json", "w") as f:
249
+ json.dump(reflections, f, indent=4, ensure_ascii=False)
250
+ st.success("Reflection saved.")
251
+
252
+ if selected_mood in reflections:
253
+ last = reflections[selected_mood]
254
+ st.markdown("#### πŸ“œ Last Saved Reflection:")
255
+ st.markdown(f"**{last['timestamp']}** β€” {last['text']}")