Fraud Detection Module

This commit is contained in:
Flatlogic Bot 2026-01-07 10:23:05 +00:00
parent 591e6e3c57
commit 196bdd9665
5 changed files with 356 additions and 145 deletions

19
README.md Normal file
View File

@ -0,0 +1,19 @@
# Credit Card Fraud Detection API
## Problem Statement
This project aims to predict whether a credit card transaction is fraudulent or legitimate in real-time. It provides a web interface to submit transaction data and receive a fraud probability score.
## Dataset
The model will be trained on the [Kaggle Credit Card Fraud Dataset](https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud).
## Model
The initial implementation uses a Logistic Regression model as a baseline.
## API Usage
* `/predict`: POST endpoint that accepts transaction data and returns a fraud probability.
* `/health`: GET endpoint to check service status.
## Future Improvements
- Implement more advanced models (Random Forest, XGBoost).
- Add user authentication.
- Deploy to a cloud environment.

172
assets/css/custom.css Normal file
View File

@ -0,0 +1,172 @@
/* General Body & Theme Variables */
:root {
--primary-color: #0d6efd;
--background-color: #f8f9fa;
--card-bg: #ffffff;
--text-color: #212529;
--border-color: #dee2e6;
--muted-text-color: #6c757d;
--hero-gradient: linear-gradient(45deg, #0d6efd, #6c757d);
--navbar-bg: #343a40;
}
[data-theme="dark"] {
--primary-color: #4dabf7;
--background-color: #121212;
--card-bg: #1e1e1e;
--text-color: #e9ecef;
--border-color: #495057;
--muted-text-color: #adb5bd;
--hero-gradient: linear-gradient(45deg, #1f3a93, #343a40);
--navbar-bg: #1e1e1e;
}
body {
background-color: var(--background-color);
color: var(--text-color);
transition: background-color 0.3s ease, color 0.3s ease;
}
/* Animations */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animated {
animation-duration: 0.8s;
animation-fill-mode: both;
animation-name: fadeInUp;
}
/* Navbar */
.navbar {
background-color: var(--navbar-bg) !important;
border-bottom: 1px solid var(--border-color);
transition: background-color 0.3s ease;
}
/* Theme Switcher */
.theme-switcher {
display: flex;
align-items: center;
}
.theme-switcher .form-check-input {
width: 40px;
height: 20px;
background-color: #555;
border: none;
cursor: pointer;
position: relative;
transition: background-color 0.3s ease;
}
.theme-switcher .form-check-input:checked {
background-color: var(--primary-color);
}
.theme-switcher .theme-icon {
margin-left: 8px;
color: #ffc107;
transition: color 0.3s ease;
}
/* Hero Section */
.hero {
background: var(--hero-gradient);
color: white;
padding: 4rem 2rem;
margin-bottom: 2rem;
border-radius: 0.5rem;
text-align: center;
animation-delay: 0.2s;
}
/* Card Styling */
.card {
background-color: var(--card-bg);
border: 1px solid var(--border-color);
border-radius: 0.5rem;
transition: background-color 0.3s ease, border-color 0.3s ease;
animation-delay: 0.4s;
}
.card-header {
background-color: transparent;
border-bottom: 1px solid var(--border-color);
padding: 1rem 1.25rem;
}
.card-header h5 {
color: var(--text-color);
}
.form-label {
color: var(--muted-text-color);
}
.form-control {
background-color: var(--background-color);
color: var(--text-color);
border: 1px solid var(--border-color);
}
.form-control:focus {
background-color: var(--background-color);
color: var(--text-color);
border-color: var(--primary-color);
box-shadow: 0 0 0 0.25rem rgba(var(--primary-color), 0.25);
}
/* Buttons */
.btn-primary {
background-color: var(--primary-color);
border-color: var(--primary-color);
transition: background-color 0.2s ease, transform 0.2s ease;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
/* Result Card */
#result-card {
transition: opacity 0.5s ease-in-out, transform 0.5s ease;
border-width: 2px;
}
.result-good {
color: #28a745;
}
.result-bad {
color: #dc3545;
}
#result-card.result-good {
border-color: #28a745;
}
#result-card.result-bad {
border-color: #dc3545;
}
.result-value {
font-size: 2.5rem;
font-weight: bold;
}
/* Footer */
footer {
color: var(--muted-text-color);
}

