File size: 6,418 Bytes
304b31b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import streamlit as st
import os
import json
import random
import requests
from datetime import datetime
from dotenv import load_dotenv

# --------------------------
# ๐Ÿ” Load environment vars
# --------------------------
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. .env โ†’ already handled by `load_dotenv()` above
if not os.path.exists(".env"):
    st.warning("โš ๏ธ .env file not found. Make sure to set secrets if using Firebase or APIs.")

# 4. firebase_config.json โ†’ check only (optional)
use_firebase = os.getenv("USE_FIREBASE", "false").lower() == "true"
firebase_available = os.path.exists("firebase_config.json")

if use_firebase and not firebase_available:
    st.error("Firebase is enabled in .env but firebase_config.json is missing!")

# Optional: Firebase setup block
if use_firebase and firebase_available:
    try:
        import firebase_admin
        from firebase_admin import credentials, db

        if not firebase_admin._apps:
            cred = credentials.Certificate("firebase_config.json")
            firebase_admin.initialize_app(cred, {
                'databaseURL': os.getenv("FIREBASE_DB_URL")
            })
        st.success("โœ… Firebase initialized.")
    except Exception as e:
        st.error(f"Firebase error: {e}")

# --------------------------
# ๐ŸŒ™ Streamlit UI
# --------------------------
st.set_page_config(page_title="Kalam Comfort", page_icon="๐Ÿ“š")
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)

# Refresh feature
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(":")

    # Fetch verse from API
    quran_api = f"https://api.alquran.cloud/v1/ayah/{surah_num}:{ayah_num}/editions/quran-simple,en.asad"
    response = requests.get(quran_api)

    if response.status_code == 200:
        data = response.json()['data']
        arabic = data[0]['text']
        english = data[1]['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}*")

        # Tafsir and Dua mappings
        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."))

        # ๐Ÿ“ User Reflection
        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.")

        # Show existing reflection
        if selected_mood in reflections:
            st.markdown("#### ๐Ÿ“œ Last Saved Reflection:")
            last = reflections[selected_mood]
            st.markdown(f"**{last['timestamp']}** โ€” {last['text']}")
    else:
        st.error("โŒ Could not fetch verse. Check internet or try again.")