Kalam_Comfort / app.py
ashhal's picture
Update app.py
45c97a4 verified
raw
history blame
5.71 kB
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.")