Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from firebase_admin import credentials, firestore
|
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
-
# Inicialização do Flask
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
# ====== CONFIGURAÇÃO FIREBASE ======
|
|
@@ -31,7 +30,7 @@ if not HF_TOKEN:
|
|
| 31 |
|
| 32 |
client = InferenceClient(token=HF_TOKEN)
|
| 33 |
|
| 34 |
-
# ======
|
| 35 |
emotion_map = {
|
| 36 |
"tristeza": ["triste", "desanimado", "desmotivado", "abatido", "deprimido"],
|
| 37 |
"ansiedade": ["ansioso", "preocupado", "tenso", "nervoso", "agitado"],
|
|
@@ -40,7 +39,19 @@ emotion_map = {
|
|
| 40 |
"alegria": ["feliz", "contente", "animado", "satisfeito", "positivo"]
|
| 41 |
}
|
| 42 |
|
| 43 |
-
# ======
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def fallback_emotion(text):
|
| 45 |
text_lower = text.lower()
|
| 46 |
for emotion, keywords in emotion_map.items():
|
|
@@ -59,19 +70,7 @@ def fallback_emotion(text):
|
|
| 59 |
"suggestion": "Emoção não identificada com precisão."
|
| 60 |
}
|
| 61 |
|
| 62 |
-
# ======
|
| 63 |
-
def gerar_sugestao(emotion):
|
| 64 |
-
sugestoes = {
|
| 65 |
-
"tristeza": "Procure fazer algo que te acalme e traga conforto emocional.",
|
| 66 |
-
"ansiedade": "Respire fundo e faça pausas para relaxar. Técnicas de respiração podem ajudar.",
|
| 67 |
-
"insegurança": "Acredite no seu valor. Lembre-se das suas conquistas.",
|
| 68 |
-
"raiva": "Afaste-se um pouco da situação e respire antes de reagir.",
|
| 69 |
-
"alegria": "Continue aproveitando esse momento positivo e compartilhe com os outros!",
|
| 70 |
-
"desconhecido": "Emoção não identificada com precisão."
|
| 71 |
-
}
|
| 72 |
-
return sugestoes.get(emotion, "Mantenha o equilíbrio emocional e cuide de você mesmo.")
|
| 73 |
-
|
| 74 |
-
# ====== ROTA PRINCIPAL DE TESTE ======
|
| 75 |
@app.route("/")
|
| 76 |
def home():
|
| 77 |
return jsonify({"message": "API Flask funcionando no Space!", "status": "online"})
|
|
@@ -86,35 +85,32 @@ def analyze():
|
|
| 86 |
|
| 87 |
text = data["text"]
|
| 88 |
|
| 89 |
-
# Tenta IA do Hugging Face
|
| 90 |
try:
|
| 91 |
result = client.text_classification(text)
|
| 92 |
if result and isinstance(result, list) and len(result) > 0:
|
| 93 |
label = result[0].get("label", "").lower()
|
| 94 |
-
|
| 95 |
-
# Mapeamento IA → emoções suportadas
|
| 96 |
if label in emotion_map.keys():
|
| 97 |
emotion = label
|
| 98 |
else:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
return jsonify({
|
| 102 |
-
"status": "ok",
|
| 103 |
-
"emotion": emotion,
|
| 104 |
-
"emode": [emotion],
|
| 105 |
-
"suggestion": gerar_sugestao(emotion)
|
| 106 |
-
})
|
| 107 |
else:
|
| 108 |
-
# Fallback local
|
| 109 |
return jsonify(fallback_emotion(text))
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
-
print(f"⚠️ Erro
|
| 113 |
return jsonify(fallback_emotion(text))
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
return jsonify({"error": str(e)}), 500
|
| 117 |
|
| 118 |
-
# ====== EXECUÇÃO
|
| 119 |
if __name__ == "__main__":
|
| 120 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
|
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
# ====== CONFIGURAÇÃO FIREBASE ======
|
|
|
|
| 30 |
|
| 31 |
client = InferenceClient(token=HF_TOKEN)
|
| 32 |
|
| 33 |
+
# ====== EMOÇÕES SUPORTADAS ======
|
| 34 |
emotion_map = {
|
| 35 |
"tristeza": ["triste", "desanimado", "desmotivado", "abatido", "deprimido"],
|
| 36 |
"ansiedade": ["ansioso", "preocupado", "tenso", "nervoso", "agitado"],
|
|
|
|
| 39 |
"alegria": ["feliz", "contente", "animado", "satisfeito", "positivo"]
|
| 40 |
}
|
| 41 |
|
| 42 |
+
# ====== GERAR SUGESTÕES ======
|
| 43 |
+
def gerar_sugestao(emotion):
|
| 44 |
+
sugestoes = {
|
| 45 |
+
"tristeza": "Procure fazer algo que te acalme e traga conforto emocional.",
|
| 46 |
+
"ansiedade": "Respire fundo e faça pausas para relaxar. Técnicas de respiração podem ajudar.",
|
| 47 |
+
"insegurança": "Acredite no seu valor. Lembre-se das suas conquistas.",
|
| 48 |
+
"raiva": "Afaste-se um pouco da situação e respire antes de reagir.",
|
| 49 |
+
"alegria": "Continue aproveitando esse momento positivo e compartilhe com os outros!",
|
| 50 |
+
"desconhecido": "Emoção não identificada com precisão."
|
| 51 |
+
}
|
| 52 |
+
return sugestoes.get(emotion, "Mantenha o equilíbrio emocional e cuide de você mesmo.")
|
| 53 |
+
|
| 54 |
+
# ====== FALLBACK LOCAL ======
|
| 55 |
def fallback_emotion(text):
|
| 56 |
text_lower = text.lower()
|
| 57 |
for emotion, keywords in emotion_map.items():
|
|
|
|
| 70 |
"suggestion": "Emoção não identificada com precisão."
|
| 71 |
}
|
| 72 |
|
| 73 |
+
# ====== ROTA PRINCIPAL ======
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
@app.route("/")
|
| 75 |
def home():
|
| 76 |
return jsonify({"message": "API Flask funcionando no Space!", "status": "online"})
|
|
|
|
| 85 |
|
| 86 |
text = data["text"]
|
| 87 |
|
|
|
|
| 88 |
try:
|
| 89 |
result = client.text_classification(text)
|
| 90 |
if result and isinstance(result, list) and len(result) > 0:
|
| 91 |
label = result[0].get("label", "").lower()
|
|
|
|
|
|
|
| 92 |
if label in emotion_map.keys():
|
| 93 |
emotion = label
|
| 94 |
else:
|
| 95 |
+
# 🔁 Novo: força fallback se IA não reconheceu emoção suportada
|
| 96 |
+
return jsonify(fallback_emotion(text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
else:
|
|
|
|
| 98 |
return jsonify(fallback_emotion(text))
|
| 99 |
|
| 100 |
+
return jsonify({
|
| 101 |
+
"status": "ok",
|
| 102 |
+
"emotion": emotion,
|
| 103 |
+
"emode": [emotion],
|
| 104 |
+
"suggestion": gerar_sugestao(emotion)
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
except Exception as e:
|
| 108 |
+
print(f"⚠️ Erro IA principal: {e}")
|
| 109 |
return jsonify(fallback_emotion(text))
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
return jsonify({"error": str(e)}), 500
|
| 113 |
|
| 114 |
+
# ====== EXECUÇÃO ======
|
| 115 |
if __name__ == "__main__":
|
| 116 |
app.run(host="0.0.0.0", port=7860)
|