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.")