fajarah commited on
Commit
20d158a
ยท
verified ยท
1 Parent(s): 2f2ec99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -152
app.py CHANGED
@@ -1,170 +1,45 @@
1
  import gradio as gr
2
- import pandas as pd
3
- import os
4
- from dotenv import load_dotenv
5
- import google.generativeai as genai
6
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoImageProcessor, AutoModelForImageClassification
7
  from torch.nn.functional import sigmoid
8
  import torch
9
  from PIL import Image
10
 
11
- load_dotenv()
12
- genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
13
- model_gemini = genai.GenerativeModel("gemini-pro")
14
 
15
- def get_gemini_advice_from_text(text):
16
- prompt = f"""Suggest one Qur'an verse or Hadith to comfort and guide a person who wrote the following:
17
-
18
- '{text}'
19
-
20
- Please include:
21
- - Arabic verse or hadith
22
- - English translation
23
- - Source (Surah or Hadith reference)
24
-
25
- Wrap it in a gentle tone."""
26
- try:
27
- response = model_gemini.generate_content(prompt)
28
- return f"""<div class='notion-card fade-in'>
29
- <p style='white-space: pre-wrap;'>{response.text}</p>
30
- </div>"""
31
- except Exception as e:
32
- return f"<div class='notion-card fade-in'><p>May Allah guide your heart.</p></div>"
33
 
 
34
  model_name = "SamLowe/roberta-base-go_emotions"
35
  tokenizer = AutoTokenizer.from_pretrained(model_name)
36
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
37
 
 
38
  image_model_name = "Celal11/resnet-50-finetuned-FER2013-0.001"
39
  image_processor = AutoImageProcessor.from_pretrained(image_model_name)
40
  image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
41
 
