Shaafi / app.py
ibrahim321123's picture
Update app.py
79e36d5 verified
import streamlit as st
import random
from gradio_client import Client, file
# ====================================
# Example Ayahs
# ====================================
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."
}
]
}
# ====================================
# Streamlit UI
# ====================================
st.set_page_config(page_title="Qurโ€™an Healing Soul - Emotion from Image", page_icon="๐ŸŒ™")
st.title("๐ŸŒ™ Qurโ€™an Healing Soul - FER via API")
uploaded_file = st.file_uploader("Upload your selfie", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
# Show uploaded image
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
st.info("Detecting emotion... please wait...")
# Save file locally
with open("temp_image.png", "wb") as f:
f.write(uploaded_file.read())
# Call Gradio API
client = Client("ElenaRyumina/face_emotion_recognition")
result = client.predict(
inp=file("temp_image.png"),
api_name="/preprocess_image_and_predict"
)
_, _, confidences = result
# Use top label directly
dominant = confidences["label"]
st.success(f"Dominant Emotion: **{dominant}**")
# Map to emotion group
dominant_lower = dominant.lower()
if dominant_lower in ["sad", "fear"]:
key = "sad"
elif dominant_lower in ["happy", "happiness", "surprise"]:
key = "happy"
elif dominant_lower in ["angry", "disgust"]:
key = "angry"
else:
key = "sad"
# Show ayah
ayah = random.choice(ayahs[key])
st.markdown(f"""
### ๐Ÿ“– Ayah ({ayah['ayah']})
**Arabic:** *{ayah['arabic']}*
**Translation:** {ayah['translation']}
**Tafsir:** {ayah['tafsir']}
""")