Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,10 +33,13 @@ else:
|
|
| 33 |
|
| 34 |
# --- 3. LOGIC ---
|
| 35 |
def analyze(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if not text or not text.strip():
|
| 37 |
return {}, "⚠️ Please type something first!"
|
| 38 |
-
if model is None:
|
| 39 |
-
return {}, "⚠️ Model files missing in this folder."
|
| 40 |
|
| 41 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 42 |
model.eval()
|
|
@@ -47,10 +50,19 @@ def analyze(text):
|
|
| 47 |
results = {}
|
| 48 |
detected_emotions = []
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
for i, label in enumerate(LABELS):
|
| 51 |
score = float(probs[i])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
results[label] = score
|
| 53 |
-
|
|
|
|
| 54 |
detected_emotions.append(label.upper())
|
| 55 |
|
| 56 |
if not detected_emotions:
|
|
|
|
| 33 |
|
| 34 |
# --- 3. LOGIC ---
|
| 35 |
def analyze(text):
|
| 36 |
+
# 1. Simple Check: Is the model variable loaded?
|
| 37 |
+
if model is None:
|
| 38 |
+
return {}, "⚠️ Model failed to load. Check logs."
|
| 39 |
+
|
| 40 |
+
# 2. Check for Empty Text
|
| 41 |
if not text or not text.strip():
|
| 42 |
return {}, "⚠️ Please type something first!"
|
|
|
|
|
|
|
| 43 |
|
| 44 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 45 |
model.eval()
|
|
|
|
| 50 |
results = {}
|
| 51 |
detected_emotions = []
|
| 52 |
|
| 53 |
+
# Penalties for "Sleep" bias
|
| 54 |
+
penalties = {"sleep": {"fear": 0.50}, "tired": {"fear": 0.40}, "hungry": {"fear": 0.40}}
|
| 55 |
+
text_lower = text.lower()
|
| 56 |
+
|
| 57 |
for i, label in enumerate(LABELS):
|
| 58 |
score = float(probs[i])
|
| 59 |
+
for word, penalty_map in penalties.items():
|
| 60 |
+
if word in text_lower and label in penalty_map:
|
| 61 |
+
score = max(0.0, score - penalty_map[label])
|
| 62 |
+
|
| 63 |
results[label] = score
|
| 64 |
+
# Use simple 0.5 threshold
|
| 65 |
+
if score > 0.5:
|
| 66 |
detected_emotions.append(label.upper())
|
| 67 |
|
| 68 |
if not detected_emotions:
|