42
- islamic_advice = {
43
- "sadness": "<h1 class='emoji'>๐Ÿ˜ข</h1><div class='notion-card animated'><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>",
44
- "joy": "<h1 class='emoji'>๐Ÿ˜ƒ</h1><div class='notion-card animated'><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>",
45
- "fear": "<h1 class='emoji'>๐Ÿ˜ฑ</h1><div class='notion-card animated'><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>",
46
- "anger": "<h1 class='emoji'>๐Ÿ˜ก</h1><div class='notion-card animated'><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>",
47
- "confusion": "<h1 class='emoji'>๐Ÿค”</h1><div class='notion-card animated'><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>",
48
- "love": "<h1 class='emoji'>โค๏ธ</h1><div class='notion-card animated'><h3>Love</h3><p><strong>โ€œThe Most Merciful will appoint for them affection.โ€</strong><br><span class='arabic'>ุณูŽูŠูŽุฌู’ุนูŽู„ู ู„ูŽู‡ูู…ู ุงู„ุฑู‘ูŽุญู’ู…ูŽูฐู†ู ูˆูุฏู‘ู‹ุง</span><br><em>Qur'an, Maryam 19:96</em></p></div>",
49
- "gratitude": "<h1 class='emoji'>๐Ÿ™</h1><div class='notion-card animated'><h3>Gratitude</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>",
50
- "disappointment": "<h1 class='emoji'>๐Ÿ˜ž</h1><div class='notion-card animated'><h3>Disappointment</h3><p><strong>โ€œIndeed, with hardship [will be] ease.โ€</strong><br><span class='arabic'>ุฅูู†ู‘ูŽ ู…ูŽุนูŽ ุงู„ู’ุนูุณู’ุฑู ูŠูุณู’ุฑู‹ุง</span><br><em>Qur'an, Ash-Sharh 94:6</em></p></div>",
51
- "nervousness": "<h1 class='emoji'>๐Ÿ˜ฌ</h1><div class='notion-card animated'><h3>Nervousness</h3><p><strong>โ€œPut your trust in Allah. Indeed, Allah loves those who rely [upon Him].โ€</strong><br><span class='arabic'>ูˆูŽุชูŽูˆูŽูƒู‘ูŽู„ู’ ุนูŽู„ูŽู‰ ุงู„ู„ู‘ูŽู‡ู</span><br><em>Qur'an, Ali 'Imran 3:159</em></p></div>",
52
- "trust": "<h1 class='emoji'>๐Ÿค</h1><div class='notion-card animated'><h3>Trust</h3><p><strong>โ€œAnd whoever puts his trust in Allah โ€“ then He is sufficient for him.โ€</strong><br><span class='arabic'>ูˆูŽู…ูŽู† ูŠูŽุชูŽูˆูŽูƒู‘ูŽู„ู’ ุนูŽู„ูŽู‰ ุงู„ู„ู‘ูŽู‡ู ููŽู‡ููˆูŽ ุญูŽุณู’ุจูู‡ู</span><br><em>Qur'an, At-Talaq 65:3</em></p></div>",
53
- "hope": "<h1 class='emoji'>๐ŸŒค๏ธ</h1><div class='notion-card animated'><h3>Hope</h3><p><strong>โ€œAnd never give up hope of Allah's mercy.โ€</strong><br><span class='arabic'>ุฅูู†ู‘ูŽู‡ู ู„ูŽุง ูŠูŽูŠู’ุฃูŽุณู ู…ูู† ุฑู‘ูŽูˆู’ุญู ุงู„ู„ู‘ูŽู‡ู ุฅูู„ู‘ูŽุง ุงู„ู’ู‚ูŽูˆู’ู…ู ุงู„ู’ูƒูŽุงููุฑููˆู†ูŽ</span><br><em>Qur'an, Yusuf 12:87</em></p></div>",
54
- "remorse": "<h1 class='emoji'>๐Ÿ˜”</h1><div class='notion-card animated'><h3>Remorse</h3><p><strong>โ€œIndeed, Allah is Forgiving and Merciful.โ€</strong><br><span class='arabic'>ุฅูู†ู‘ูŽ ุงู„ู„ู‘ูŽู‡ูŽ ุบูŽูููˆุฑูŒ ุฑู‘ูŽุญููŠู…ูŒ</span><br><em>Qur'an, Al-Baqarah 2:218</em></p></div>",
55
- "caring": "<h1 class='emoji'>๐Ÿค—</h1><div class='notion-card animated'><h3>Caring</h3><p><strong>โ€œThe believers are but brothers, so make peace between your brothers.โ€</strong><br><span class='arabic'>ุฅูู†ู‘ูŽู…ูŽุง ุงู„ู’ู…ูุคู’ู…ูู†ููˆู†ูŽ ุฅูุฎู’ูˆูŽุฉูŒ</span><br><em>Qur'an, Al-Hujurat 49:10</em></p></div>",
56
- "surprise": "<h1 class='emoji'>๐Ÿ˜ฒ</h1><div class='notion-card animated'><h3>Surprise</h3><p><strong>โ€œAllah creates what you do not know.โ€</strong><br><span class='arabic'>ูˆูŽูŠูŽุฎู’ู„ูู‚ู ู…ูŽุง ู„ูŽุง ุชูŽุนู’ู„ูŽู…ููˆู†ูŽ</span><br><em>Qur'an, An-Nahl 16:8</em></p></div>",
57
- "embarrassment": "<h1 class='emoji'>๐Ÿ˜ณ</h1><div class='notion-card animated'><h3>Embarrassment</h3><p><strong>โ€œIndeed, Allah is ever Accepting of repentance.โ€</strong><br><span class='arabic'>ุฅูู†ู‘ูŽู‡ู ูƒูŽุงู†ูŽ ุชูŽูˆู‘ูŽุงุจู‹ุง</span><br><em>Qur'an, An-Nisa 4:16</em></p></div>",
58
- "relief": "<h1 class='emoji'>๐Ÿ˜Œ</h1><div class='notion-card animated'><h3>Relief</h3><p><strong>โ€œAnd He it is who sent down tranquility into the hearts of the believers.โ€</strong><br><span class='arabic'>ู‡ููˆูŽ ุงู„ู‘ูŽุฐููŠ ุฃูŽู†ุฒูŽู„ูŽ ุงู„ุณู‘ูŽูƒููŠู†ูŽุฉูŽ ูููŠ ู‚ูู„ููˆุจู ุงู„ู’ู…ูุคู’ู…ูู†ููŠู†ูŽ</span><br><em>Qur'an, Al-Fath 48:4</em></p></div>",
59
- "neutral": "<h1 class='emoji'>๐Ÿ˜</h1><div class='notion-card animated'><h3>Neutral</h3><p><strong>May Allah always guide your heart in every situation.</strong></p></div>"
60
- }
61
 
62
- def analyze_combined(text, threshold, image):
63
- # Text analysis
64
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
65
  with torch.no_grad():
