Update app.py
Browse files
app.py
CHANGED
@@ -16,13 +16,13 @@ image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
|
|
16 |
|
17 |
# Islamic advice mapping based on emotion
|
18 |
islamic_advice = {
|
19 |
-
"sadness": "
|
20 |
-
"joy": "
|
21 |
-
"fear": "
|
22 |
-
"anger": "
|
23 |
-
"confusion": "
|
24 |
-
"love": "
|
25 |
-
"neutral": "May Allah always guide your heart in every situation
|
26 |
}
|
27 |
|
28 |
# Analyze image emotion using processor and model
|
@@ -36,41 +36,9 @@ def analyze_image_emotion(image):
|
|
36 |
pred_idx = torch.argmax(probs).item()
|
37 |
label = image_model.config.id2label[pred_idx].lower()
|
38 |
score = probs[pred_idx].item()
|
39 |
-
islamic = islamic_advice.get(label, "
|
40 |
return f"{label.capitalize()} ({score:.2f})", islamic
|
41 |
|
42 |
-
# Emotion label to icon mapping (subset)
|
43 |
-
emotion_icons = {
|
44 |
-
"admiration": "๐",
|
45 |
-
"amusement": "๐
",
|
46 |
-
"anger": "๐ก",
|
47 |
-
"annoyance": "๐",
|
48 |
-
"approval": "๐",
|
49 |
-
"caring": "๐",
|
50 |
-
"confusion": "๐ค",
|
51 |
-
"curiosity": "๐ฎ",
|
52 |
-
"desire": "๐คค",
|
53 |
-
"disappointment": "๐",
|
54 |
-
"disapproval": "๐",
|
55 |
-
"disgust": "๐คฎ",
|
56 |
-
"embarrassment": "๐ณ",
|
57 |
-
"excitement": "๐",
|
58 |
-
"fear": "๐ฑ",
|
59 |
-
"gratitude": "๐",
|
60 |
-
"grief": "๐ญ",
|
61 |
-
"joy": "๐",
|
62 |
-
"love": "โค๏ธ",
|
63 |
-
"nervousness": "๐คง",
|
64 |
-
"optimism": "๐",
|
65 |
-
"pride": "๐",
|
66 |
-
"realization": "๐คฏ",
|
67 |
-
"relief": "๐",
|
68 |
-
"remorse": "๐",
|
69 |
-
"sadness": "๐ข",
|
70 |
-
"surprise": "๐ฒ",
|
71 |
-
"neutral": "๐"
|
72 |
-
}
|
73 |
-
|
74 |
# Analyze text emotion
|
75 |
def get_emotions(text, threshold):
|
76 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
@@ -81,10 +49,10 @@ def get_emotions(text, threshold):
|
|
81 |
top_idx = torch.argmax(probs).item()
|
82 |
top_label = model.config.id2label[top_idx].lower()
|
83 |
top_score = probs[top_idx].item()
|
84 |
-
islamic = islamic_advice.get(top_label, "
|
85 |
|
86 |
labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]
|
87 |
-
icons = [
|
88 |
|
89 |
result = ", ".join(icons) if icons else "No strong emotion detected."
|
90 |
return result, islamic
|
@@ -98,31 +66,49 @@ def analyze_combined(text, threshold, image):
|
|
98 |
# Gradio UI
|
99 |
custom_css = """
|
100 |
body {
|
101 |
-
background:
|
102 |
-
font-family: '
|
|
|
|
|
103 |
}
|
104 |
.gr-button {
|
105 |
-
background-color: #
|
106 |
color: white !important;
|
107 |
border-radius: 8px !important;
|
108 |
-
font-weight:
|
|
|
109 |
}
|
110 |
.gr-button:hover {
|
111 |
-
background-color: #
|
112 |
}
|
113 |
-
.gr-textbox {
|
114 |
-
border-radius:
|
115 |
-
border: 1px solid #
|
116 |
-
padding:
|
|
|
117 |
}
|
118 |
.output-textbox {
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
border: 1px solid #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
}
|
127 |
"""
|
128 |
|
@@ -134,15 +120,15 @@ demo = gr.Interface(
|
|
134 |
gr.Image(type="pil", label="Upload Face Photo")
|
135 |
],
|
136 |
outputs=[
|
137 |
-
gr.Textbox(label="Detected Text Emotions", elem_classes=["output-textbox"]),
|
138 |
-
gr.
|
139 |
-
gr.Textbox(label="Detected Photo Emotion", elem_classes=["output-textbox"]),
|
140 |
-
gr.
|
141 |
],
|
142 |
-
title="
|
143 |
-
description="Analyze
|
144 |
theme="default",
|
145 |
css=custom_css
|
146 |
)
|
147 |
|
148 |
-
demo.launch()
|
|
|
16 |
|
17 |
# Islamic advice mapping based on emotion
|
18 |
islamic_advice = {
|
19 |
+
"sadness": "<div class='notion-card'><h3>\u201cDo not grieve; indeed Allah is with us.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ููุง ุชูุญูุฒููู ุฅูููู ุงูููููู ู
ูุนูููุง</span><br><em>Qur'an, At-Tawbah 9:40</em></p></div>",
|
20 |
+
"joy": "<div class='notion-card'><h3>\u201cIf you are grateful, I will surely increase your favor.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ููุฆูู ุดูููุฑูุชูู
ู ููุฃูุฒููุฏููููููู
ู</span><br><em>Qur'an, Ibrahim 14:7</em></p></div>",
|
21 |
+
"fear": "<div class='notion-card'><h3>\u201cSufficient for us is Allah, and [He is] the best Disposer of affairs.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ุญูุณูุจูููุง ุงูููููู ููููุนูู
ู ุงููููููููู</span><br><em>Qur'an, Al Imran 3:173</em></p></div>",
|
22 |
+
"anger": "<div class='notion-card'><h3>\u201cThe strong is not the one who overcomes people by his strength, but the strong is the one who controls himself while in anger.\u201d</h3><p><em>Hadith, Sahih al-Bukhari 6114</em></p></div>",
|
23 |
+
"confusion": "<div class='notion-card'><h3>\u201cSeek help through patience and prayer.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ููุงุณูุชูุนูููููุง ุจูุงูุตููุจูุฑู ููุงูุตููููุงุฉู</span><br><em>Qur'an, Al-Baqarah 2:45</em></p></div>",
|
24 |
+
"love": "<div class='notion-card'><h3>\u201cIndeed, those who have believed and done righteous deeds - the Most Merciful will appoint for them affection.\u201d</h3><p><strong>Arabic:</strong> <span style='font-size: 1.2em;'>ุณูููุฌูุนููู ููููู
ู ุงูุฑููุญูู
ููฐูู ููุฏููุง</span><br><em>Qur'an, Maryam 19:96</em></p></div>",
|
25 |
+
"neutral": "<div class='notion-card'><h3>May Allah always guide your heart in every situation.</h3></div>"
|
26 |
}
|
27 |
|
28 |
# Analyze image emotion using processor and model
|
|
|
36 |
pred_idx = torch.argmax(probs).item()
|
37 |
label = image_model.config.id2label[pred_idx].lower()
|
38 |
score = probs[pred_idx].item()
|
39 |
+
islamic = islamic_advice.get(label, islamic_advice["neutral"])
|
40 |
return f"{label.capitalize()} ({score:.2f})", islamic
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Analyze text emotion
|
43 |
def get_emotions(text, threshold):
|
44 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
49 |
top_idx = torch.argmax(probs).item()
|
50 |
top_label = model.config.id2label[top_idx].lower()
|
51 |
top_score = probs[top_idx].item()
|
52 |
+
islamic = islamic_advice.get(top_label, islamic_advice["neutral"])
|
53 |
|
54 |
labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]
|
55 |
+
icons = [f"{label.capitalize()} ({probs[i]:.2f})" for i, label in enumerate(labels)]
|
56 |
|
57 |
result = ", ".join(icons) if icons else "No strong emotion detected."
|
58 |
return result, islamic
|
|
|
66 |
# Gradio UI
|
67 |
custom_css = """
|
68 |
body {
|
69 |
+
background: #f9fafb;
|
70 |
+
font-family: 'Inter', sans-serif;
|
71 |
+
color: #1f2937;
|
72 |
+
padding: 20px;
|
73 |
}
|
74 |
.gr-button {
|
75 |
+
background-color: #3b82f6 !important;
|
76 |
color: white !important;
|
77 |
border-radius: 8px !important;
|
78 |
+
font-weight: 600;
|
79 |
+
padding: 10px 20px;
|
80 |
}
|
81 |
.gr-button:hover {
|
82 |
+
background-color: #2563eb !important;
|
83 |
}
|
84 |
+
.gr-textbox, .output-textbox {
|
85 |
+
border-radius: 12px !important;
|
86 |
+
border: 1px solid #d1d5db !important;
|
87 |
+
padding: 12px !important;
|
88 |
+
font-size: 1rem;
|
89 |
}
|
90 |
.output-textbox {
|
91 |
+
background-color: #ffffff;
|
92 |
+
border: 1px solid #60a5fa;
|
93 |
+
}
|
94 |
+
.notion-card {
|
95 |
+
background: #ffffff;
|
96 |
+
border-radius: 12px;
|
97 |
+
border: 1px solid #e5e7eb;
|
98 |
+
padding: 16px;
|
99 |
+
margin-top: 12px;
|
100 |
+
box-shadow: 0 2px 6px rgba(0,0,0,0.04);
|
101 |
+
}
|
102 |
+
.notion-card h3 {
|
103 |
+
margin-top: 0;
|
104 |
+
color: #111827;
|
105 |
+
font-size: 1.1rem;
|
106 |
+
margin-bottom: 6px;
|
107 |
+
}
|
108 |
+
.notion-card p {
|
109 |
+
font-size: 0.95rem;
|
110 |
+
color: #374151;
|
111 |
+
margin: 0;
|
112 |
}
|
113 |
"""
|
114 |
|
|
|
120 |
gr.Image(type="pil", label="Upload Face Photo")
|
121 |
],
|
122 |
outputs=[
|
123 |
+
gr.Textbox(label="Detected Text Emotions", elem_classes=["output-textbox"], lines=1),
|
124 |
+
gr.HTML(label="Quranic/Hadith Advice (Text)"),
|
125 |
+
gr.Textbox(label="Detected Photo Emotion", elem_classes=["output-textbox"], lines=1),
|
126 |
+
gr.HTML(label="Quranic/Hadith Advice (Image)")
|
127 |
],
|
128 |
+
title="๐ง EmotionLens: Understand & Reflect Emotionally",
|
129 |
+
description="An AI-powered emotional intelligence assistant with Islamic wisdom. Analyze how you feel from text and facial expressions, and receive gentle spiritual insight.",
|
130 |
theme="default",
|
131 |
css=custom_css
|
132 |
)
|
133 |
|
134 |
+
demo.launch()
|