Update app.py
Browse files
app.py
CHANGED
@@ -4,66 +4,46 @@ from torch.nn.functional import sigmoid
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
|
7 |
-
# Load text emotion model
|
8 |
model_name = "SamLowe/roberta-base-go_emotions"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
11 |
|
12 |
-
# Load image emotion model
|
13 |
image_model_name = "Celal11/resnet-50-finetuned-FER2013-0.001"
|
14 |
image_processor = AutoImageProcessor.from_pretrained(image_model_name)
|
15 |
image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
|
16 |
|
17 |
-
# Islamic advice mapping based on emotion
|
18 |
islamic_advice = {
|
19 |
-
"sadness": "<div class='notion-card'><h3
|
20 |
-
"joy": "<div class='notion-card'><h3
|
21 |
-
"fear": "<div class='notion-card'><h3
|
22 |
-
"anger": "<div class='notion-card'><h3
|
23 |
-
"confusion": "<div class='notion-card'><h3
|
24 |
-
"love": "<div class='notion-card'><h3
|
25 |
-
"neutral": "<div class='notion-card'><h3>May Allah always guide your heart in every situation.</
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
def analyze_image_emotion(image):
|
30 |
-
if image is None:
|
31 |
-
return "No image provided.", ""
|
32 |
-
inputs = image_processor(images=image, return_tensors="pt")
|
33 |
-
with torch.no_grad():
|
34 |
-
logits = image_model(**inputs).logits
|
35 |
-
probs = torch.nn.functional.softmax(logits, dim=1)[0]
|
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)
|
45 |
with torch.no_grad():
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
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 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
|
|
59 |
|
60 |
-
# Combined analysis
|
61 |
-
def analyze_combined(text, threshold, image):
|
62 |
-
text_result, text_tip = get_emotions(text, threshold)
|
63 |
-
image_result, image_tip = analyze_image_emotion(image)
|
64 |
-
return text_result, text_tip, image_result, image_tip
|
65 |
-
|
66 |
-
# Gradio UI
|
67 |
custom_css = """
|
68 |
body {
|
69 |
background: #f9fafb;
|
@@ -81,16 +61,6 @@ body {
|
|
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;
|
@@ -98,18 +68,37 @@ body {
|
|
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.
|
106 |
-
margin-bottom:
|
|
|
107 |
}
|
108 |
.notion-card p {
|
109 |
-
font-size:
|
110 |
color: #374151;
|
111 |
margin: 0;
|
112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
"""
|
114 |
|
115 |
demo = gr.Interface(
|
@@ -119,16 +108,11 @@ demo = gr.Interface(
|
|
119 |
gr.Slider(minimum=0.1, maximum=0.9, value=0.3, step=0.05, label="Threshold"),
|
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="
|
130 |
theme="default",
|
131 |
css=custom_css
|
132 |
)
|
133 |
|
134 |
-
demo.launch()
|
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
|
|
|
7 |
model_name = "SamLowe/roberta-base-go_emotions"
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
|
|
|
11 |
image_model_name = "Celal11/resnet-50-finetuned-FER2013-0.001"
|
12 |
image_processor = AutoImageProcessor.from_pretrained(image_model_name)
|
13 |
image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
|
14 |
|
|
|
15 |
islamic_advice = {
|
16 |
+
"sadness": "<h1 class='emoji'>๐ข</h1><div class='notion-card'><h3>Sadness</h3><p><strong>โDo not grieve; indeed Allah is with us.โ</strong><br><span class='arabic'>ููุง ุชูุญูุฒููู ุฅูููู ุงูููููู ู
ูุนูููุง</span><br><em>Qur'an, At-Tawbah 9:40</em></p></div>",
|
17 |
+
"joy": "<h1 class='emoji'>๐</h1><div class='notion-card'><h3>Joy</h3><p><strong>โIf you are grateful, I will surely increase your favor.โ</strong><br><span class='arabic'>ููุฆูู ุดูููุฑูุชูู
ู ููุฃูุฒููุฏููููููู
ู</span><br><em>Qur'an, Ibrahim 14:7</em></p></div>",
|
18 |
+
"fear": "<h1 class='emoji'>๐ฑ</h1><div class='notion-card'><h3>Fear</h3><p><strong>โSufficient for us is Allah, and [He is] the best Disposer of affairs.โ</strong><br><span class='arabic'>ุญูุณูุจูููุง ุงูููููู ููููุนูู
ู ุงููููููููู</span><br><em>Qur'an, Al Imran 3:173</em></p></div>",
|
19 |
+
"anger": "<h1 class='emoji'>๐ก</h1><div class='notion-card'><h3>Anger</h3><p><strong>โThe strong is not the one who overcomes people by his strength, but the strong is the one who controls himself while in anger.โ</strong><br><em>Hadith, Sahih al-Bukhari 6114</em></p></div>",
|
20 |
+
"confusion": "<h1 class='emoji'>๐ค</h1><div class='notion-card'><h3>Confusion</h3><p><strong>โSeek help through patience and prayer.โ</strong><br><span class='arabic'>ููุงุณูุชูุนูููููุง ุจูุงูุตููุจูุฑู ููุงูุตููููุงุฉู</span><br><em>Qur'an, Al-Baqarah 2:45</em></p></div>",
|
21 |
+
"love": "<h1 class='emoji'>โค๏ธ</h1><div class='notion-card'><h3>Love</h3><p><strong>โIndeed, those who have believed and done righteous deeds - the Most Merciful will appoint for them affection.โ</strong><br><span class='arabic'>ุณูููุฌูุนููู ููููู
ู ุงูุฑููุญูู
ููฐูู ููุฏููุง</span><br><em>Qur'an, Maryam 19:96</em></p></div>",
|
22 |
+
"neutral": "<h1 class='emoji'>๐</h1><div class='notion-card'><h3>Neutral</h3><p><strong>May Allah always guide your heart in every situation.</strong></p></div>"
|
23 |
}
|
24 |
|
25 |
+
def analyze_combined(text, threshold, image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
27 |
with torch.no_grad():
|
28 |
+
logits_text = model(**inputs).logits
|
29 |
+
probs_text = sigmoid(logits_text)[0]
|
30 |
+
text_idx = torch.argmax(probs_text).item()
|
31 |
+
text_label = model.config.id2label[text_idx].lower()
|
|
|
|
|
|
|
32 |
|
33 |
+
if image:
|
34 |
+
inputs_image = image_processor(images=image, return_tensors="pt")
|
35 |
+
with torch.no_grad():
|
36 |
+
logits_img = image_model(**inputs_image).logits
|
37 |
+
probs_img = torch.nn.functional.softmax(logits_img, dim=1)[0]
|
38 |
+
img_idx = torch.argmax(probs_img).item()
|
39 |
+
img_label = image_model.config.id2label[img_idx].lower()
|
40 |
+
else:
|
41 |
+
img_label = None
|
42 |
|
43 |
+
final_label = text_label if img_label is None else img_label
|
44 |
+
card = islamic_advice.get(final_label, islamic_advice["neutral"])
|
45 |
+
return card
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
custom_css = """
|
48 |
body {
|
49 |
background: #f9fafb;
|
|
|
61 |
.gr-button:hover {
|
62 |
background-color: #2563eb !important;
|
63 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
.notion-card {
|
65 |
background: #ffffff;
|
66 |
border-radius: 12px;
|
|
|
68 |
padding: 16px;
|
69 |
margin-top: 12px;
|
70 |
box-shadow: 0 2px 6px rgba(0,0,0,0.04);
|
71 |
+
max-width: 600px;
|
72 |
+
margin-left: auto;
|
73 |
+
margin-right: auto;
|
74 |
}
|
75 |
.notion-card h3 {
|
76 |
margin-top: 0;
|
77 |
color: #111827;
|
78 |
+
font-size: 1.25rem;
|
79 |
+
margin-bottom: 8px;
|
80 |
+
font-weight: 600;
|
81 |
}
|
82 |
.notion-card p {
|
83 |
+
font-size: 1rem;
|
84 |
color: #374151;
|
85 |
margin: 0;
|
86 |
}
|
87 |
+
.emoji {
|
88 |
+
text-align: center;
|
89 |
+
font-size: 3rem;
|
90 |
+
margin: 0;
|
91 |
+
padding: 0;
|
92 |
+
}
|
93 |
+
.arabic {
|
94 |
+
font-size: 1.3rem;
|
95 |
+
direction: rtl;
|
96 |
+
display: block;
|
97 |
+
margin-top: 4px;
|
98 |
+
margin-bottom: 4px;
|
99 |
+
text-align: right;
|
100 |
+
font-family: 'Scheherazade', serif;
|
101 |
+
}
|
102 |
"""
|
103 |
|
104 |
demo = gr.Interface(
|
|
|
108 |
gr.Slider(minimum=0.1, maximum=0.9, value=0.3, step=0.05, label="Threshold"),
|
109 |
gr.Image(type="pil", label="Upload Face Photo")
|
110 |
],
|
111 |
+
outputs=gr.HTML(label="Detected Emotion & Islamic Insight"),
|
|
|
|
|
|
|
|
|
|
|
112 |
title="๐ง EmotionLens: Understand & Reflect Emotionally",
|
113 |
+
description="Analyze how you feel through text and facial expression, and receive inspirational Islamic wisdom in a beautifully styled reflection.",
|
114 |
theme="default",
|
115 |
css=custom_css
|
116 |
)
|
117 |
|
118 |
+
demo.launch()
|