74
assets/js/main.js Normal file
View File

@ -0,0 +1,74 @@
document.addEventListener('DOMContentLoaded', function () {
// --- Theme Switcher --- //
const themeSwitch = document.getElementById('theme-switch');
const themeIcon = document.querySelector('.theme-icon');
const currentTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
themeSwitch.checked = true;
themeIcon.classList.remove('fa-moon');
themeIcon.classList.add('fa-sun');
} else {
themeIcon.classList.remove('fa-sun');
themeIcon.classList.add('fa-moon');
}
themeSwitch.addEventListener('change', function(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
themeIcon.classList.remove('fa-moon');
themeIcon.classList.add('fa-sun');
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
themeIcon.classList.remove('fa-sun');
themeIcon.classList.add('fa-moon');
}
});
// --- Prediction Form --- //
const predictionForm = document.getElementById('prediction-form');
const resultCard = document.getElementById('result-card');
const resultMessage = document.getElementById('result-message');
const resultValue = document.getElementById('result-value');
predictionForm.addEventListener('submit', function (e) {
e.preventDefault();
// Mocking the API call for demonstration
const mockProbability = Math.random();
displayResult(mockProbability);
});
function displayResult(probability) {
const isFraud = probability > 0.5; // Example threshold: tune this based on your model's ROC curve
resultValue.textContent = `${(probability * 100).toFixed(2)}%`;
resultCard.classList.remove('result-good', 'result-bad');
if (isFraud) {
resultMessage.textContent = 'High Probability of Fraud';
resultValue.classList.remove('result-good');
resultValue.classList.add('result-bad');
resultCard.classList.add('result-bad');
} else {
resultMessage.textContent = 'Low Probability of Fraud';
resultValue.classList.remove('result-bad');
resultValue.classList.add('result-good');
resultCard.classList.add('result-good');
}
// Show the result card with a smooth animation
resultCard.style.display = 'block';
resultCard.style.transform = 'translateY(20px)';
setTimeout(() => {
resultCard.style.opacity = 1;
resultCard.style.transform = 'translateY(0)';
}, 10); // A small delay to ensure the display property is applied first
}
});

230
index.php
View File

