hrlima commited on
Commit
2bcf2cc
·
verified ·
1 Parent(s): 07c88ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -92,24 +92,28 @@ def analyze():
92
 
93
  text = data["text"]
94
 
95
- # Chama o modelo de emoção do Hugging Face
96
- response = client.text_classification(
97
- text,
98
- model="pysentimiento/robertuito-emotion-analysis"
 
 
 
99
  )
100
 
101
- if not response or len(response) == 0:
102
- raise ValueError("Resposta vazia da API do Hugging Face")
 
 
103
 
104
- # Mapeia e organiza as probabilidades
105
  probabilities = {}
106
  for item in response:
107
- label = item["label"]
108
- score = round(float(item["score"]), 4)
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
- # Armazena no Firestore
 
 
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
  # =============================