ibrahim321123's picture
Update app.py
c79b849 verified
# 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']}
""")