Spaces:
Sleeping
Sleeping
File size: 6,525 Bytes
0e7f61c | 1 2 3 4 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | import os
import cv2
import torch
import numpy as np
from flask import Flask, jsonify, request
from flask_cors import CORS
from torchvision import transforms
from dotenv import load_dotenv
from timm import create_model
from groq import Groq
import time
from pymongo import MongoClient
from helpers import get_random_joke, get_one_fact
from trivia import fetch_questionnaire, score_assessment, interpret_score
load_dotenv()
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "*"}})
MONGO_URI = os.getenv("MONGO_URI")
client = MongoClient(MONGO_URI)
db = client["mood_monitor"]
sessions_col = db["sessions"]
print("β
Connected to MongoDB Atlas")
MODEL_PATH = "best_fer2013_model_70.pth"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
NUM_CLASSES = 7
EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
print(f"βοΈ Using device: {DEVICE}")
MODEL_LOADED = False
last_face_time = 0
try:
print("Loading model...")
model = create_model("efficientnet_b0", pretrained=False, num_classes=NUM_CLASSES)
model.load_state_dict(torch.load(MODEL_PATH, map_location=DEVICE))
model = model.to(DEVICE)
model.eval()
MODEL_LOADED = True
print("β
Model loaded")
except Exception as e:
print("β Model load error:", e)
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
groq_client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Grayscale(num_output_channels=3),
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
def detect_emotion_from_frame(face_img):
if not MODEL_LOADED:
return "Neutral", 0.0
try:
face_tensor = transform(face_img).unsqueeze(0).to(DEVICE)
with torch.no_grad():
output = model(face_tensor)
probs = torch.nn.functional.softmax(output, dim=1)
conf, idx = torch.max(probs, 1)
return EMOTION_LABELS[idx.item()], conf.item()
except Exception as e:
print("Inference error:", e)
return "Neutral", 0.0
@app.route("/api/health")
def health():
return jsonify({"status": "ok", "model_loaded": MODEL_LOADED})
@app.route("/api/analyze-frame", methods=["POST"])
def analyze_frame():
global last_face_time
if "image" not in request.files:
return jsonify({"error": "No image"}), 400
file = request.files["image"]
img_bytes = np.frombuffer(file.read(), np.uint8)
frame = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
if frame is None:
return jsonify({"error": "Bad image"}), 400
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
faces = face_cascade.detectMultiScale(gray, 1.1, 4, minSize=(30, 30))
if len(faces) == 0:
if time.time() - last_face_time < 1.5:
return jsonify({"face_detected": True, "dominant_emotion": "", "emotions": {}})
return jsonify({"face_detected": False, "dominant_emotion": "", "emotions": {}})
last_face_time = time.time()
x, y, w, h = max(faces, key=lambda r: r[2] * r[3])
face_roi = frame[y:y+h, x:x+w]
emotion, confidence = detect_emotion_from_frame(face_roi)
return jsonify({
"face_detected": True,
"dominant_emotion": emotion,
"emotions": { emotion.lower(): float(confidence) }
})
@app.route("/api/session/save", methods=["POST"])
def save_session():
try:
data = request.get_json(force=True)
print("π₯ Received session data:", data)
user_id = data.get("user_id")
if not user_id:
return jsonify({"error": "Missing user_id"}), 400
if not MONGO_URI or not client:
return jsonify({"error": "Database not configured"}), 500
session_doc = {
"user_id": str(user_id),
"timestamp": time.time(),
"duration": int(data.get("duration", 0)),
"emotions": data.get("emotions", {}),
"dominant_emotion": data.get("dominant_emotion", ""),
}
print("π Inserting into MongoDB:", session_doc)
result = sessions_col.insert_one(session_doc)
if not result.inserted_id:
return jsonify({"error": "Failed to insert document"}), 500
return jsonify({"success": True, "id": str(result.inserted_id)})
except Exception as e:
print("β SAVE SESSION ERROR:", str(e))
import traceback
traceback.print_exc()
return jsonify({"error": str(e)}), 500
@app.route("/api/session/list")
def list_sessions():
user_id = request.args.get("user_id")
if not user_id:
return jsonify({"error": "Missing user_id"}), 400
sessions = list(sessions_col.find({"user_id": str(user_id)}, {"_id": 0}))
return jsonify(sessions)
@app.route("/api/analytics")
def analytics():
user_id = request.args.get("user_id")
if not user_id:
return jsonify({"error": "Missing user_id"}), 400
sessions = list(sessions_col.find({"user_id": str(user_id)}))
if not sessions:
return jsonify({
"total_sessions": 0,
"emotion_distribution": {},
"avg_duration": 0
})
emotion_counts = {}
total_duration = 0
for s in sessions:
total_duration += s.get("duration", 0)
emo = s.get("dominant_emotion")
if emo:
emotion_counts[emo] = emotion_counts.get(emo, 0) + 1
return jsonify({
"total_sessions": len(sessions),
"emotion_distribution": emotion_counts,
"avg_duration": round(total_duration / len(sessions), 1)
})
@app.route("/api/joke")
def joke():
return jsonify({"joke": get_random_joke()})
@app.route("/api/fact")
def fact():
return jsonify({"fact": get_one_fact(os.getenv("API_NINJAS_KEY"))})
@app.route("/api/grok", methods=["POST"])
def grok():
data = request.json
question = data.get("question")
if not groq_client:
return jsonify({"reply": "AI not configured"})
completion = groq_client.chat.completions.create(
messages=[{"role": "user", "content": question}],
model="llama-3.3-70b-versatile"
)
return jsonify({"reply": completion.choices[0].message.content})
if __name__ == "__main__":
port = int(os.environ.get("PORT", 7860))
app.run(host="0.0.0.0", port=port) |