Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import random
|
5 |
+
import requests
|
6 |
+
from datetime import datetime
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
# --------------------------
|
10 |
+
# ๐ Load environment vars
|
11 |
+
# --------------------------
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# --------------------------
|
15 |
+
# ๐ Create or load files
|
16 |
+
# --------------------------
|
17 |
+
|
18 |
+
# 1. moods.json
|
19 |
+
default_moods = {
|
20 |
+
"anxious": ["13:28", "2:286", "10:62"],
|
21 |
+
"sad": ["94:5", "93:6", "65:7"],
|
22 |
+
"hopeless": ["39:53", "12:87", "3:139"],
|
23 |
+
"grateful": ["14:7", "31:12"],
|
24 |
+
"lonely": ["2:186", "9:40"],
|
25 |
+
"angry": ["3:134", "41:34"],
|
26 |
+
"lost": ["93:7", "6:122"],
|
27 |
+
"tired": ["94:6", "3:200"],
|
28 |
+
"afraid": ["8:46", "33:3"]
|
29 |
+
}
|
30 |
+
|
31 |
+
if not os.path.exists("moods.json"):
|
32 |
+
with open("moods.json", "w") as f:
|
33 |
+
json.dump(default_moods, f, indent=4)
|
34 |
+
with open("moods.json", "r") as f:
|
35 |
+
moods = json.load(f)
|
36 |
+
|
37 |
+
# 2. reflections.json
|
38 |
+
if not os.path.exists("reflections.json"):
|
39 |
+
with open("reflections.json", "w") as f:
|
40 |
+
json.dump({}, f)
|
41 |
+
with open("reflections.json", "r") as f:
|
42 |
+
reflections = json.load(f)
|
43 |
+
|
44 |
+
# 3. .env โ already handled by `load_dotenv()` above
|
45 |
+
if not os.path.exists(".env"):
|
46 |
+
st.warning("โ ๏ธ .env file not found. Make sure to set secrets if using Firebase or APIs.")
|
47 |
+
|
48 |
+
# 4. firebase_config.json โ check only (optional)
|
49 |
+
use_firebase = os.getenv("USE_FIREBASE", "false").lower() == "true"
|
50 |
+
firebase_available = os.path.exists("firebase_config.json")
|
51 |
+
|
52 |
+
if use_firebase and not firebase_available:
|
53 |
+
st.error("Firebase is enabled in .env but firebase_config.json is missing!")
|
54 |
+
|
55 |
+
# Optional: Firebase setup block
|
56 |
+
if use_firebase and firebase_available:
|
57 |
+
try:
|
58 |
+
import firebase_admin
|
59 |
+
from firebase_admin import credentials, db
|
60 |
+
|
61 |
+
if not firebase_admin._apps:
|
62 |
+
cred = credentials.Certificate("firebase_config.json")
|
63 |
+
firebase_admin.initialize_app(cred, {
|
64 |
+
'databaseURL': os.getenv("FIREBASE_DB_URL")
|
65 |
+
})
|
66 |
+
st.success("โ
Firebase initialized.")
|
67 |
+
except Exception as e:
|
68 |
+
st.error(f"Firebase error: {e}")
|
69 |
+
|
70 |
+
# --------------------------
|
71 |
+
# ๐ Streamlit UI
|
72 |
+
# --------------------------
|
73 |
+
st.set_page_config(page_title="Kalam Comfort", page_icon="๐")
|
74 |
+
st.title("๐ Kalam Comfort")
|
75 |
+
st.subheader("Find Quranic comfort by emotion ๐")
|
76 |
+
|
77 |
+
selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0)
|
78 |
+
|
79 |
+
# Refresh feature
|
80 |
+
if st.button("๐ Show another verse"):
|
81 |
+
st.experimental_rerun()
|
82 |
+
|
83 |
+
if selected_mood:
|
84 |
+
verse_list = moods[selected_mood]
|
85 |
+
verse_ref = random.choice(verse_list)
|
86 |
+
surah_num, ayah_num = verse_ref.split(":")
|
87 |
+
|
88 |
+
# Fetch verse from API
|
89 |
+
quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad"
|
90 |
+
response = requests.get(quran_api)
|
91 |
+
|
92 |
+
if response.status_code == 200:
|
93 |
+
data = response.json()['data']
|
94 |
+
arabic = data[0]['text']
|
95 |
+
english = data[1]['text']
|
96 |
+
surah_info = f"{data[0]['surah']['englishName']} ({data[0]['surah']['name']})"
|
97 |
+
|
98 |
+
st.markdown("### ๐ Quranic Verse")
|
99 |
+
st.markdown(f"**Surah:** {surah_info} โ Ayah {ayah_num}")
|
100 |
+
st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
101 |
+
st.markdown(f"*{english}*")
|
102 |
+
|
103 |
+
# Tafsir and Dua mappings
|
104 |
+
tafsir_map = {
|
105 |
+
"anxious": "The heart finds true peace in the remembrance of Allah.",
|
106 |
+
"sad": "Ease always follows hardship โ hold on to hope.",
|
107 |
+
"hopeless": "Allahโs mercy is vast. Never give up.",
|
108 |
+
"grateful": "Gratitude brings increase โ in peace, health, and blessings.",
|
109 |
+
"lonely": "Allah is always near. Call upon Him.",
|
110 |
+
"angry": "Restrain your anger and be among the righteous.",
|
111 |
+
"lost": "He guided you when you were unaware.",
|
112 |
+
"tired": "After difficulty comes ease โ stay patient.",
|
113 |
+
"afraid": "Allah never burdens a soul more than it can bear."
|
114 |
+
}
|
115 |
+
|
116 |
+
dua_map = {
|
117 |
+
"anxious": "ุงูููู
ุงุฌุนู ููุจู ู
ุทู
ุฆููุง ุจุฐูุฑู\n*O Allah, make my heart tranquil with Your remembrance.*",
|
118 |
+
"sad": "ุงูููู
ุงุฌุจุฑ ููุจู ุฌุจุฑุง ูุชุนุฌุจ ูู ุฃูู ุงูุณู
ุงูุงุช ูุงูุฃุฑุถ\n*O Allah, mend my heart in a way that amazes the heavens and earth.*",
|
119 |
+
"hopeless": "ุฑุจ ูุง ุชุฐุฑูู ูุฑุฏุง ูุฃูุช ุฎูุฑ ุงููุงุฑุซูู\n*My Lord, do not leave me alone โ You are the Best Inheritor.*",
|
120 |
+
"grateful": "ุงูููู
ุงุฌุนููู ูู ุดููููุฑูุง\n*O Allah, make me deeply grateful to You.*",
|
121 |
+
"lonely": "ุงูููู
ูู ู
ุนู ุญูู ูุง ูููู ุฃุญุฏ ุจุฌุงูุจู\n*O Allah, be with me when no one else is.*",
|
122 |
+
"angry": "ุงูููู
ุงุฑุฒููู ุงูุญูู
ุนูุฏ ุงูุบุถุจ\n*O Allah, grant me forbearance when Iโm angry.*",
|
123 |
+
"lost": "ุงูููู
ุฏูููู ุนูู ุตุฑุงุทู ุงูู
ุณุชููู
\n*O Allah, guide me to Your straight path.*",
|
124 |
+
"tired": "ุงูููู
ุฌุฏุฏ ุทุงูุชูุ ูุงุฑุฒููู ุฑุงุญุฉ ุงูุจุงู\n*O Allah, renew my energy and grant me peace of mind.*",
|
125 |
+
"afraid": "ุฑุจูู ุฃุนูู ููุง ุชุนู ุนููู\n*My Lord, support me and not against me.*"
|
126 |
+
}
|
127 |
+
|
128 |
+
st.markdown("### โจ Tafsir")
|
129 |
+
st.info(tafsir_map.get(selected_mood, "Let the verse speak to your heart."))
|
130 |
+
|
131 |
+
st.markdown("### ๐คฒ Dua")
|
132 |
+
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation."))
|
133 |
+
|
134 |
+
# ๐ User Reflection
|
135 |
+
st.markdown("### ๐ Your Reflection")
|
136 |
+
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
|
137 |
+
|
138 |
+
if st.button("๐พ Save Reflection"):
|
139 |
+
reflections[selected_mood] = {
|
140 |
+
"text": reflection_input,
|
141 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M")
|
142 |
+
}
|
143 |
+
with open("reflections.json", "w") as f:
|
144 |
+
json.dump(reflections, f, indent=4)
|
145 |
+
st.success("Reflection saved.")
|
146 |
+
|
147 |
+
# Show existing reflection
|
148 |
+
if selected_mood in reflections:
|
149 |
+
st.markdown("#### ๐ Last Saved Reflection:")
|
150 |
+
last = reflections[selected_mood]
|
151 |
+
st.markdown(f"**{last['timestamp']}** โ {last['text']}")
|
152 |
+
else:
|
153 |
+
st.error("โ Could not fetch verse. Check internet or try again.")
|