Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,11 @@ import requests
|
|
6 |
from datetime import datetime
|
7 |
from dotenv import load_dotenv
|
8 |
|
|
|
|
|
|
|
9 |
# --------------------------
|
10 |
-
# π Load environment
|
11 |
# --------------------------
|
12 |
load_dotenv()
|
13 |
|
@@ -41,42 +44,18 @@ if not os.path.exists("reflections.json"):
|
|
41 |
with open("reflections.json", "r") as f:
|
42 |
reflections = json.load(f)
|
43 |
|
44 |
-
# 3.
|
45 |
if not os.path.exists(".env"):
|
46 |
-
st.warning("β οΈ .env file not found.
|
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 |
|
@@ -85,7 +64,6 @@ if 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 |
|
@@ -100,7 +78,7 @@ if selected_mood:
|
|
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
|
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.",
|
@@ -131,7 +109,7 @@ if selected_mood:
|
|
131 |
st.markdown("### π€² Dua")
|
132 |
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation."))
|
133 |
|
134 |
-
#
|
135 |
st.markdown("### π Your Reflection")
|
136 |
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
|
137 |
|
@@ -144,10 +122,9 @@ if selected_mood:
|
|
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.")
|
|
|
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 |
|
|
|
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 you're adding secrets later.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.experimental_rerun()
|
61 |
|
|
|
64 |
verse_ref = random.choice(verse_list)
|
65 |
surah_num, ayah_num = verse_ref.split(":")
|
66 |
|
|
|
67 |
quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad"
|
68 |
response = requests.get(quran_api)
|
69 |
|
|
|
78 |
st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True)
|
79 |
st.markdown(f"*{english}*")
|
80 |
|
81 |
+
# Tafsir and Dua
|
82 |
tafsir_map = {
|
83 |
"anxious": "The heart finds true peace in the remembrance of Allah.",
|
84 |
"sad": "Ease always follows hardship β hold on to hope.",
|
|
|
109 |
st.markdown("### π€² Dua")
|
110 |
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation."))
|
111 |
|
112 |
+
# Reflection Input
|
113 |
st.markdown("### π Your Reflection")
|
114 |
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?")
|
115 |
|
|
|
122 |
json.dump(reflections, f, indent=4)
|
123 |
st.success("Reflection saved.")
|
124 |
|
|
|
125 |
if selected_mood in reflections:
|
|
|
126 |
last = reflections[selected_mood]
|
127 |
+
st.markdown("#### π Last Saved Reflection:")
|
128 |
st.markdown(f"**{last['timestamp']}** β {last['text']}")
|
129 |
else:
|
130 |
st.error("β Could not fetch verse. Check internet or try again.")
|