@ -1,150 +1,90 @@
<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
$phpVersion = PHP_VERSION;
$now = date('Y-m-d H:i:s');
?>
<!doctype html>
<html lang="en">
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>New Style</title>
<?php
// Read project preview data from environment
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<?php if ($projectDescription): ?>
<!-- Meta description -->
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
<!-- Open Graph meta tags -->
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<!-- Twitter meta tags -->
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
<!-- Open Graph image -->
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<!-- Twitter image -->
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color-start: #6a11cb;
--bg-color-end: #2575fc;
--text-color: #ffffff;
--card-bg-color: rgba(255, 255, 255, 0.01);
--card-border-color: rgba(255, 255, 255, 0.1);
}
body {
margin: 0;
font-family: 'Inter', sans-serif;
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
overflow: hidden;
position: relative;
}
body::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
animation: bg-pan 20s linear infinite;
z-index: -1;
}
@keyframes bg-pan {
0% { background-position: 0% 0%; }
100% { background-position: 100% 100%; }
}
main {
padding: 2rem;
}
.card {
background: var(--card-bg-color);
border: 1px solid var(--card-border-color);
border-radius: 16px;
padding: 2rem;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
}
.loader {
margin: 1.25rem auto 1.25rem;
width: 48px;
height: 48px;
border: 3px solid rgba(255, 255, 255, 0.25);
border-top-color: #fff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.hint {
opacity: 0.9;
}
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; border: 0;
}
h1 {
font-size: 3rem;
font-weight: 700;
margin: 0 0 1rem;
letter-spacing: -1px;
}
p {
margin: 0.5rem 0;
font-size: 1.1rem;
}
code {
background: rgba(0,0,0,0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
footer {
position: absolute;
bottom: 1rem;
font-size: 0.8rem;
opacity: 0.7;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Credit Card Fraud Detection</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
</head>
<body>
<main>
<div class="card">
<h1>Analyzing your requirements and generating your website…</h1>
<div class="loader" role="status" aria-live="polite" aria-label="Applying initial changes">
<span class="sr-only">Loading…</span>
</div>
<p class="hint"><?= ($_SERVER['HTTP_HOST'] ?? '') === 'appwizzy.com' ? 'AppWizzy' : 'Flatlogic' ?> AI is collecting your requirements and applying the first changes.</p>
<p class="hint">This page will update automatically as the plan is implemented.</p>
<p>Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container">
<a class="navbar-brand" href="#">Fraud Detection</a>
<div class="theme-switcher ms-auto">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="theme-switch">
<label class="form-check-label" for="theme-switch"><i class="fas fa-moon theme-icon"></i></label>
</div>
</div>
</div>
</nav>
<div class="container mt-4">
<div class="hero animated">
<h1 class="animated" style="animation-delay: 0.3s;">Real-Time Transaction Analysis</h1>
<p class="lead animated" style="animation-delay: 0.5s;">Enter transaction details below to get a fraud probability score.</p>
</div>
<div class="row">
<div class="col-md-8 mx-auto">
<div class="card shadow-sm animated">
<div class="card-header">
<h5 class="mb-0">New Transaction</h5>
</div>
<div class="card-body">
<form id="prediction-form">
<p class="text-muted small">NOTE: For this demo, only a few of the 28 anonymized features are shown.</p>
<div class="row g-3">
<div class="col-md-6">
<label for="time" class="form-label">Time (seconds since first transaction)</label>
<input type="number" class="form-control" id="time" value="3600" required>
</div>
<div class="col-md-6">
<label for="amount" class="form-label">Amount</label>
<input type="number" step="0.01" class="form-control" id="amount" value="129.99" required>
</div>
<div class="col-md-3">
<label for="v1" class="form-label">V1</label>
<input type="number" step="any" class="form-control" id="v1" value="-1.35">
</div>
<div class="col-md-3">
<label for="v2" class="form-label">V2</label>
<input type="number" step="any" class="form-control" id="v2" value="-0.03">
</div>
<div class="col-md-3">
<label for="v3" class="form-label">V3</label>
<input type="number" step="any" class="form-control" id="v3" value="2.53">
</div>
<div class="col-md-3">
<label for="v4" class="form-label">V4</label>
<input type="number" step="any" class="form-control" id="v4" value="1.37">
</div>
</div>
<div class="text-center mt-4">
<button type="submit" class="btn btn-primary btn-lg">Predict Fraud</button>
</div>
</form>
</div>
</div>
<div class="card shadow-sm mt-4" id="result-card" style="display: none; opacity: 0;">
<div class="card-body text-center">
<h5 id="result-message"></h5>
<p class="result-value" id="result-value"></p>
</div>
</div>
</div>
</div>
</div>
</main>
<footer>
Page updated: <?= htmlspecialchars($now) ?> (UTC)
</footer>
<footer class="text-center text-muted py-4 mt-5">
<p>&copy; <?php echo date("Y"); ?> Fraud Detection Service</p>
</footer>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>
</html>

6
requirements.txt Normal file
View File

@ -0,0 +1,6 @@
fastapi
uvicorn[standard]
scikit-learn
pandas
numpy
joblib