ashhal commited on
Commit
f9ed339
·
verified ·
1 Parent(s): 94bdf58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -76
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
- # --- Streamlit Config ---
10
- st.set_page_config(page_title="Kalam Comfort", page_icon="📚")
11
- load_dotenv()
12
-
13
- # --- Default Mappings ---
14
- default_moods = {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": ["16:18", "28:70", "55:13"]
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
- # --- Initial File Setup ---
135
- if not os.path.exists("moods.json"):
136
- with open("moods.json", "w") as f:
137
- json.dump(default_moods, f, indent=4)
138
- if not os.path.exists("reflections.json"):
139
- with open("reflections.json", "w") as f:
140
- json.dump({}, f, indent=4)
141
-
142
- with open("moods.json") as f:
143
- moods = json.load(f)
144
- with open("reflections.json") as f:
145
- reflections = json.load(f)
146
-
147
- # --- UI ---
148
- st.title("📚 Kalam Comfort")
149
- st.subheader("Guidance for how you feel 🤍")
150
-
151
- selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
152
-
153
- if selected_mood:
154
- verse_ref = random.choice(moods[selected_mood])
155
- surah, ayah = verse_ref.split(":")
156
- url = f"https://api.alquran.cloud/v1/ayah/{surah}:{ayah}/editions/quran-simple,en.asad,ur.jalandhry"
157
- response = requests.get(url)
158
-
159
- if response.status_code == 200:
160
- data = response.json()["data"]
161
- st.markdown("### 🌙 Quranic Verse")
162
- st.markdown(f"**Surah:** {data[0]['surah']['englishName']} — Ayah {ayah}")
163
- st.markdown(f"<div style='font-size:24px; direction: rtl'>{data[0]['text']}</div>", unsafe_allow_html=True)
164
- st.markdown(f"**📖 English:** *{data[1]['text']}*")
165
- st.markdown(f"**📖 Urdu:** {data[2]['text']}")
166
- else:
167
- st.error("Failed to fetch Quranic verse.")
168
-
169
- st.markdown("### 🤲 Dua")
170
- dua = random.choice(default_duas[selected_mood])
171
- st.markdown(f"**Arabic:** {dua[0]}")
172
- st.markdown(f"**English:** *{dua[1]}*")
173
- st.markdown(f"**Urdu:** {dua[2]}")
174
-
175
- if selected_mood in default_hadiths:
176
- hadith = random.choice(default_hadiths[selected_mood])
 
 
 
 
 
 
 
 
177
  st.markdown("### 📜 Hadith")
178
- st.markdown(f"**Arabic:** {hadith[0]}")
179
- st.markdown(f"**English:** *{hadith[1]}*")
180
- st.markdown(f"**Urdu:** {hadith[2]}")
181
-
182
- st.markdown("### 📝 Reflection")
183
- reflection = st.text_area("Write your reflection:")
184
- if st.button("💾 Save Reflection"):
185
- reflections[selected_mood] = {
186
- "text": reflection,
187
- "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
188
- }
189
- with open("reflections.json", "w") as f:
190
- json.dump(reflections, f, indent=4)
191
- st.success("Reflection saved.")
192
-
193
- if selected_mood in reflections:
194
- last = reflections[selected_mood]
195
- st.markdown("#### 📄 Last Reflection")
196
- st.markdown(f"**{last['timestamp']}** — {last['text']}")
197
-
198
- if st.button("🔁 Show another"):
199
- st.rerun()
 
 
 
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']}")