'localhost', 'dbname' => 'wevads', 'user' => 'admin', 'password' => WEVADS_DB_PASS ]; try { $pdo = new PDO( "pgsql:host={$db_config['host']};dbname={$db_config['dbname']}", $db_config['user'], $db_config['password'], [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 30 ] ); $pdo->exec("SET search_path TO admin, public;"); // Récupérer la méthode HTTP $method = $_SERVER['REQUEST_METHOD']; // Routes API $path = $_GET['action'] ?? 'list'; switch ($path) { case 'list': if ($method === 'GET') { $stmt = $pdo->query(" SELECT m.*, COUNT(DISTINCT p.id) as prediction_count, COUNT(DISTINCT t.id) as training_count, COALESCE(AVG(e.accuracy), 0) as avg_evaluation_score FROM admin.deepseek_models m LEFT JOIN admin.deepseek_predictions p ON m.id = p.model_id LEFT JOIN admin.deepseek_training_jobs t ON m.id = t.model_id LEFT JOIN admin.deepseek_evaluations e ON m.id = e.model_id GROUP BY m.id ORDER BY m.created_at DESC "); $models = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode([ 'status' => 'success', 'data' => $models, 'count' => count($models), 'timestamp' => date('c') ]); } break; case 'create': if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true); $stmt = $pdo->prepare(" INSERT INTO admin.deepseek_models (name, version, model_type, accuracy, training_data_size, config) VALUES (:name, :version, :model_type, :accuracy, :training_size, :config) RETURNING id "); $stmt->execute([ ':name' => $input['name'], ':version' => $input['version'] ?? '1.0.0', ':model_type' => $input['model_type'], ':accuracy' => $input['accuracy'] ?? null, ':training_size' => $input['training_data_size'] ?? 0, ':config' => json_encode($input['config'] ?? []) ]); $modelId = $stmt->fetchColumn(); echo json_encode([ 'status' => 'success', 'message' => 'Modèle créé avec succès', 'model_id' => $modelId, 'timestamp' => date('c') ]); } break; case 'predict': if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true); $modelId = $input['model_id'] ?? 1; // Simulation de prédiction (à remplacer par l'appel réel au modèle) $predictionResult = [ 'predicted_class' => 'inbox', 'confidence' => rand(85, 99) / 100, 'features_used' => count($input['features'] ?? []), 'explanation' => 'Modèle basé sur 1.5M emails d\'entraînement' ]; $stmt = $pdo->prepare(" INSERT INTO admin.deepseek_predictions (model_id, input_data, prediction_result, confidence, execution_time_ms) VALUES (:model_id, :input_data, :prediction_result, :confidence, :execution_time) RETURNING id "); $stmt->execute([ ':model_id' => $modelId, ':input_data' => json_encode($input['data'] ?? []), ':prediction_result' => json_encode($predictionResult), ':confidence' => $predictionResult['confidence'], ':execution_time' => rand(50, 500) ]); $predictionId = $stmt->fetchColumn(); // Log de l'inférence $pdo->prepare(" INSERT INTO admin.deepseek_inference_logs (model_id, prediction_id, response_time_ms, input_hash) VALUES (:model_id, :prediction_id, :response_time, :input_hash) ")->execute([ ':model_id' => $modelId, ':prediction_id' => $predictionId, ':response_time' => rand(50, 500), ':input_hash' => hash('sha256', json_encode($input)) ]); echo json_encode([ 'status' => 'success', 'prediction_id' => $predictionId, 'result' => $predictionResult, 'model_used' => 'Inbox Predictor v1.2.0', 'inference_time_ms' => rand(50, 500), 'timestamp' => date('c') ]); } break; case 'stats': if ($method === 'GET') { $stats = $pdo->query(" SELECT (SELECT COUNT(*) FROM admin.deepseek_models WHERE status = 'active') as active_models, (SELECT COUNT(*) FROM admin.deepseek_predictions) as total_predictions, (SELECT ROUND(AVG(accuracy), 2) FROM admin.deepseek_models) as avg_model_accuracy, (SELECT COUNT(DISTINCT model_id) FROM admin.deepseek_training_jobs WHERE status = 'completed') as trained_models, (SELECT COUNT(*) FROM admin.deepseek_datasets) as total_datasets, (SELECT SUM(total_records) FROM admin.deepseek_datasets) as total_data_points ")->fetch(PDO::FETCH_ASSOC); // Dernières prédictions $recent = $pdo->query(" SELECT p.*, m.name as model_name FROM admin.deepseek_predictions p JOIN admin.deepseek_models m ON p.model_id = m.id ORDER BY p.created_at DESC LIMIT 10 ")->fetchAll(PDO::FETCH_ASSOC); echo json_encode([ 'status' => 'success', 'statistics' => $stats, 'recent_predictions' => $recent, 'timestamp' => date('c') ]); } break; default: echo json_encode([ 'status' => 'error', 'message' => 'Action non reconnue', 'available_actions' => [ 'GET /api/deepseek-models.php?action=list' => 'Lister tous les modèles', 'POST /api/deepseek-models.php?action=create' => 'Créer un nouveau modèle', 'POST /api/deepseek-models.php?action=predict' => 'Faire une prédiction', 'GET /api/deepseek-models.php?action=stats' => 'Statistiques du système' ] ]); } } catch (PDOException $e) { echo json_encode([ 'status' => 'error', 'message' => 'Erreur base de données: ' . $e->getMessage(), 'fallback_data' => [ 'status' => 'fallback', 'models' => [ ['id' => 1, 'name' => 'Inbox Predictor', 'accuracy' => 94.5], ['id' => 2, 'name' => 'Spam Detector', 'accuracy' => 98.7] ] ] ]); } ?>