66
- logits_text = model(**inputs).logits
67
- probs_text = sigmoid(logits_text)[0]
68
- text_idx = torch.argmax(probs_text).item()
69
- text_label = model.config.id2label[text_idx].lower()
70
-
71
- text_card = islamic_advice.get(text_label)
72
- if not text_card:
73
- text_card = get_gemini_advice_from_text(text)
74
-
75
- # Image analysis
76
- image_card = ""
77
- if image:
78
- inputs_image = image_processor(images=image, return_tensors="pt")
79
- with torch.no_grad():
80
- logits_img = image_model(**inputs_image).logits
81
- probs_img = torch.nn.functional.softmax(logits_img, dim=1)[0]
82
- img_idx = torch.argmax(probs_img).item()
83
- img_label = image_model.config.id2label[img_idx].lower()
84
- image_card = islamic_advice.get(img_label, f"<div class='notion-card fade-in'><h3>{img_label.capitalize()}</h3><p>May Allah guide your expression.</p></div>")
85
-
86
- return f"""
87
- <div class='notion-card fade-in slide-up glow-card'>
88
- <h2 style='text-align:center'>๐Ÿ“ From Text Analysis</h2>
89
- {text_card}
90
- </div>
91
-
92
- {f"<div class='notion-card fade-in slide-up glow-card'><h2 style='text-align:center'>๐Ÿ–ผ๏ธ From Image Analysis</h2>{image_card}</div>" if image else ""}
93
- """
94
-
95
- custom_css = """
96
- @keyframes slideInUp {
97
- from {
98
- transform: translateY(20px);
99
- opacity: 0;
100
- }
101
- to {
102
- transform: translateY(0);
103
- opacity: 1;
104
- }
105
- }
106
- @keyframes glowFadeIn {
107
- 0% {
108
- box-shadow: 0 0 0px rgba(0, 0, 0, 0);
109
- opacity: 0;
110
- }
111
- 100% {
112
- box-shadow: 0 0 15px rgba(59, 130, 246, 0.3);
113
- opacity: 1;
114
- }
115
- }
116
- .fade-in {
117
- animation: fadeInPop 0.6s ease-out both;
118
- }
119
- .slide-up {
120
- animation: slideInUp 0.6s ease-out both;
121
- }
122
- .glow-card {
123
- animation: glowFadeIn 1s ease-in-out both;
124
- }
125
- @keyframes fadeInPop {
126
- 0% {
127
- opacity: 0;
128
- transform: scale(0.95);
129
- }
130
- 100% {
131
- opacity: 1;
132
- transform: scale(1);
133
- }
134
- }
135
- .fade-in {
136
- animation: fadeInPop 0.6s ease-out both;
137
- }
138
- body {
139
- background: #f9fafb;
140
- font-family: 'Inter', sans-serif;
141
- color: #1f2937;
142
- padding: 20px;
143
- }
144
- .gr-button {
145
- background-color: #3b82f6 !important;
146
- color: white !important;
147
- border-radius: 8px !important;
148
- font-weight: 600;
149
- padding: 10px 20px;
150
- transition: background-color 0.3s ease;
151
- }
152
- .gr-button:hover {
153
- background-color: #2563eb !important;
154
- }
155
- """
156
-
157
- with gr.Blocks(css=custom_css) as demo:
158
- gr.Markdown("# ๐Ÿง  EmotionLens")
159
- gr.Markdown("Analyze your text and optionally a facial photo. Receive emotional insight and reflective Islamic advice.")
160
-
161
- with gr.Tab("Single Input"):
162
- text_input = gr.Textbox(lines=4, label="Text Input")
163
- threshold_slider = gr.Slider(0.1, 0.9, value=0.3, step=0.05, label="Threshold")
164
- image_input = gr.Image(type="pil", label="Upload Face Photo (optional)")
165
- btn = gr.Button("Analyze Emotion")
166
- result = gr.HTML()
167
- btn.click(fn=analyze_combined, inputs=[text_input, threshold_slider, image_input], outputs=result)
168
-
169
-
170
- demo.launch()
 
1
  import gradio as gr
 
 
 
 
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoImageProcessor, AutoModelForImageClassification
3
  from torch.nn.functional import sigmoid
4
  import torch
5
  from PIL import Image
6
 
 
 
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Load text emotion model
10
  model_name = "SamLowe/roberta-base-go_emotions"
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
  model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
 
14
+ # Load image emotion model
15
  image_model_name = "Celal11/resnet-50-finetuned-FER2013-0.001"
16
  image_processor = AutoImageProcessor.from_pretrained(image_model_name)
17
  image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
18
 
19
+ # Analyze image emotion using processor and model
20
+ def analyze_image_emotion(image):
21
+ if image is None:
22
+ return "No image provided."
23
+ inputs = image_processor(images=image, return_tensors="pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
 
25
  with torch.no_grad():
26
+ logits = image_model(**inputs).logits
27
+ probs = torch.nn.functional.softmax(logits, dim=1)[0]
28
+ pred_idx = torch.argmax(probs).item()
29
+ label = image_model.config.id2label[pred_idx]
30
+ score = probs[pred_idx].item()
31
+ return f"{label} ({score:.2f})"
32
+
33
+ # Emotion label to icon mapping (subset)
34
+ emotion_icons = {
35
+ inputs=[
36
+ gr.Textbox(lines=5, placeholder="Write a sentence or a full paragraph...", label="Your Text"),
37
+ gr.Slider(minimum=0.1, maximum=0.9, value=0.3, step=0.05, label="Threshold"),
38
+ gr.Image(type="pil", label="Upload Face Photo")
39
+ ],
40
+ outputs=[
41
+ gr.Textbox(label="Detected Text Emotions", elem_classes=["output-textbox"]),
42
+ css=custom_css
43
+ )
44
+
45
+ demo.launch()