Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import json | |
import random | |
import requests | |
from datetime import datetime | |
from dotenv import load_dotenv | |
# โ MUST be the first Streamlit command | |
st.set_page_config(page_title="Kalam Comfort", page_icon="๐") | |
# -------------------------- | |
# ๐ Load environment variables (optional) | |
# -------------------------- | |
load_dotenv() | |
# -------------------------- | |
# ๐ Create or load files | |
# -------------------------- | |
# 1. moods.json | |
default_moods = { | |
"anxious": ["13:28", "2:286", "10:62"], | |
"sad": ["94:5", "93:6", "65:7"], | |
"hopeless": ["39:53", "12:87", "3:139"], | |
"grateful": ["14:7", "31:12"], | |
"lonely": ["2:186", "9:40"], | |
"angry": ["3:134", "41:34"], | |
"lost": ["93:7", "6:122"], | |
"tired": ["94:6", "3:200"], | |
"afraid": ["8:46", "33:3"] | |
} | |
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) | |
# 2. reflections.json | |
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) | |
# 3. Optional: warn if .env doesn't exist | |
if not os.path.exists(".env"): | |
st.warning("โ ๏ธ Optional: .env file not found. It's not required unless you're adding secrets later.") | |
# -------------------------- | |
# ๐ Streamlit 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.experimental_rerun() | |
if selected_mood: | |
verse_list = moods[selected_mood] | |
verse_ref = random.choice(verse_list) | |
surah_num, ayah_num = verse_ref.split(":") | |
# ๐ 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}") | |
# Tafsir and Dua | |
tafsir_map = { | |
"anxious": "The heart finds true peace in the remembrance of Allah.", | |
"sad": "Ease always follows hardship โ hold on to hope.", | |
"hopeless": "Allahโs mercy is vast. Never give up.", | |
"grateful": "Gratitude brings increase โ in peace, health, and blessings.", | |
"lonely": "Allah is always near. Call upon Him.", | |
"angry": "Restrain your anger and be among the righteous.", | |
"lost": "He guided you when you were unaware.", | |
"tired": "After difficulty comes ease โ stay patient.", | |
"afraid": "Allah never burdens a soul more than it can bear." | |
} | |
dua_map = { | |
"anxious": "ุงูููู ุงุฌุนู ููุจู ู ุทู ุฆููุง ุจุฐูุฑู\n*O Allah, make my heart tranquil with Your remembrance.*", | |
"sad": "ุงูููู ุงุฌุจุฑ ููุจู ุฌุจุฑุง ูุชุนุฌุจ ูู ุฃูู ุงูุณู ุงูุงุช ูุงูุฃุฑุถ\n*O Allah, mend my heart in a way that amazes the heavens and earth.*", | |
"hopeless": "ุฑุจ ูุง ุชุฐุฑูู ูุฑุฏุง ูุฃูุช ุฎูุฑ ุงููุงุฑุซูู\n*My Lord, do not leave me alone โ You are the Best Inheritor.*", | |
"grateful": "ุงูููู ุงุฌุนููู ูู ุดููููุฑูุง\n*O Allah, make me deeply grateful to You.*", | |
"lonely": "ุงูููู ูู ู ุนู ุญูู ูุง ูููู ุฃุญุฏ ุจุฌุงูุจู\n*O Allah, be with me when no one else is.*", | |
"angry": "ุงูููู ุงุฑุฒููู ุงูุญูู ุนูุฏ ุงูุบุถุจ\n*O Allah, grant me forbearance when Iโm angry.*", | |
"lost": "ุงูููู ุฏูููู ุนูู ุตุฑุงุทู ุงูู ุณุชููู \n*O Allah, guide me to Your straight path.*", | |
"tired": "ุงูููู ุฌุฏุฏ ุทุงูุชูุ ูุงุฑุฒููู ุฑุงุญุฉ ุงูุจุงู\n*O Allah, renew my energy and grant me peace of mind.*", | |
"afraid": "ุฑุจูู ุฃุนูู ููุง ุชุนู ุนููู\n*My Lord, support me and not against me.*" | |
} | |
st.markdown("### โจ Tafsir") | |
st.info(tafsir_map.get(selected_mood, "Let the verse speak to your heart.")) | |
st.markdown("### ๐คฒ Dua") | |
st.markdown(dua_map.get(selected_mood, "May Allah ease your situation.")) | |
# Reflection Input | |
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']}") | |
else: | |
st.error("โ Could not fetch verse. Check internet or try again.") | |