fajarah commited on
Commit
2f2ec99
·
verified ·
1 Parent(s): 2baa38f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -60,13 +60,20 @@ islamic_advice = {
60
  }
61
 
62
  def analyze_combined(text, threshold, image):
 
63
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
64
  with torch.no_grad():
65
  logits_text = model(**inputs).logits
66
  probs_text = sigmoid(logits_text)[0]
67
  text_idx = torch.argmax(probs_text).item()
68
  text_label = model.config.id2label[text_idx].lower()
 
 
 
 
69
 
 
 
70
  if image:
71
  inputs_image = image_processor(images=image, return_tensors="pt")
72
  with torch.no_grad():
@@ -74,14 +81,16 @@ def analyze_combined(text, threshold, image):
74
  probs_img = torch.nn.functional.softmax(logits_img, dim=1)[0]
75
  img_idx = torch.argmax(probs_img).item()
76
  img_label = image_model.config.id2label[img_idx].lower()
77
- else:
78
- img_label = None
79
-
80
- final_label = text_label if img_label is None else img_label
81
- card = islamic_advice.get(final_label)
82
- if not card:
83
- card = get_gemini_advice_from_text(text)
84
- return card
 
 
85
 
86
  custom_css = """
87
  @keyframes slideInUp {
 
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
  logits_text = model(**inputs).logits
67
  probs_text = sigmoid(logits_text)[0]
68
  text_idx = torch.argmax(probs_text).item()
69
  text_label = model.config.id2label[text_idx].lower()
70
+
71
+ text_card = islamic_advice.get(text_label)
72
+ if not text_card:
73
+ text_card = get_gemini_advice_from_text(text)
74
 
75
+ # Image analysis
76
+ image_card = ""
77
  if image:
78
  inputs_image = image_processor(images=image, return_tensors="pt")
79
  with torch.no_grad():
 
81
  probs_img = torch.nn.functional.softmax(logits_img, dim=1)[0]
82
  img_idx = torch.argmax(probs_img).item()
83
  img_label = image_model.config.id2label[img_idx].lower()
84
+ 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>")
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 {