Update app.py
Browse files
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
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 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|