Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
|
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoImageProcessor, AutoModelForImageClassification
|
4 |
from torch.nn.functional import sigmoid
|
5 |
import torch
|
6 |
from PIL import Image
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
model_name = "SamLowe/roberta-base-go_emotions"
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
@@ -19,7 +47,17 @@ islamic_advice = {
|
|
19 |
"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>",
|
20 |
"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>",
|
21 |
"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>",
|
22 |
-
"love": "<h1 class='emoji'>โค๏ธ</h1><div class='notion-card animated'><h3>Love</h3><p><strong>โ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"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>"
|
24 |
}
|
25 |
|
@@ -42,9 +80,8 @@ def analyze_combined(text, threshold, image):
|
|
42 |
img_label = None
|
43 |
|
44 |
final_label = text_label if img_label is None else img_label
|
45 |
-
card = islamic_advice.get(final_label
|
46 |
return card
|
47 |
-
return f"<h2 style='font-size:2rem;text-align:center;'>{final_label.capitalize()}</h2><p style='text-align:center;font-size:1.2rem;'>{advice}</p>"
|
48 |
|
49 |
def analyze_batch_csv(file):
|
50 |
df = pd.read_csv(file.name)
|
|
|
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(emotion):
|
16 |
+
prompt = f"""Give me one verse from the Qur'an or Hadith that is relevant to someone feeling {emotion}.
|
17 |
+
Format it like this:
|
18 |
+
|
19 |
+
Arabic:
|
20 |
+
[Verse Arabic]
|
21 |
+
|
22 |
+
English Translation:
|
23 |
+
[Translation]
|
24 |
+
|
25 |
+
Source:
|
26 |
+
[Surah or Hadith Reference]"""
|
27 |
+
try:
|
28 |
+
response = model_gemini.generate_content(prompt)
|
29 |
+
return f"""<div class='notion-card animated'>
|
30 |
+
<h3>{emotion.capitalize()}</h3>
|
31 |
+
<p style='white-space: pre-wrap;'>{response.text}</p>
|
32 |
+
</div>"""
|
33 |
+
except Exception as e:
|
34 |
+
return f"<div class='notion-card'><h3>{emotion.capitalize()}</h3><p>May Allah guide your heart. (AI fallback failed)</p></div>"
|
35 |
+
|
36 |
model_name = "SamLowe/roberta-base-go_emotions"
|
37 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
38 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
47 |
"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>",
|
48 |
"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>",
|
49 |
"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>",
|
50 |
+
"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>",
|
51 |
+
"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>",
|
52 |
+
"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>",
|
53 |
+
"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>",
|
54 |
+
"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>",
|
55 |
+
"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>",
|
56 |
+
"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>",
|
57 |
+
"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>",
|
58 |
+
"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>",
|
59 |
+
"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>",
|
60 |
+
"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>",
|
61 |
"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>"
|
62 |
}
|
63 |
|
|
|
80 |
img_label = None
|
81 |
|
82 |
final_label = text_label if img_label is None else img_label
|
83 |
+
card = islamic_advice.get(final_label) or get_gemini_advice(final_label)
|
84 |
return card
|
|
|
85 |
|
86 |
def analyze_batch_csv(file):
|
87 |
df = pd.read_csv(file.name)
|