Spaces:
Running
Running
import streamlit as st | |
import os | |
import json | |
import random | |
import requests | |
from datetime import datetime | |
from dotenv import load_dotenv | |
st.set_page_config(page_title="Kalam Comfort", page_icon="📚") | |
load_dotenv() | |
# ---------- File Setup ---------- | |
default_moods = { | |
"anxious": ["13:28", "2:286", "10:62", "20:46", "48:4"], | |
"sad": ["94:5", "93:6", "65:7", "9:51", "12:86"], | |
"hopeless": ["39:53", "12:87", "3:139", "65:2", "57:22"], | |
"grateful": ["14:7", "31:12", "2:152", "16:114", "76:3"], | |
"lonely": ["2:186", "9:40", "6:103", "50:16", "17:79"], | |
"angry": ["3:134", "41:34", "7:199", "42:37", "16:126"], | |
"lost": ["93:7", "6:122", "2:2", "24:35", "42:52"], | |
"tired": ["94:6", "3:200", "29:69", "52:48", "6:162"], | |
"afraid": ["8:46", "33:3", "2:286", "3:173", "11:56"] | |
} | |
if not os.path.exists("moods.json"): | |
with open("moods.json", "w") as f: | |
json.dump(default_moods, f, indent=4) | |
with open("moods.json", "r") as f: | |
moods = json.load(f) | |
if not os.path.exists("reflections.json"): | |
with open("reflections.json", "w") as f: | |
json.dump({}, f) | |
with open("reflections.json", "r") as f: | |
reflections = json.load(f) | |
# ---------- UI ---------- | |
st.title("📚 Kalam Comfort") | |
st.subheader("Find Quranic comfort by emotion 💖") | |
selected_mood = st.selectbox("How are you feeling today?", list(moods.keys()), index=0) | |
if st.button("🔁 Show another verse"): | |
st.rerun() | |
if selected_mood: | |
verse_list = moods[selected_mood] | |
verse_ref = random.choice(verse_list) | |
surah_num, ayah_num = verse_ref.split(":") | |
# 1. Quran Arabic, English, Urdu | |
quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad,ur.jalandhry" | |
response = requests.get(quran_api) | |
if response.status_code == 200: | |
data = response.json()['data'] | |
arabic = data[0]['text'] | |
english = data[1]['text'] | |
urdu = data[2]['text'] | |
surah_info = f"{data[0]['surah']['englishName']} ({data[0]['surah']['name']})" | |
st.markdown("### 🌙 Quranic Verse") | |
st.markdown(f"**Surah:** {surah_info} — Ayah {ayah_num}") | |
st.markdown(f"<div style='font-size:24px; direction: rtl'>{arabic}</div>", unsafe_allow_html=True) | |
st.markdown(f"**📖 English:** *{english}*") | |
st.markdown(f"**📖 Urdu:** {urdu}") | |
else: | |
st.error("❌ Failed to fetch Quranic text.") | |
st.stop() | |
# 2. Dua (Mood-based, bilingual) | |
dua_map = { | |
"anxious": "اللهم اجعل قلبي مطمئنًا بذكرك\n**O Allah, make my heart tranquil with Your remembrance.**\n**یا اللہ! میرے دل کو اپنے ذکر سے اطمینان دے۔**", | |
"sad": "اللهم اجبر قلبي جبرا يتعجب له أهل السماوات والأرض\n**O Allah, mend my heart in a way that amazes the heavens and earth.**\n**یا اللہ! میرے دل کو ایسا سہارا دے جو زمین و آسمان کو حیران کر دے۔**", | |
"hopeless": "رب لا تذرني فردا وأنت خير الوارثين\n**My Lord, do not leave me alone — You are the Best Inheritor.**\n**یا رب! مجھے تنہا نہ چھوڑ، تو بہترین وارث ہے۔**", | |
"grateful": "اللهم اجعلني لك شَكُورًا\n**O Allah, make me deeply grateful to You.**\n**یا اللہ! مجھے شکر گزار بنا دے۔**", | |
"lonely": "اللهم كن معي حين لا يكون أحد بجانبي\n**O Allah, be with me when no one else is.**\n**یا اللہ! جب کوئی ساتھ نہ ہو، تو میرے ساتھ ہو۔**", | |
"angry": "اللهم ارزقني الحلم عند الغضب\n**O Allah, grant me forbearance when I’m angry.**\n**یا اللہ! غصے میں مجھے برداشت عطا فرما۔**", | |
"lost": "اللهم دلّني على صراطك المستقيم\n**O Allah, guide me to Your straight path.**\n**یا اللہ! مجھے اپنے سیدھے راستے پر چلا۔**", | |
"tired": "اللهم جدد طاقتي، وارزقني راحة البال\n**O Allah, renew my energy and grant me peace of mind.**\n**یا اللہ! میری طاقت کو تازہ کر اور مجھے ذہنی سکون عطا فرما۔**", | |
"afraid": "ربِّ أعني ولا تعن عليّ\n**My Lord, support me and not against me.**\n**میرے رب! میری مدد فرما، میرے خلاف نہ ہو۔**" | |
} | |
st.markdown("### 🤲 Dua (English + Urdu)") | |
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation.")) | |
# 3. Hadith (Mood-related inspiration) | |
hadith_map = { | |
"anxious": "The Prophet ﷺ said: *Verily, in the remembrance of Allah do hearts find rest.*", | |
"sad": "The Prophet ﷺ said: *Indeed with hardship comes ease.*", | |
"hopeless": "The Prophet ﷺ said: *No one relies upon Allah except that He is sufficient for him.*", | |
"grateful": "The Prophet ﷺ said: *He who does not thank people, does not thank Allah.*", | |
"lonely": "The Prophet ﷺ said: *I am closer to the believers than their own selves.*", | |
"angry": "The Prophet ﷺ said: *The strong man is not one who can wrestle, but the one who controls himself at the time of anger.*", | |
"lost": "The Prophet ﷺ said: *Whoever seeks a path in search of knowledge, Allah will ease his path to Paradise.*", | |
"tired": "The Prophet ﷺ said: *Every hardship a believer experiences removes sin.*", | |
"afraid": "The Prophet ﷺ said: *Be mindful of Allah, He will protect you.*" | |
} | |
st.markdown("### 🌟 Inspirational Hadith") | |
st.info(hadith_map.get(selected_mood, "May peace and strength be with you.")) | |
# 4. Reflections | |
st.markdown("### 📝 Your Reflection") | |
reflection_input = st.text_area("Write your thoughts:", placeholder="How does this verse speak to you today?") | |
if st.button("💾 Save Reflection"): | |
reflections[selected_mood] = { | |
"text": reflection_input, | |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M") | |
} | |
with open("reflections.json", "w") as f: | |
json.dump(reflections, f, indent=4) | |
st.success("Reflection saved.") | |
if selected_mood in reflections: | |
last = reflections[selected_mood] | |
st.markdown("#### 📜 Last Saved Reflection:") | |
st.markdown(f"**{last['timestamp']}** — {last['text']}") | |