File size: 2,225 Bytes
a0df0eb
0db56a4
a0df0eb
 
e74f6be
 
0fe0caa
e74f6be
 
 
0db56a4
0a2b0f7
e74f6be
 
 
0a2b0f7
0db56a4
e74f6be
0a2b0f7
e74f6be
0a2b0f7
0db56a4
0a2b0f7
e74f6be
0a2b0f7
a0df0eb
 
e74f6be
 
 
 
 
 
 
 
 
 
 
 
 
df09473
 
 
 
 
 
e74f6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fe0caa
 
e74f6be
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
import streamlit as st
from deepface import DeepFace
from PIL import Image
import tempfile
import json
import random

# Load local Ayahs JSON
with open("ayahs.json", "r", encoding="utf-8") as f:
    ayah_data = json.load(f)

st.set_page_config(
    page_title="Emotion Recognition & Quranic Guidance",
    page_icon="πŸ•Œ",
    layout="centered",
)

st.title("πŸ“Έ Emotion Recognition & Quranic Ayah Suggestion")
st.write(
    "Upload your selfie to detect your emotion and receive a Quranic Ayah with translation and brief tafsir."
)

uploaded_file = st.file_uploader(
    "Choose an image...", type=["jpg", "jpeg", "png"]
)

if uploaded_file is not None:
    # Display uploaded image
    img = Image.open(uploaded_file).convert("RGB")
    st.image(img, caption="Uploaded Selfie", use_container_width=True)

    # Save temporarily to disk
    with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
        img.save(tmp_file.name)
        image_path = tmp_file.name

    # Analyze using DeepFace
    with st.spinner("Analyzing emotion..."):
        result = DeepFace.analyze(img_path=image_path, actions=["emotion"], enforce_detection=False)

    # βœ… Safe extraction β€” handles both dict or list
    if isinstance(result, list):
        dominant_emotion = result[0]["dominant_emotion"].lower()
    else:
        dominant_emotion = result["dominant_emotion"].lower()

    st.success(f"Detected Emotion: **{dominant_emotion.capitalize()}**")

    # Pick a random Ayah for this emotion
    ayahs_for_emotion = ayah_data.get(dominant_emotion)

    if ayahs_for_emotion:
        selected_ayah = random.choice(ayahs_for_emotion)
        st.header("πŸ“– Suggested Quranic Ayah")
        st.markdown(f"**Ayah (Arabic):** {selected_ayah['ayah_arabic']}")
        st.markdown(f"**Surah & Ayah:** {selected_ayah['surah_ayah']}")
        st.markdown(f"**Translation:** {selected_ayah['translation']}")
        st.markdown(f"**Tafsir:** {selected_ayah['tafsir']}")
    else:
        st.warning(
            f"❌ No Ayahs found for **{dominant_emotion}**. "
            f"Please add more to `ayahs.json`!"
        )

    st.info("πŸ’‘ This app uses only local, verified Ayahs for accuracy and zero hallucination.")