fajarah commited on
Commit
8942940
ยท
verified ยท
1 Parent(s): 452c0cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -3
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>โ€œIndeed, those who have believed and done righteous deeds - the Most Merciful will appoint for them affection.โ€</strong><br><span class='arabic'>ุณูŽูŠูŽุฌู’ุนูŽู„ู ู„ูŽู‡ูู…ู ุงู„ุฑู‘ูŽุญู’ู…ูŽูฐู†ู ูˆูุฏู‘ู‹ุง</span><br><em>Qur'an, Maryam 19:96</em></p></div>",
 
 
 
 
 
 
 
 
 
 
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, islamic_advice["neutral"])
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)