Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,73 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
import httpx
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
return {"error":
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
async def translate_to_english(text: str) -> str:
|
| 55 |
-
"""Traduz PT -> EN se necessário"""
|
| 56 |
-
result = await query_inference(TRANSLATION_MODEL, {"inputs": text})
|
| 57 |
-
if isinstance(result, list) and result and "translation_text" in result[0]:
|
| 58 |
-
return result[0]["translation_text"]
|
| 59 |
-
return text
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
async def classify_emotion(text: str) -> dict:
|
| 63 |
-
"""Classifica emoção e retorna sugestão"""
|
| 64 |
-
result = await query_inference(EMOTION_MODEL, {"inputs": text})
|
| 65 |
-
|
| 66 |
-
if isinstance(result, dict) and "error" in result:
|
| 67 |
-
return {
|
| 68 |
-
"emotion": "unknown",
|
| 69 |
-
"suggestion": "Não foi possível analisar a emoção.",
|
| 70 |
-
"debug": result,
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
if isinstance(result, list) and result:
|
| 74 |
-
top_label = result[0]["label"].lower()
|
| 75 |
-
suggestion = EMOTION_SUGGESTIONS.get(top_label, DEFAULT_SUGGESTION)
|
| 76 |
-
return {"emotion": top_label, "suggestion": suggestion, "raw": result}
|
| 77 |
-
|
| 78 |
-
return {"emotion": "unknown", "suggestion": "Não foi possível analisar a emoção."}
|
| 79 |
-
|
| 80 |
|
| 81 |
-
@app.post("/analyze")
|
| 82 |
-
async def analyze_text(input: TextInput):
|
| 83 |
-
"""Endpoint principal de análise"""
|
| 84 |
-
text = input.text.strip()
|
| 85 |
-
# Heurística simples para decidir se traduz
|
| 86 |
-
if any(c in text for c in "áéíóúãõâêôçà"):
|
| 87 |
-
text_en = await translate_to_english(text)
|
| 88 |
-
else:
|
| 89 |
-
text_en = text
|
| 90 |
-
return await classify_emotion(text_en)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
@app.get("/")
|
| 94 |
-
async def root():
|
| 95 |
-
"""Endpoint de saúde"""
|
| 96 |
-
return {"status": "ok", "message": "API de análise emocional rodando 🚀"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
from flask_cors import CORS
|
| 5 |
+
import firebase_admin
|
| 6 |
+
from firebase_admin import credentials, firestore
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# Carregar variáveis de ambiente
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
app = Flask(__name__)
|
| 13 |
+
CORS(app)
|
| 14 |
+
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# Firebase Setup
|
| 17 |
+
# -----------------------------
|
| 18 |
+
if not firebase_admin._apps:
|
| 19 |
+
cred = credentials.Certificate("firebase_key.json") # coloque a chave no container
|
| 20 |
+
firebase_admin.initialize_app(cred)
|
| 21 |
+
|
| 22 |
+
db = firestore.client()
|
| 23 |
+
|
| 24 |
+
# -----------------------------
|
| 25 |
+
# HuggingFace API
|
| 26 |
+
# -----------------------------
|
| 27 |
+
HF_API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/YOUR_MODEL")
|
| 28 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 29 |
+
|
| 30 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 31 |
+
|
| 32 |
+
@app.route("/")
|
| 33 |
+
def home():
|
| 34 |
+
return jsonify({"status": "ok", "message": "MindVoice API online!"})
|
| 35 |
+
|
| 36 |
+
@app.route("/analyze", methods=["POST"])
|
| 37 |
+
def analyze():
|
| 38 |
+
"""
|
| 39 |
+
Recebe um áudio ou texto e envia para HuggingFace.
|
| 40 |
+
Exemplo de payload:
|
| 41 |
+
{
|
| 42 |
+
"text": "Estou feliz hoje"
|
| 43 |
+
}
|
| 44 |
+
"""
|
| 45 |
try:
|
| 46 |
+
data = request.get_json()
|
| 47 |
+
if not data:
|
| 48 |
+
return jsonify({"error": "Payload vazio"}), 400
|
| 49 |
+
|
| 50 |
+
# Caso seja texto
|
| 51 |
+
if "text" in data:
|
| 52 |
+
payload = {"inputs": data["text"]}
|
| 53 |
+
else:
|
| 54 |
+
return jsonify({"error": "Faltando campo 'text'"}), 400
|
| 55 |
+
|
| 56 |
+
response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=60)
|
| 57 |
+
result = response.json()
|
| 58 |
+
|
| 59 |
+
# Salvar no Firestore
|
| 60 |
+
db.collection("insights").add({
|
| 61 |
+
"text": data["text"],
|
| 62 |
+
"result": result,
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
return jsonify({"analysis": result})
|
| 66 |
except Exception as e:
|
| 67 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
port = int(os.getenv("PORT", 8080))
|
| 72 |
+
app.run(host="0.0.0.0", port=port, debug=True)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|