Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,236 +1,146 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
| 3 |
-
from deepface import DeepFace
|
| 4 |
from PIL import Image
|
| 5 |
-
from
|
| 6 |
import tempfile
|
| 7 |
import json
|
| 8 |
-
import os
|
| 9 |
import datetime
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# =====
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
layout="centered",
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 20 |
-
if not OPENAI_API_KEY:
|
| 21 |
-
st.error("โ OPENAI_API_KEY not found! Please add it to your environment variables.")
|
| 22 |
-
st.stop()
|
| 23 |
-
|
| 24 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
)
|
| 139 |
-
st.subheader("๐ Suggested Qur'anic Ayah")
|
| 140 |
-
st.write(response.choices[0].message.content)
|
| 141 |
-
|
| 142 |
-
except Exception as e:
|
| 143 |
-
st.error(f"โ Error: {e}")
|
| 144 |
-
|
| 145 |
-
# ============================================================
|
| 146 |
-
# โ TAB 3: Ask the Qurโan
|
| 147 |
-
# ============================================================
|
| 148 |
-
with tabs[3]:
|
| 149 |
-
st.header("โ Ask the Qurโan")
|
| 150 |
-
topic = st.text_input("Ask your question:", placeholder="What does the Qurโan say about ____?")
|
| 151 |
-
|
| 152 |
-
if st.button("Get Qurโanic Guidance", key="askquran"):
|
| 153 |
-
if topic.strip():
|
| 154 |
-
prompt = f"""
|
| 155 |
-
Provide a Qur'anic Ayah in Arabic related to: {topic}.
|
| 156 |
-
Also provide:
|
| 157 |
-
1) The English translation.
|
| 158 |
-
2) A short tafsir/explanation.
|
| 159 |
-
Keep it clear and authentic.
|
| 160 |
-
"""
|
| 161 |
-
try:
|
| 162 |
-
response = client.chat.completions.create(
|
| 163 |
-
model="gpt-4o-mini",
|
| 164 |
-
messages=[{"role": "user", "content": prompt}]
|
| 165 |
-
)
|
| 166 |
-
st.subheader("๐ Qurโanic Answer")
|
| 167 |
-
st.write(response.choices[0].message.content)
|
| 168 |
-
except Exception as e:
|
| 169 |
-
st.error(f"โ Error: {e}")
|
| 170 |
-
else:
|
| 171 |
-
st.warning("Please type your question.")
|
| 172 |
-
|
| 173 |
-
# ============================================================
|
| 174 |
-
# ๐ฑ TAB 4: Soul Healing Tracker
|
| 175 |
-
# ============================================================
|
| 176 |
-
with tabs[4]:
|
| 177 |
-
st.header("๐ฑ Healing Tracker")
|
| 178 |
-
emotion = st.selectbox(
|
| 179 |
-
"Which emotion/challenge are you working on?",
|
| 180 |
-
["Anxiety", "Sadness", "Anger", "Hopelessness", "Loneliness"]
|
| 181 |
-
)
|
| 182 |
-
|
| 183 |
-
goal = st.text_input("Set your spiritual goal (e.g., Recite Surah Duha every morning for 7 days):")
|
| 184 |
-
|
| 185 |
-
days = st.slider("How many days do you want to track?", 1, 30, 7)
|
| 186 |
-
|
| 187 |
-
if "progress" not in st.session_state:
|
| 188 |
-
st.session_state.progress = [False] * days
|
| 189 |
-
|
| 190 |
-
st.subheader("Daily Check-In")
|
| 191 |
-
for i in range(days):
|
| 192 |
-
done = st.checkbox(f"Day {i+1} completed?", value=st.session_state.progress[i], key=f"day{i}")
|
| 193 |
-
st.session_state.progress[i] = done
|
| 194 |
-
|
| 195 |
-
completed = sum(st.session_state.progress)
|
| 196 |
-
percent = int((completed / days) * 100)
|
| 197 |
-
st.progress(percent)
|
| 198 |
-
st.write(f"โ
Youโve completed {completed} of {days} days. ({percent}%)")
|
| 199 |
-
|
| 200 |
-
if percent == 100:
|
| 201 |
-
st.success("MashaAllah! Youโve achieved your goal. Keep going!")
|
| 202 |
-
elif percent >= 50:
|
| 203 |
-
st.info("Great work! Youโre more than halfway there.")
|
| 204 |
-
elif percent > 0:
|
| 205 |
-
st.write("Good start! Keep it consistent.")
|
| 206 |
-
|
| 207 |
-
# ============================================================
|
| 208 |
-
# ๐ TAB 5: Masjid Finder
|
| 209 |
-
# ============================================================
|
| 210 |
-
with tabs[5]:
|
| 211 |
-
st.header("๐ Find Nearby Masajid")
|
| 212 |
-
|
| 213 |
-
try:
|
| 214 |
-
with open("masjid_data.json", "r", encoding="utf-8") as f:
|
| 215 |
-
masjid_data = json.load(f)
|
| 216 |
-
except Exception as e:
|
| 217 |
-
st.error(f"Could not load masjid data: {e}")
|
| 218 |
-
st.stop()
|
| 219 |
-
|
| 220 |
-
city = st.selectbox("Select your city:", list(masjid_data.keys()))
|
| 221 |
-
area_input = st.text_input("Enter your area name (e.g., 'Model Town')")
|
| 222 |
-
|
| 223 |
-
if st.button("Find Masajid"):
|
| 224 |
-
if area_input.strip():
|
| 225 |
-
areas = masjid_data[city]
|
| 226 |
-
best_match, score = process.extractOne(area_input.lower(), areas.keys())
|
| 227 |
-
|
| 228 |
-
if score >= 70:
|
| 229 |
-
st.success(f"๐ Masajid near *{best_match.title()}* in *{city.title()}*:")
|
| 230 |
-
for name in areas[best_match]:
|
| 231 |
-
st.markdown(f"- {name}")
|
| 232 |
-
else:
|
| 233 |
-
st.warning("โ ๏ธ No close match found. Try different spelling.")
|
| 234 |
-
else:
|
| 235 |
-
st.info("Please enter an area name.")
|
| 236 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from openai import OpenAI
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
from deepface import DeepFace
|
| 5 |
import tempfile
|
| 6 |
import json
|
|
|
|
| 7 |
import datetime
|
| 8 |
+
from fuzzywuzzy import process
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# ======================
|
| 12 |
+
# CONFIG
|
| 13 |
+
# ======================
|
| 14 |
+
st.set_page_config(page_title="Shaafee - The App that Helps You Heal", layout="centered")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Load OpenAI key from env
|
| 17 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 19 |
|
| 20 |
+
# Load masjid data
|
| 21 |
+
with open("masjid_data.json", "r", encoding="utf-8") as f:
|
| 22 |
+
masjid_data = json.load(f)
|
| 23 |
+
|
| 24 |
+
# Init session state
|
| 25 |
+
if "page" not in st.session_state:
|
| 26 |
+
st.session_state.page = "home"
|
| 27 |
+
|
| 28 |
+
# ======================
|
| 29 |
+
# LOAD CUSTOM CSS
|
| 30 |
+
# ======================
|
| 31 |
+
def local_css(file_name):
|
| 32 |
+
with open(file_name) as f:
|
| 33 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
local_css("style.css")
|
| 36 |
+
|
| 37 |
+
# ======================
|
| 38 |
+
# HOME PAGE
|
| 39 |
+
# ======================
|
| 40 |
+
if st.session_state.page == "home":
|
| 41 |
+
st.markdown("""
|
| 42 |
+
<div class="hero">
|
| 43 |
+
<h1>The App that helps you heal</h1>
|
| 44 |
+
<p>Guiding you through stress, sadness and anxiety with the wisdom of the Qurโan</p>
|
| 45 |
+
<button onclick="window.location.href='?page=menu'">Browse App</button>
|
| 46 |
+
</div>
|
| 47 |
+
""", unsafe_allow_html=True)
|
| 48 |
+
|
| 49 |
+
if st.query_params.get("page") == "menu":
|
| 50 |
+
st.session_state.page = "menu"
|
| 51 |
+
|
| 52 |
+
# ======================
|
| 53 |
+
# MENU PAGE
|
| 54 |
+
# ======================
|
| 55 |
+
elif st.session_state.page == "menu":
|
| 56 |
+
st.markdown("""
|
| 57 |
+
<div class="menu">
|
| 58 |
+
<div class="card" onclick="window.location.href='?page=mood'">๐ธ Mood Detection</div>
|
| 59 |
+
<div class="card" onclick="window.location.href='?page=voice'">๐๏ธ Voice Journal</div>
|
| 60 |
+
<div class="card" onclick="window.location.href='?page=search'">โ Qurโan Search</div>
|
| 61 |
+
<div class="card" onclick="window.location.href='?page=tracker'">๐ฑ Progress Tracker</div>
|
| 62 |
+
<div class="card" onclick="window.location.href='?page=masjid'">๐ Masjid Finder</div>
|
| 63 |
+
</div>
|
| 64 |
+
""", unsafe_allow_html=True)
|
| 65 |
+
|
| 66 |
+
page = st.query_params.get("page")
|
| 67 |
+
if page:
|
| 68 |
+
st.session_state.page = page
|
| 69 |
+
|
| 70 |
+
# ======================
|
| 71 |
+
# MOOD DETECTION
|
| 72 |
+
# ======================
|
| 73 |
+
elif st.session_state.page == "mood":
|
| 74 |
+
st.title("๐ธ Mood Detection")
|
| 75 |
+
uploaded_file = st.file_uploader("Upload your selfie:", type=["jpg", "jpeg", "png"])
|
| 76 |
+
if uploaded_file:
|
| 77 |
+
img = Image.open(uploaded_file).convert("RGB")
|
| 78 |
+
st.image(img)
|
| 79 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 80 |
+
img.save(tmp_file.name)
|
| 81 |
+
image_path = tmp_file.name
|
| 82 |
+
|
| 83 |
+
result = DeepFace.analyze(img_path=image_path, actions=["emotion"], enforce_detection=False)
|
| 84 |
+
emotion = result["dominant_emotion"]
|
| 85 |
+
st.success(f"Detected Emotion: {emotion}")
|
| 86 |
+
|
| 87 |
+
if st.button("Get Ayah"):
|
| 88 |
+
prompt = f"Suggest a Quranic Ayah for someone feeling {emotion}."
|
| 89 |
+
response = client.chat.completions.create(
|
| 90 |
+
model="gpt-4o-mini",
|
| 91 |
+
messages=[{"role": "user", "content": prompt}]
|
| 92 |
+
)
|
| 93 |
+
st.write(response.choices[0].message.content)
|
| 94 |
+
|
| 95 |
+
# ======================
|
| 96 |
+
# VOICE JOURNAL
|
| 97 |
+
# ======================
|
| 98 |
+
elif st.session_state.page == "voice":
|
| 99 |
+
st.title("๐๏ธ Voice Journal")
|
| 100 |
+
st.info("Coming soon InshaAllah!")
|
| 101 |
+
|
| 102 |
+
# ======================
|
| 103 |
+
# QURAN SEARCH
|
| 104 |
+
# ======================
|
| 105 |
+
elif st.session_state.page == "search":
|
| 106 |
+
st.title("โ Qurโan Search")
|
| 107 |
+
topic = st.text_input("What does the Qurโan say about...")
|
| 108 |
+
if st.button("Get Ayah"):
|
| 109 |
+
prompt = f"Provide a Quranic Ayah for: {topic}."
|
| 110 |
+
response = client.chat.completions.create(
|
| 111 |
+
model="gpt-4o-mini",
|
| 112 |
+
messages=[{"role": "user", "content": prompt}]
|
| 113 |
)
|
| 114 |
+
st.write(response.choices[0].message.content)
|
| 115 |
+
|
| 116 |
+
# ======================
|
| 117 |
+
# TRACKER
|
| 118 |
+
# ======================
|
| 119 |
+
elif st.session_state.page == "tracker":
|
| 120 |
+
st.title("๐ฑ Healing Tracker")
|
| 121 |
+
goal = st.text_input("Your Goal")
|
| 122 |
+
days = st.slider("Days:", 1, 30, 7)
|
| 123 |
+
if "progress" not in st.session_state:
|
| 124 |
+
st.session_state.progress = [False] * days
|
| 125 |
+
for i in range(days):
|
| 126 |
+
done = st.checkbox(f"Day {i+1}", value=st.session_state.progress[i], key=f"day{i}")
|
| 127 |
+
st.session_state.progress[i] = done
|
| 128 |
+
percent = int(sum(st.session_state.progress) / days * 100)
|
| 129 |
+
st.progress(percent)
|
| 130 |
+
|
| 131 |
+
# ======================
|
| 132 |
+
# MASJID FINDER
|
| 133 |
+
# ======================
|
| 134 |
+
elif st.session_state.page == "masjid":
|
| 135 |
+
st.title("๐ Masjid Finder")
|
| 136 |
+
city = st.selectbox("City", list(masjid_data.keys()))
|
| 137 |
+
area = st.text_input("Area")
|
| 138 |
+
if st.button("Find"):
|
| 139 |
+
match, score = process.extractOne(area.lower(), masjid_data[city].keys())
|
| 140 |
+
if score >= 70:
|
| 141 |
+
st.write(f"Masajid near {match.title()}:")
|
| 142 |
+
for masjid in masjid_data[city][match]:
|
| 143 |
+
st.write(f"- {masjid}")
|
| 144 |
+
else:
|
| 145 |
+
st.warning("No match found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|