persee-tech commited on
Commit
c504f93
·
verified ·
1 Parent(s): b8cf921

Delete backend/main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +0 -175
backend/main.py DELETED
@@ -1,175 +0,0 @@
1
- import os
2
- import socketio
3
- import uvicorn
4
- from fastapi import FastAPI, HTTPException
5
- from fastapi.middleware.cors import CORSMiddleware
6
- import asyncio
7
- import base64
8
- import cv2
9
- import numpy as np
10
- import random
11
- from datetime import datetime
12
- from deepface import DeepFace
13
- from supabase import create_client, Client
14
-
15
- # --- CONFIGURATION SUPABASE (COMPATIBLE CLOUD & LOCAL) ---
16
- SUPABASE_URL = os.getenv("SUPABASE_URL", "https://gwjrwejdjpctizolfkcz.supabase.co")
17
- SUPABASE_KEY = os.getenv("SUPABASE_KEY", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imd3anJ3ZWpkanBjdGl6b2xma2N6Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc2OTA5ODEyNCwiZXhwIjoyMDg0Njc0MTI0fQ.EjU1DGTN-jrdkaC6nJWilFtYZgtu-NKjnfiMVMnHal0")
18
-
19
- try:
20
- supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
21
- print("☁️ Connecté à Supabase")
22
- except Exception as e:
23
- print(f"❌ Erreur Supabase : {e}")
24
-
25
- # --- CONFIGURATION SERVEUR (CORS FIXED) ---
26
- # C'est ici que la magie opère : cors_allowed_origins='*' autorise tout le monde
27
- sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
28
-
29
- app = FastAPI()
30
- # Middleware pour autoriser les requêtes API REST
31
- app.add_middleware(
32
- CORSMiddleware,
33
- allow_origins=["*"],
34
- allow_credentials=True,
35
- allow_methods=["*"],
36
- allow_headers=["*"],
37
- )
38
- socket_app = socketio.ASGIApp(sio, app)
39
-
40
- # --- API REST ---
41
- @app.get("/api/sessions")
42
- def get_sessions():
43
- response = supabase.table('sessions').select("*").order('id', desc=True).execute()
44
- return response.data
45
-
46
- @app.get("/api/sessions/{session_id}")
47
- def get_session_details(session_id: int):
48
- sess = supabase.table('sessions').select("*").eq('id', session_id).execute()
49
- if not sess.data: raise HTTPException(status_code=404, detail="Session introuvable")
50
- meas = supabase.table('measurements').select("*").eq('session_id', session_id).order('session_time', desc=False).execute()
51
- return {"info": sess.data[0], "data": meas.data}
52
-
53
- @app.delete("/api/sessions/{session_id}")
54
- def delete_session(session_id: int):
55
- try:
56
- supabase.table('sessions').delete().eq('id', session_id).execute()
57
- return {"message": "Supprimé"}
58
- except Exception as e:
59
- raise HTTPException(status_code=500, detail=str(e))
60
-
61
- # --- LOGIQUE MÉTIER ---
62
- def calculate_kpis(emotion):
63
- valence = 0.0; arousal = 0.0; noise = random.uniform(-0.05, 0.05)
64
- if emotion == "happy": valence = 0.8 + noise; arousal = 0.6 + noise
65
- elif emotion == "surprise": valence = 0.2 + noise; arousal = 0.9 + noise
66
- elif emotion in ["fear", "angry"]: valence = -0.7 + noise; arousal = 0.8 + noise
67
- elif emotion == "disgust": valence = -0.8 + noise; arousal = 0.5 + noise
68
- elif emotion == "sad": valence = -0.6 + noise; arousal = 0.2 + noise
69
- else: valence = 0.0 + noise; arousal = 0.3 + noise
70
-
71
- def clamp(n): return max(0, min(100, int(n)))
72
- val_eng = clamp((arousal * 100) + random.uniform(0, 5))
73
- val_sat = clamp(((valence + 1) / 2) * 100)
74
- val_tru = clamp(50 + (valence * 40) + random.uniform(0, 5)) if valence > 0 else clamp(50 - (abs(valence) * 40) + random.uniform(0, 5))
75
- val_loy = clamp((val_sat * 0.7) + (val_tru * 0.3))
76
- val_opi = val_sat
77
-
78
- # Labels
79
- if val_eng >= 75: lbl_eng = "Engagement Fort 🔥"
80
- elif val_eng >= 40: lbl_eng = "Engagement Moyen"
81
- else: lbl_eng = "Désengagement 💤"
82
-
83
- if val_sat >= 70: lbl_sat = "Très Satisfait 😃"
84
- elif val_sat >= 45: lbl_sat = "Neutre 😐"
85
- else: lbl_sat = "Insatisfait 😡"
86
-
87
- return {
88
- "engagement": val_eng, "satisfaction": val_sat, "trust": val_tru, "loyalty": val_loy, "opinion": val_opi,
89
- "lbl_eng": lbl_eng, "lbl_sat": lbl_sat
90
- }
91
-
92
- # --- GESTION WEBSOCKET ---
93
- camera_state = { "emotion": "neutral", "emotion_score": 0, "face_coords": None }
94
- active_sessions = {}
95
-
96
- @sio.event
97
- async def process_frame(sid, data_uri):
98
- try:
99
- encoded_data = data_uri.split(',')[1]
100
- nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
101
- frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
102
-
103
- result = DeepFace.analyze(frame, actions=['emotion'], enforce_detection=False, silent=True)
104
- data = result[0] if isinstance(result, list) else result
105
-
106
- camera_state["emotion"] = data['dominant_emotion']
107
- camera_state["emotion_score"] = data['emotion'][data['dominant_emotion']]
108
-
109
- region = data['region']
110
- if region['w'] > 0:
111
- camera_state["face_coords"] = {'x': region['x'], 'y': region['y'], 'w': region['w'], 'h': region['h']}
112
- else:
113
- camera_state["face_coords"] = None
114
- except:
115
- camera_state["face_coords"] = None
116
-
117
- async def session_manager_loop():
118
- while True:
119
- for sid, user_data in list(active_sessions.items()):
120
- kpis = calculate_kpis(camera_state["emotion"])
121
-
122
- if user_data["is_recording"]:
123
- user_data["session_time"] += 1
124
- if user_data["db_id"]:
125
- try:
126
- data_to_insert = {
127
- "session_id": user_data["db_id"],
128
- "session_time": user_data["session_time"],
129
- "emotion": camera_state["emotion"],
130
- "emotion_score": camera_state["emotion_score"],
131
- "engagement_val": kpis["engagement"], "engagement_lbl": kpis["lbl_eng"],
132
- "satisfaction_val": kpis["satisfaction"], "satisfaction_lbl": kpis["lbl_sat"],
133
- "trust_val": kpis["trust"], "loyalty_val": kpis["loyalty"], "opinion_val": kpis["opinion"]
134
- }
135
- supabase.table('measurements').insert(data_to_insert).execute()
136
- except Exception as e: print(f"DB Error: {e}")
137
-
138
- await sio.emit('metrics_update', {
139
- "emotion": camera_state["emotion"], "metrics": kpis,
140
- "face_coords": camera_state["face_coords"],
141
- "session_time": user_data["session_time"],
142
- "is_recording": user_data["is_recording"]
143
- }, room=sid)
144
- await asyncio.sleep(1)
145
-
146
- @sio.event
147
- async def connect(sid, environ):
148
- print(f"Client connecté: {sid}")
149
- active_sessions[sid] = { "is_recording": False, "session_time": 0, "db_id": None }
150
-
151
- @sio.event
152
- async def disconnect(sid):
153
- if sid in active_sessions: del active_sessions[sid]
154
-
155
- @sio.event
156
- async def start_session(sid, data):
157
- user_session = active_sessions.get(sid)
158
- if user_session:
159
- user_session["is_recording"] = True
160
- user_session["session_time"] = 0
161
- try:
162
- new_session = { "first_name": data.get('firstName'), "last_name": data.get('lastName'), "client_id": data.get('clientId') }
163
- res = supabase.table('sessions').insert(new_session).execute()
164
- user_session["db_id"] = res.data[0]['id']
165
- except Exception as e: print(f"Start Session Error: {e}")
166
-
167
- @sio.event
168
- async def stop_session(sid):
169
- if sid in active_sessions: active_sessions[sid]["is_recording"] = False
170
-
171
- if __name__ == "__main__":
172
- @app.on_event("startup")
173
- async def startup_event():
174
- asyncio.create_task(session_manager_loop())
175
- uvicorn.run(socket_app, host="0.0.0.0", port=7860)