File size: 9,860 Bytes
23e46f5 452c0cc 8942940 49238a6 89891d1 23e46f5 4aa2d44 23e46f5 8942940 2baa38f 8942940 2baa38f 8942940 2baa38f 8942940 2baa38f 8942940 2baa38f 8942940 2baa38f 8942940 8d812a8 23e46f5 49238a6 4aa2d44 69f23c0 a1860ca 8942940 a1860ca 69f23c0 aeb36c3 2f2ec99 89891d1 aeb36c3 2f2ec99 69f23c0 2f2ec99 aeb36c3 2f2ec99 89891d1 2f2ec99 452c0cc 23e46f5 2baa38f 23e46f5 5cad09b 23e46f5 5cad09b 23e46f5 5cad09b a1860ca 23e46f5 5cad09b 23e46f5 452c0cc 2baa38f 452c0cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import gradio as gr
import pandas as pd
import os
from dotenv import load_dotenv
import google.generativeai as genai
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoImageProcessor, AutoModelForImageClassification
from torch.nn.functional import sigmoid
import torch
from PIL import Image
load_dotenv()
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
model_gemini = genai.GenerativeModel("gemini-pro")
def get_gemini_advice_from_text(text):
prompt = f"""Suggest one Qur'an verse or Hadith to comfort and guide a person who wrote the following:
'{text}'
Please include:
- Arabic verse or hadith
- English translation
- Source (Surah or Hadith reference)
Wrap it in a gentle tone."""
try:
response = model_gemini.generate_content(prompt)
return f"""<div class='notion-card fade-in'>
<p style='white-space: pre-wrap;'>{response.text}</p>
</div>"""
except Exception as e:
return f"<div class='notion-card fade-in'><p>May Allah guide your heart.</p></div>"
model_name = "SamLowe/roberta-base-go_emotions"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
image_model_name = "Celal11/resnet-50-finetuned-FER2013-0.001"
image_processor = AutoImageProcessor.from_pretrained(image_model_name)
image_model = AutoModelForImageClassification.from_pretrained(image_model_name)
islamic_advice = {
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>",
"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>"
}
def analyze_combined(text, threshold, image):
# Text analysis
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
logits_text = model(**inputs).logits
probs_text = sigmoid(logits_text)[0]
text_idx = torch.argmax(probs_text).item()
text_label = model.config.id2label[text_idx].lower()
text_card = islamic_advice.get(text_label)
if not text_card:
text_card = get_gemini_advice_from_text(text)
# Image analysis
image_card = ""
if image:
inputs_image = image_processor(images=image, return_tensors="pt")
with torch.no_grad():
logits_img = image_model(**inputs_image).logits
probs_img = torch.nn.functional.softmax(logits_img, dim=1)[0]
img_idx = torch.argmax(probs_img).item()
img_label = image_model.config.id2label[img_idx].lower()
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>")
return f"""
<div class='notion-card fade-in slide-up glow-card'>
<h2 style='text-align:center'>๐ From Text Analysis</h2>
{text_card}
</div>
{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 ""}
"""
custom_css = """
@keyframes slideInUp {
from {
transform: translateY(20px);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
@keyframes glowFadeIn {
0% {
box-shadow: 0 0 0px rgba(0, 0, 0, 0);
opacity: 0;
}
100% {
box-shadow: 0 0 15px rgba(59, 130, 246, 0.3);
opacity: 1;
}
}
.fade-in {
animation: fadeInPop 0.6s ease-out both;
}
.slide-up {
animation: slideInUp 0.6s ease-out both;
}
.glow-card {
animation: glowFadeIn 1s ease-in-out both;
}
@keyframes fadeInPop {
0% {
opacity: 0;
transform: scale(0.95);
}
100% {
opacity: 1;
transform: scale(1);
}
}
.fade-in {
animation: fadeInPop 0.6s ease-out both;
}
body {
background: #f9fafb;
font-family: 'Inter', sans-serif;
color: #1f2937;
padding: 20px;
}
.gr-button {
background-color: #3b82f6 !important;
color: white !important;
border-radius: 8px !important;
font-weight: 600;
padding: 10px 20px;
transition: background-color 0.3s ease;
}
.gr-button:hover {
background-color: #2563eb !important;
}
"""
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("# ๐ง EmotionLens")
gr.Markdown("Analyze your text and optionally a facial photo. Receive emotional insight and reflective Islamic advice.")
with gr.Tab("Single Input"):
text_input = gr.Textbox(lines=4, label="Text Input")
threshold_slider = gr.Slider(0.1, 0.9, value=0.3, step=0.05, label="Threshold")
image_input = gr.Image(type="pil", label="Upload Face Photo (optional)")
btn = gr.Button("Analyze Emotion")
result = gr.HTML()
btn.click(fn=analyze_combined, inputs=[text_input, threshold_slider, image_input], outputs=result)
demo.launch()
|