Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,7 +23,11 @@ except Exception as e:
|
|
| 23 |
|
| 24 |
# ====== MODELO ======
|
| 25 |
try:
|
| 26 |
-
model_pipeline = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
print("✅ Modelo carregado com sucesso!")
|
| 28 |
except Exception as e:
|
| 29 |
print(f"❌ Erro ao carregar modelo: {e}")
|
|
@@ -68,7 +72,6 @@ def fallback_emotion(text):
|
|
| 68 |
|
| 69 |
text_lower = text.lower()
|
| 70 |
match_counts = {k: sum(1 for w in v if w in text_lower) for k, v in emotion_map.items()}
|
| 71 |
-
# Detecta a emoção com mais palavras-chave presentes
|
| 72 |
emotion = max(match_counts, key=match_counts.get)
|
| 73 |
if match_counts[emotion] == 0:
|
| 74 |
emotion = "neutro"
|
|
@@ -94,15 +97,17 @@ def analyze():
|
|
| 94 |
if not model_pipeline:
|
| 95 |
return jsonify(fallback_emotion(text))
|
| 96 |
|
| 97 |
-
result = model_pipeline(text
|
| 98 |
if not result or len(result) == 0:
|
| 99 |
return jsonify(fallback_emotion(text))
|
| 100 |
|
|
|
|
| 101 |
scores = {r["label"]: r["score"] for r in result[0]}
|
| 102 |
top_label = max(scores, key=scores.get)
|
| 103 |
confidence = round(scores[top_label], 2)
|
| 104 |
emotion_pt = emotion_labels.get(top_label, "desconhecido")
|
| 105 |
|
|
|
|
| 106 |
if emotion_pt == "tristeza" and confidence >= 0.9:
|
| 107 |
emotion_pt = "depressão"
|
| 108 |
|
|
|
|
| 23 |
|
| 24 |
# ====== MODELO ======
|
| 25 |
try:
|
| 26 |
+
model_pipeline = pipeline(
|
| 27 |
+
"text-classification",
|
| 28 |
+
model="pysentimiento/robertuito-emotion-analysis",
|
| 29 |
+
return_all_scores=True
|
| 30 |
+
)
|
| 31 |
print("✅ Modelo carregado com sucesso!")
|
| 32 |
except Exception as e:
|
| 33 |
print(f"❌ Erro ao carregar modelo: {e}")
|
|
|
|
| 72 |
|
| 73 |
text_lower = text.lower()
|
| 74 |
match_counts = {k: sum(1 for w in v if w in text_lower) for k, v in emotion_map.items()}
|
|
|
|
| 75 |
emotion = max(match_counts, key=match_counts.get)
|
| 76 |
if match_counts[emotion] == 0:
|
| 77 |
emotion = "neutro"
|
|
|
|
| 97 |
if not model_pipeline:
|
| 98 |
return jsonify(fallback_emotion(text))
|
| 99 |
|
| 100 |
+
result = model_pipeline(text)
|
| 101 |
if not result or len(result) == 0:
|
| 102 |
return jsonify(fallback_emotion(text))
|
| 103 |
|
| 104 |
+
# Extrai probabilidades e identifica maior confiança
|
| 105 |
scores = {r["label"]: r["score"] for r in result[0]}
|
| 106 |
top_label = max(scores, key=scores.get)
|
| 107 |
confidence = round(scores[top_label], 2)
|
| 108 |
emotion_pt = emotion_labels.get(top_label, "desconhecido")
|
| 109 |
|
| 110 |
+
# Caso confiança alta em tristeza, considera depressão
|
| 111 |
if emotion_pt == "tristeza" and confidence >= 0.9:
|
| 112 |
emotion_pt = "depressão"
|
| 113 |
|