Spaces:
Sleeping
Sleeping
File size: 8,810 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | 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 collections import Counter
from pymongo import MongoClient, ASCENDING
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"]
# Create indexes for performance
sessions_col.create_index([("user_id", ASCENDING)])
sessions_col.create_index([("timestamp", ASCENDING)])
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():
"""
Expects JSON body:
{
"user_id": "...",
"duration": 120,
"frames": [
{ "emotion": "Happy", "confidence": 0.82, "time": 1710000123 },
...
]
}
"""
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
frames = data.get("frames", [])
duration = int(data.get("duration", 0))
# --- Aggregate frames on the backend ---
if frames:
emotion_list = [f.get("emotion", "Neutral") for f in frames if f.get("emotion")]
counts = Counter(emotion_list)
dominant_emotion = counts.most_common(1)[0][0] if counts else "Neutral"
confidences = [f.get("confidence", 0.0) for f in frames if "confidence" in f]
avg_confidence = round(sum(confidences) / len(confidences), 4) if confidences else 0.0
# Normalised distribution (count per emotion)
emotion_distribution = dict(counts)
else:
dominant_emotion = "Neutral"
avg_confidence = 0.0
emotion_distribution = {}
session_doc = {
"user_id": str(user_id),
"timestamp": time.time(),
"duration": duration,
"dominant_emotion": dominant_emotion,
"emotion_distribution": emotion_distribution,
"avg_confidence": avg_confidence,
"total_frames": len(frames),
}
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}
).sort("timestamp", ASCENDING)
)
return jsonify(sessions)
@app.route("/api/analytics")
def analytics():
"""
Returns:
{
"total_sessions": 10,
"avg_duration": 95.4,
"emotion_distribution": { "Happy": 6, "Sad": 2, ... },
"trend": [
{ "day": "Mon", "Happy": 2, "Sad": 1 },
...
],
"avg_confidence": 0.74
}
"""
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,
"trend": [],
"avg_confidence": 0
})
emotion_counts = {}
total_duration = 0
total_confidence = 0.0
trend_map = {} # day-label -> { emotion: count }
DAY_LABELS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
for s in sessions:
total_duration += s.get("duration", 0)
total_confidence += s.get("avg_confidence", 0.0)
emo = s.get("dominant_emotion", "")
if emo:
emotion_counts[emo] = emotion_counts.get(emo, 0) + 1
ts = s.get("timestamp")
if ts:
import datetime
day_label = DAY_LABELS[datetime.datetime.fromtimestamp(ts).weekday() % 7]
if day_label not in trend_map:
trend_map[day_label] = {}
if emo:
trend_map[day_label][emo] = trend_map[day_label].get(emo, 0) + 1
trend = [{"day": day, **emotions} for day, emotions in trend_map.items()]
return jsonify({
"total_sessions": len(sessions),
"emotion_distribution": emotion_counts,
"avg_duration": round(total_duration / len(sessions), 1),
"avg_confidence": round(total_confidence / len(sessions), 4),
"trend": trend
})
@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) |