File size: 2,436 Bytes
5acf4ff
 
 
 
 
 
c79b849
 
 
5acf4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py

import streamlit as st
from transformers import pipeline
import random

# โœ… โœ… Must be FIRST!
st.set_page_config(page_title="Qurโ€™an Healing Soul - Text Emotion", page_icon="๐Ÿ•Œ")

# Quranic ayahs dataset mapped to emotions
ayahs = {
    "sad": [
        {
            "ayah": "2:286",
            "arabic": "ู„ูŽุง ูŠููƒูŽู„ู‘ููู ุงู„ู„ู‘ูŽู‡ู ู†ูŽูู’ุณู‹ุง ุฅูู„ู‘ูŽุง ูˆูุณู’ุนูŽู‡ูŽุง",
            "translation": "Allah does not burden a soul beyond that it can bear.",
            "tafsir": "Every test is within your capacity, with Allahโ€™s help."
        }
    ],
    "happy": [
        {
            "ayah": "94:5-6",
            "arabic": "ููŽุฅูู†ู‘ูŽ ู…ูŽุนูŽ ุงู„ู’ุนูุณู’ุฑู ูŠูุณู’ุฑู‹ุง ุฅูู†ู‘ูŽ ู…ูŽุนูŽ ุงู„ู’ุนูุณู’ุฑู ูŠูุณู’ุฑู‹ุง",
            "translation": "Indeed, with hardship comes ease.",
            "tafsir": "Your happiness is part of Allahโ€™s ease."
        }
    ],
    "angry": [
        {
            "ayah": "3:134",
            "arabic": "ูˆูŽุงู„ู’ูƒูŽุงุธูู…ููŠู†ูŽ ุงู„ู’ุบูŽูŠู’ุธูŽ",
            "translation": "Those who restrain anger...",
            "tafsir": "Patience and controlling anger are rewarded."
        }
    ]
}

# Load pre-trained sentiment-analysis pipeline
@st.cache_resource
def load_pipeline():
    return pipeline("sentiment-analysis")

nlp = load_pipeline()

# UI
st.title("๐Ÿ•Œ Qurโ€™an Healing Soul - Text Emotion")
st.write("Share how you feel in your own words, and receive an ayah for healing.")

user_input = st.text_area("โœ๏ธ How are you feeling today?")

if st.button("Analyze"):
    if user_input.strip() == "":
        st.warning("Please write something about how you feel.")
    else:
        result = nlp(user_input)[0]
        label = result['label'].lower()
        score = result['score']

        st.info(f"**Detected Sentiment:** `{label}` ({round(score*100, 2)}%)")

        # Very basic mapping: map sentiment to emotion group
        if "negative" in label or "sad" in label:
            key = "sad"
        elif "positive" in label or "happy" in label:
            key = "happy"
        elif "angry" in label:
            key = "angry"
        else:
            key = "sad"

        ayah = random.choice(ayahs[key])
        st.markdown(f"""
        **๐Ÿ“– Ayah ({ayah['ayah']})**

        Arabic: *{ayah['arabic']}*

        Translation: {ayah['translation']}

        Tafsir: {ayah['tafsir']}
        """)