Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,24 +92,28 @@ def analyze():
|
|
| 92 |
|
| 93 |
text = data["text"]
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
#
|
| 105 |
probabilities = {}
|
| 106 |
for item in response:
|
| 107 |
-
label = item
|
| 108 |
-
score = round(float(item
|
| 109 |
emotion_pt = map_emotion(label, score)
|
| 110 |
probabilities[emotion_pt] = score
|
| 111 |
|
| 112 |
-
# Identifica a emoção dominante
|
| 113 |
dominant_emotion = max(probabilities, key=probabilities.get)
|
| 114 |
suggestion = get_suggestion(dominant_emotion)
|
| 115 |
|
|
@@ -121,7 +125,9 @@ def analyze():
|
|
| 121 |
"suggestion": suggestion,
|
| 122 |
}
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
if db:
|
| 126 |
db.collection("emotions").add({
|
| 127 |
"text": text,
|
|
@@ -132,7 +138,7 @@ def analyze():
|
|
| 132 |
return jsonify(result)
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
-
print(f"❌ Erro na rota /analyze: {e}")
|
| 136 |
return jsonify({"error": str(e)}), 500
|
| 137 |
|
| 138 |
# =============================
|
|
|
|
| 92 |
|
| 93 |
text = data["text"]
|
| 94 |
|
| 95 |
+
print(f"\n🧠 Recebido texto para análise: {text}\n")
|
| 96 |
+
|
| 97 |
+
# Chamada segura via API genérica
|
| 98 |
+
response = client.post(
|
| 99 |
+
json={"inputs": text},
|
| 100 |
+
model="pysentimiento/robertuito-emotion-analysis",
|
| 101 |
+
task="text-classification"
|
| 102 |
)
|
| 103 |
|
| 104 |
+
print(f"📩 Resposta bruta do modelo:\n{json.dumps(response, indent=2, ensure_ascii=False)}\n")
|
| 105 |
+
|
| 106 |
+
if not isinstance(response, list) or len(response) == 0:
|
| 107 |
+
raise ValueError("Resposta inesperada da API do Hugging Face")
|
| 108 |
|
| 109 |
+
# Processa probabilidades
|
| 110 |
probabilities = {}
|
| 111 |
for item in response:
|
| 112 |
+
label = item.get("label", "").lower()
|
| 113 |
+
score = round(float(item.get("score", 0.0)), 4)
|
| 114 |
emotion_pt = map_emotion(label, score)
|
| 115 |
probabilities[emotion_pt] = score
|
| 116 |
|
|
|
|
| 117 |
dominant_emotion = max(probabilities, key=probabilities.get)
|
| 118 |
suggestion = get_suggestion(dominant_emotion)
|
| 119 |
|
|
|
|
| 125 |
"suggestion": suggestion,
|
| 126 |
}
|
| 127 |
|
| 128 |
+
print(f"✅ Resultado final: {json.dumps(result, indent=2, ensure_ascii=False)}\n")
|
| 129 |
+
|
| 130 |
+
# Salva no Firestore
|
| 131 |
if db:
|
| 132 |
db.collection("emotions").add({
|
| 133 |
"text": text,
|
|
|
|
| 138 |
return jsonify(result)
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
+
print(f"❌ Erro detalhado na rota /analyze: {e}")
|
| 142 |
return jsonify({"error": str(e)}), 500
|
| 143 |
|
| 144 |
# =============================
|