Spaces:
Running
Running
Create main.py
Browse files- backend/main.py +76 -0
backend/main.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import socketio
|
| 3 |
+
import uvicorn
|
| 4 |
+
from fastapi import FastAPI
|
| 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 deepface import DeepFace
|
| 12 |
+
from supabase import create_client, Client
|
| 13 |
+
|
| 14 |
+
# --- CONFIGURATION SUPABASE ---
|
| 15 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL", "https://gwjrwejdjpctizolfkcz.supabase.co")
|
| 16 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
if SUPABASE_KEY:
|
| 20 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 21 |
+
print("☁️ Connecté à Supabase")
|
| 22 |
+
else:
|
| 23 |
+
print("⚠️ Pas de clé Supabase")
|
| 24 |
+
except:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
# --- CONFIGURATION SOCKET.IO (LE BYPASS) ---
|
| 28 |
+
# Cette fonction force l'acceptation et imprime l'origine
|
| 29 |
+
def force_allow_origins(origin, environ, **kwargs):
|
| 30 |
+
print(f"🔍 ORIGIN DETECTED: {origin}") # Debug Log
|
| 31 |
+
return True # Force YES
|
| 32 |
+
|
| 33 |
+
sio = socketio.AsyncServer(
|
| 34 |
+
async_mode='asgi',
|
| 35 |
+
cors_allowed_origins=force_allow_origins, # On utilise la fonction
|
| 36 |
+
logger=True,
|
| 37 |
+
engineio_logger=True,
|
| 38 |
+
always_connect=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
app = FastAPI()
|
| 42 |
+
app.add_middleware(
|
| 43 |
+
CORSMiddleware,
|
| 44 |
+
allow_origins=["*"],
|
| 45 |
+
allow_credentials=True,
|
| 46 |
+
allow_methods=["*"],
|
| 47 |
+
allow_headers=["*"],
|
| 48 |
+
)
|
| 49 |
+
socket_app = socketio.ASGIApp(sio, app)
|
| 50 |
+
|
| 51 |
+
# --- ROUTES ---
|
| 52 |
+
@app.get("/")
|
| 53 |
+
def home(): return {"status": "ok"}
|
| 54 |
+
|
| 55 |
+
@app.get("/api/sessions")
|
| 56 |
+
def get_sessions():
|
| 57 |
+
try:
|
| 58 |
+
return supabase.table('sessions').select("*").order('id', desc=True).execute().data
|
| 59 |
+
except: return []
|
| 60 |
+
|
| 61 |
+
# --- SOCKET EVENTS ---
|
| 62 |
+
@sio.event
|
| 63 |
+
async def connect(sid, environ):
|
| 64 |
+
print(f"✅ CONNECTED: {sid}")
|
| 65 |
+
|
| 66 |
+
@sio.event
|
| 67 |
+
async def process_frame(sid, data):
|
| 68 |
+
# (Votre logique DeepFace simplifiée pour le test)
|
| 69 |
+
pass
|
| 70 |
+
|
| 71 |
+
@sio.event
|
| 72 |
+
async def start_session(sid, data):
|
| 73 |
+
print(f"▶️ START SESSION: {data}")
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
uvicorn.run(socket_app, host="0.0.0.0", port=7860)
|