fajarah commited on
Commit
5cad09b
ยท
verified ยท
1 Parent(s): 69f23c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -65
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": "\"Do not grieve; indeed Allah is with us.\"\n\n**Arabic:** ู„ูŽุง ุชูŽุญู’ุฒูŽู†ู’ ุฅูู†ู‘ูŽ ุงู„ู„ู‘ูŽู‡ูŽ ู…ูŽุนูŽู†ูŽุง\n(Qur'an, At-Tawbah 9:40)",
20
- "joy": "\"If you are grateful, I will surely increase your favor.\"\n\n**Arabic:** ู„ูŽุฆูู† ุดูŽูƒูŽุฑู’ุชูู…ู’ ู„ูŽุฃูŽุฒููŠุฏูŽู†ู‘ูŽูƒูู…ู’\n(Qur'an, Ibrahim 14:7)",
21
- "fear": "\"Sufficient for us is Allah, and [He is] the best Disposer of affairs.\"\n\n**Arabic:** ุญูŽุณู’ุจูู†ูŽุง ุงู„ู„ู‘ูŽู‡ู ูˆูŽู†ูุนู’ู…ูŽ ุงู„ู’ูˆูŽูƒููŠู„ู\n(Qur'an, Al Imran 3:173)",
22
- "anger": "\"The strong is not the one who overcomes people by his strength, but the strong is the one who controls himself while in anger.\"\n\n(Hadith, Sahih al-Bukhari 6114)",
23
- "confusion": "\"Seek help through patience and prayer.\"\n\n**Arabic:** ูˆูŽุงุณู’ุชูŽุนููŠู†ููˆุง ุจูุงู„ุตู‘ูŽุจู’ุฑู ูˆูŽุงู„ุตู‘ูŽู„ูŽุงุฉู\n(Qur'an, Al-Baqarah 2:45)",
24
- "love": "\"Indeed, those who have believed and done righteous deeds - the Most Merciful will appoint for them affection.\"\n\n**Arabic:** ุณูŽูŠูŽุฌู’ุนูŽู„ู ู„ูŽู‡ูู…ู ุงู„ุฑู‘ูŽุญู’ู…ูŽูฐู†ู ูˆูุฏู‘ู‹ุง\n(Qur'an, Maryam 19:96)",
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, "May Allah always guide your heart.")
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, "May Allah always guide your heart.")
85
 
86
  labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]
87
- icons = [emotion_icons.get(label, '') + ' ' + label.capitalize() + f" ({probs[i]:.2f})" for i, label in enumerate(labels)]
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: linear-gradient(to right, #f9f9f9, #d4ecff);
102
- font-family: 'Segoe UI', sans-serif;
 
 
103
  }
104
  .gr-button {
105
- background-color: #007BFF !important;
106
  color: white !important;
107
  border-radius: 8px !important;
108
- font-weight: bold;
 
109
  }
110
  .gr-button:hover {
111
- background-color: #0056b3 !important;
112
  }
113
- .gr-textbox {
114
- border-radius: 8px !important;
115
- border: 1px solid #ccc !important;
116
- padding: 10px !important;
 
117
  }
118
  .output-textbox {
119
- font-size: 1.5rem;
120
- font-weight: bold;
121
- color: #333;
122
- background-color: #f1f9ff;
123
- border-radius: 8px;
124
- padding: 10px;
125
- border: 1px solid #007BFF;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.Textbox(label="Quranic/Hadith Advice (Text)", elem_classes=["output-textbox"]),
139
- gr.Textbox(label="Detected Photo Emotion", elem_classes=["output-textbox"]),
140
- gr.Textbox(label="Quranic/Hadith Advice (Image)", elem_classes=["output-textbox"])
141
  ],
142
- title="๐Ÿฅฐ Multi-Modal Emotion Detector with Islamic Insight",
143
- description="Analyze emotion from both text and a facial photo. Then receive inspirational Islamic advice based on your mood.",
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()