Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,9 @@ if not HF_TOKEN:
|
|
| 34 |
|
| 35 |
client = InferenceClient(token=HF_TOKEN)
|
| 36 |
|
| 37 |
-
#
|
|
|
|
|
|
|
| 38 |
EMOTION_MAP = {
|
| 39 |
"joy": "alegria",
|
| 40 |
"sadness": "tristeza",
|
|
@@ -43,27 +45,22 @@ EMOTION_MAP = {
|
|
| 43 |
"surprise": "surpresa",
|
| 44 |
"disgust": "desgosto",
|
| 45 |
"neutral": "neutro",
|
| 46 |
-
"depression": "depressão"
|
| 47 |
}
|
| 48 |
|
| 49 |
-
# Sugestões simples conforme emoção
|
| 50 |
SUGESTOES = {
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"raiva": "
|
| 54 |
-
"
|
|
|
|
| 55 |
"surpresa": "Nem tudo é previsível — abrace o inesperado.",
|
| 56 |
-
"desgosto": "
|
| 57 |
-
"
|
| 58 |
-
"neutro": "Você parece equilibrado no momento."
|
| 59 |
}
|
| 60 |
|
| 61 |
-
@app.route("/")
|
| 62 |
-
def index():
|
| 63 |
-
return jsonify({"status": "online", "message": "API Flask funcionando!"})
|
| 64 |
-
|
| 65 |
# =============================
|
| 66 |
-
# Função
|
| 67 |
# =============================
|
| 68 |
def normalizar_probabilidades(results):
|
| 69 |
total = sum([r["score"] for r in results])
|
|
@@ -85,29 +82,35 @@ def analyze():
|
|
| 85 |
|
| 86 |
text = data["text"]
|
| 87 |
|
| 88 |
-
#
|
| 89 |
response = client.text_classification(
|
| 90 |
text,
|
| 91 |
model="SamLowe/roberta-base-go_emotions"
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Normaliza scores
|
| 95 |
results = normalizar_probabilidades(response)
|
| 96 |
|
| 97 |
-
# Monta dicionário de probabilidades traduzidas
|
| 98 |
probabilidades = {}
|
| 99 |
for item in results:
|
| 100 |
label = item["label"].lower()
|
| 101 |
score = round(item["score"], 4)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
if label in ["sadness", "grief", "hopelessness"]:
|
| 105 |
label = "depression"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
label_pt = EMOTION_MAP.get(label, "neutro")
|
| 108 |
-
probabilidades[label_pt] = score
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
# Define
|
| 111 |
principal = max(probabilidades, key=probabilidades.get)
|
| 112 |
sugestao = SUGESTOES.get(principal, "Mantenha o equilíbrio emocional.")
|
| 113 |
|
|
@@ -119,7 +122,7 @@ def analyze():
|
|
| 119 |
"suggestion": sugestao
|
| 120 |
}
|
| 121 |
|
| 122 |
-
#
|
| 123 |
if db:
|
| 124 |
db.collection("emotions").add({
|
| 125 |
"text": text,
|
|
@@ -135,7 +138,7 @@ def analyze():
|
|
| 135 |
"status": "fallback",
|
| 136 |
"emotion": "neutro",
|
| 137 |
"emode": ["neutro"],
|
| 138 |
-
"suggestion": "
|
| 139 |
"debug": str(e)
|
| 140 |
}), 500
|
| 141 |
|
|
|
|
| 34 |
|
| 35 |
client = InferenceClient(token=HF_TOKEN)
|
| 36 |
|
| 37 |
+
# =============================
|
| 38 |
+
# Mapeamento de emoções e sugestões
|
| 39 |
+
# =============================
|
| 40 |
EMOTION_MAP = {
|
| 41 |
"joy": "alegria",
|
| 42 |
"sadness": "tristeza",
|
|
|
|
| 45 |
"surprise": "surpresa",
|
| 46 |
"disgust": "desgosto",
|
| 47 |
"neutral": "neutro",
|
| 48 |
+
"depression": "depressão"
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
SUGESTOES = {
|
| 52 |
+
"tristeza": "Você parece triste. Tente conversar com alguém ou fazer algo que te acalme.",
|
| 53 |
+
"depressão": "Sinais de depressão detectados. Considere buscar apoio emocional ou profissional.",
|
| 54 |
+
"raiva": "Você demonstra raiva. Tente respirar fundo e dar um tempo antes de reagir.",
|
| 55 |
+
"alegria": "Continue fazendo o que te traz alegria!",
|
| 56 |
+
"medo": "Tente identificar a causa do medo e se apoiar em algo seguro.",
|
| 57 |
"surpresa": "Nem tudo é previsível — abrace o inesperado.",
|
| 58 |
+
"desgosto": "Talvez seja bom mudar o foco e pensar em algo que te faz bem.",
|
| 59 |
+
"neutro": "Você parece tranquilo no momento."
|
|
|
|
| 60 |
}
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# =============================
|
| 63 |
+
# Função auxiliar
|
| 64 |
# =============================
|
| 65 |
def normalizar_probabilidades(results):
|
| 66 |
total = sum([r["score"] for r in results])
|
|
|
|
| 82 |
|
| 83 |
text = data["text"]
|
| 84 |
|
| 85 |
+
# Análise via modelo HF
|
| 86 |
response = client.text_classification(
|
| 87 |
text,
|
| 88 |
model="SamLowe/roberta-base-go_emotions"
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
results = normalizar_probabilidades(response)
|
| 92 |
|
|
|
|
| 93 |
probabilidades = {}
|
| 94 |
for item in results:
|
| 95 |
label = item["label"].lower()
|
| 96 |
score = round(item["score"], 4)
|
| 97 |
|
| 98 |
+
# Conversão e agrupamento manual
|
| 99 |
if label in ["sadness", "grief", "hopelessness"]:
|
| 100 |
label = "depression"
|
| 101 |
+
elif label == "anger":
|
| 102 |
+
label = "anger"
|
| 103 |
+
elif label == "joy":
|
| 104 |
+
label = "joy"
|
| 105 |
|
| 106 |
label_pt = EMOTION_MAP.get(label, "neutro")
|
| 107 |
+
probabilidades[label_pt] = probabilidades.get(label_pt, 0) + score
|
| 108 |
+
|
| 109 |
+
# Ajuste fino — se detectar tristeza forte, amplia depressão
|
| 110 |
+
if probabilidades.get("tristeza", 0) > 0.4:
|
| 111 |
+
probabilidades["depressão"] = min(1.0, probabilidades.get("depressão", 0) + 0.3)
|
| 112 |
|
| 113 |
+
# Define principal emoção
|
| 114 |
principal = max(probabilidades, key=probabilidades.get)
|
| 115 |
sugestao = SUGESTOES.get(principal, "Mantenha o equilíbrio emocional.")
|
| 116 |
|
|
|
|
| 122 |
"suggestion": sugestao
|
| 123 |
}
|
| 124 |
|
| 125 |
+
# Grava no Firestore
|
| 126 |
if db:
|
| 127 |
db.collection("emotions").add({
|
| 128 |
"text": text,
|
|
|
|
| 138 |
"status": "fallback",
|
| 139 |
"emotion": "neutro",
|
| 140 |
"emode": ["neutro"],
|
| 141 |
+
"suggestion": "Falha na análise. Fallback ativado.",
|
| 142 |
"debug": str(e)
|
| 143 |
}), 500
|
| 144 |
|