cs751 / app.py
fajarah's picture
Update app.py
2f2ec99 verified
raw
history blame
9.86 kB
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()