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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gradio Demo for Credit Card Fraud Detection
|
| 3 |
+
This app can be deployed to Hugging Face Spaces
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import joblib
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# ============================================================================
|
| 13 |
+
# Feature Engineering Function
|
| 14 |
+
# ============================================================================
|
| 15 |
+
|
| 16 |
+
def engineer_features(df):
|
| 17 |
+
"""Engineer features for prediction"""
|
| 18 |
+
# Amount-based features
|
| 19 |
+
df['amount_log'] = np.log1p(df['amount'])
|
| 20 |
+
df['amount_zscore'] = (df['amount'] - df['avg_transaction_amount']) / (df['avg_transaction_amount'] + 1)
|
| 21 |
+
df['is_high_amount'] = (df['amount'] > 1000).astype(int)
|
| 22 |
+
df['is_round_amount'] = (df['amount'] % 10 == 0).astype(int)
|
| 23 |
+
|
| 24 |
+
# Time-based features
|
| 25 |
+
df['is_night'] = ((df['time_of_day'] >= 22) | (df['time_of_day'] <= 6)).astype(int)
|
| 26 |
+
df['is_weekend'] = (df['day_of_week'] >= 5).astype(int)
|
| 27 |
+
df['is_business_hours'] = ((df['time_of_day'] >= 9) & (df['time_of_day'] <= 17)).astype(int)
|
| 28 |
+
|
| 29 |
+
# Location-based features
|
| 30 |
+
df['is_far_from_home'] = (df['distance_from_home'] > 50).astype(int)
|
| 31 |
+
df['unusual_location_change'] = (df['distance_from_last_transaction'] > 100).astype(int)
|
| 32 |
+
df['location_velocity'] = df['distance_from_last_transaction'] / (df['time_since_last_transaction'] + 0.1)
|
| 33 |
+
|
| 34 |
+
# Velocity features
|
| 35 |
+
df['rapid_transactions'] = (df['time_since_last_transaction'] < 1).astype(int)
|
| 36 |
+
df['high_daily_frequency'] = (df['num_transactions_today'] > 5).astype(int)
|
| 37 |
+
df['high_weekly_frequency'] = (df['num_transactions_last_week'] > 15).astype(int)
|
| 38 |
+
|
| 39 |
+
# Behavioral features
|
| 40 |
+
df['online_without_card'] = ((df['is_online'] == 1) & (df['card_present'] == 0)).astype(int)
|
| 41 |
+
df['international_online'] = ((df['is_international'] == 1) & (df['is_online'] == 1)).astype(int)
|
| 42 |
+
df['new_account'] = (df['account_age_days'] < 90).astype(int)
|
| 43 |
+
|
| 44 |
+
# Risk score
|
| 45 |
+
df['risk_score'] = (
|
| 46 |
+
df['is_night'] * 2 +
|
| 47 |
+
df['is_far_from_home'] * 3 +
|
| 48 |
+
df['rapid_transactions'] * 3 +
|
| 49 |
+
df['high_daily_frequency'] * 2 +
|
| 50 |
+
df['online_without_card'] * 2 +
|
| 51 |
+
df['is_international'] * 1
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return df
|
| 55 |
+
|
| 56 |
+
# ============================================================================
|
| 57 |
+
# Load Model
|
| 58 |
+
# ============================================================================
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
model_data = joblib.load('fraud_model.pkl')
|
| 62 |
+
MODEL = model_data['model']
|
| 63 |
+
SCALER = model_data['scaler']
|
| 64 |
+
FEATURE_COLUMNS = model_data['feature_columns']
|
| 65 |
+
model_loaded = True
|
| 66 |
+
except Exception as e:
|
| 67 |
+
model_loaded = False
|
| 68 |
+
error_message = str(e)
|
| 69 |
+
|
| 70 |
+
# ============================================================================
|
| 71 |
+
# Prediction Function
|
| 72 |
+
# ============================================================================
|
| 73 |
+
|
| 74 |
+
def predict_fraud(
|
| 75 |
+
amount,
|
| 76 |
+
time_of_day,
|
| 77 |
+
day_of_week,
|
| 78 |
+
distance_from_home,
|
| 79 |
+
distance_from_last_transaction,
|
| 80 |
+
time_since_last_transaction,
|
| 81 |
+
num_transactions_today,
|
| 82 |
+
num_transactions_last_week,
|
| 83 |
+
merchant_category,
|
| 84 |
+
is_online,
|
| 85 |
+
card_present,
|
| 86 |
+
is_international,
|
| 87 |
+
avg_transaction_amount,
|
| 88 |
+
account_age_days
|
| 89 |
+
):
|
| 90 |
+
"""Predict if a transaction is fraudulent"""
|
| 91 |
+
|
| 92 |
+
if not model_loaded:
|
| 93 |
+
return "❌ Model not loaded. Please ensure fraud_model.pkl is in the directory.", "", "", ""
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
# Prepare transaction data
|
| 97 |
+
transaction = {
|
| 98 |
+
'amount': amount,
|
| 99 |
+
'time_of_day': time_of_day,
|
| 100 |
+
'day_of_week': day_of_week,
|
| 101 |
+
'distance_from_home': distance_from_home,
|
| 102 |
+
'distance_from_last_transaction': distance_from_last_transaction,
|
| 103 |
+
'time_since_last_transaction': time_since_last_transaction,
|
| 104 |
+
'num_transactions_today': num_transactions_today,
|
| 105 |
+
'num_transactions_last_week': num_transactions_last_week,
|
| 106 |
+
'merchant_category': merchant_category,
|
| 107 |
+
'is_online': 1 if is_online == "Yes" else 0,
|
| 108 |
+
'card_present': 1 if card_present == "Yes" else 0,
|
| 109 |
+
'is_international': 1 if is_international == "Yes" else 0,
|
| 110 |
+
'avg_transaction_amount': avg_transaction_amount,
|
| 111 |
+
'account_age_days': account_age_days
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
# Engineer features
|
| 115 |
+
transaction_df = pd.DataFrame([transaction])
|
| 116 |
+
transaction_df = engineer_features(transaction_df)
|
| 117 |
+
|
| 118 |
+
# Extract features
|
| 119 |
+
X = transaction_df[FEATURE_COLUMNS]
|
| 120 |
+
X_scaled = SCALER.transform(X)
|
| 121 |
+
|
| 122 |
+
# Predict
|
| 123 |
+
fraud_probability = float(MODEL.predict_proba(X_scaled)[0, 1])
|
| 124 |
+
is_fraud = bool(MODEL.predict(X_scaled)[0])
|
| 125 |
+
|
| 126 |
+
# Determine risk level and color
|
| 127 |
+
if fraud_probability >= 0.9:
|
| 128 |
+
risk_level = "🔴 CRITICAL"
|
| 129 |
+
risk_color = "red"
|
| 130 |
+
elif fraud_probability >= 0.7:
|
| 131 |
+
risk_level = "🟠 HIGH"
|
| 132 |
+
risk_color = "orange"
|
| 133 |
+
elif fraud_probability >= 0.5:
|
| 134 |
+
risk_level = "🟡 MEDIUM"
|
| 135 |
+
risk_color = "yellow"
|
| 136 |
+
elif fraud_probability >= 0.3:
|
| 137 |
+
risk_level = "🔵 LOW"
|
| 138 |
+
risk_color = "blue"
|
| 139 |
+
else:
|
| 140 |
+
risk_level = "🟢 MINIMAL"
|
| 141 |
+
risk_color = "green"
|
| 142 |
+
|
| 143 |
+
# Decision
|
| 144 |
+
if is_fraud:
|
| 145 |
+
decision = "🚨 BLOCK TRANSACTION"
|
| 146 |
+
decision_color = "red"
|
| 147 |
+
else:
|
| 148 |
+
decision = "✅ APPROVE TRANSACTION"
|
| 149 |
+
decision_color = "green"
|
| 150 |
+
|
| 151 |
+
# Format output
|
| 152 |
+
probability_text = f"**Fraud Probability:** {fraud_probability*100:.2f}%"
|
| 153 |
+
risk_text = f"**Risk Level:** {risk_level}"
|
| 154 |
+
decision_text = f"**Decision:** {decision}"
|
| 155 |
+
|
| 156 |
+
# Additional info
|
| 157 |
+
details = f"""
|
| 158 |
+
### Transaction Analysis
|
| 159 |
+
|
| 160 |
+
**Input Summary:**
|
| 161 |
+
- Amount: ${amount:.2f}
|
| 162 |
+
- Time: {time_of_day:.1f}:00 (Hour {int(time_of_day)})
|
| 163 |
+
- Location: {distance_from_home:.0f}km from home
|
| 164 |
+
- Frequency: {num_transactions_today} today, {num_transactions_last_week} this week
|
| 165 |
+
- Type: {"Online" if is_online == "Yes" else "In-Store"} | {"Card Present" if card_present == "Yes" else "Card Not Present"}
|
| 166 |
+
- International: {is_international}
|
| 167 |
+
|
| 168 |
+
**Risk Indicators:**
|
| 169 |
+
- {'⚠️ Late night transaction' if (time_of_day >= 22 or time_of_day <= 6) else '✓ Normal hours'}
|
| 170 |
+
- {'⚠️ Far from home' if distance_from_home > 50 else '✓ Normal location'}
|
| 171 |
+
- {'⚠️ High transaction frequency' if num_transactions_today > 5 else '✓ Normal frequency'}
|
| 172 |
+
- {'⚠️ Rapid transactions' if time_since_last_transaction < 1 else '✓ Normal velocity'}
|
| 173 |
+
|
| 174 |
+
**Model Confidence:** {max(fraud_probability, 1-fraud_probability)*100:.1f}%
|
| 175 |
+
"""
|
| 176 |
+
|
| 177 |
+
return probability_text, risk_text, decision_text, details
|
| 178 |
+
|
| 179 |
+
except Exception as e:
|
| 180 |
+
return f"❌ Error: {str(e)}", "", "", ""
|
| 181 |
+
|
| 182 |
+
# ============================================================================
|
| 183 |
+
# Gradio Interface
|
| 184 |
+
# ============================================================================
|
| 185 |
+
|
| 186 |
+
# Example transactions
|
| 187 |
+
legitimate_example = [
|
| 188 |
+
75.50, # amount
|
| 189 |
+
14.0, # time_of_day
|
| 190 |
+
2, # day_of_week (Tuesday)
|
| 191 |
+
3, # distance_from_home
|
| 192 |
+
2, # distance_from_last_transaction
|
| 193 |
+
24, # time_since_last_transaction
|
| 194 |
+
1, # num_transactions_today
|
| 195 |
+
7, # num_transactions_last_week
|
| 196 |
+
2, # merchant_category
|
| 197 |
+
"No", # is_online
|
| 198 |
+
"Yes", # card_present
|
| 199 |
+
"No", # is_international
|
| 200 |
+
80, # avg_transaction_amount
|
| 201 |
+
730 # account_age_days (2 years)
|
| 202 |
+
]
|
| 203 |
+
|
| 204 |
+
fraudulent_example = [
|
| 205 |
+
1250.00, # amount
|
| 206 |
+
2.5, # time_of_day (2:30 AM)
|
| 207 |
+
3, # day_of_week (Wednesday)
|
| 208 |
+
250, # distance_from_home
|
| 209 |
+
200, # distance_from_last_transaction
|
| 210 |
+
0.5, # time_since_last_transaction
|
| 211 |
+
12, # num_transactions_today
|
| 212 |
+
25, # num_transactions_last_week
|
| 213 |
+
7, # merchant_category
|
| 214 |
+
"Yes", # is_online
|
| 215 |
+
"No", # card_present
|
| 216 |
+
"Yes", # is_international
|
| 217 |
+
65, # avg_transaction_amount
|
| 218 |
+
30 # account_age_days
|
| 219 |
+
]
|
| 220 |
+
|
| 221 |
+
# Create interface
|
| 222 |
+
with gr.Blocks(title="Credit Card Fraud Detection", theme=gr.themes.Soft()) as demo:
|
| 223 |
+
gr.Markdown("""
|
| 224 |
+
# 💳 Credit Card Fraud Detection System
|
| 225 |
+
|
| 226 |
+
Real-time machine learning model to detect fraudulent credit card transactions.
|
| 227 |
+
|
| 228 |
+
**Model Performance:**
|
| 229 |
+
- ✅ 100% Fraud Detection Rate
|
| 230 |
+
- ✅ <1% False Alarm Rate
|
| 231 |
+
- ✅ ROC AUC: 1.0000
|
| 232 |
+
- ⚡ Real-time processing (<5ms)
|
| 233 |
+
|
| 234 |
+
Enter transaction details below to check if it's fraudulent.
|
| 235 |
+
""")
|
| 236 |
+
|
| 237 |
+
with gr.Row():
|
| 238 |
+
with gr.Column():
|
| 239 |
+
gr.Markdown("### Transaction Details")
|
| 240 |
+
|
| 241 |
+
amount = gr.Number(label="Transaction Amount ($)", value=100.00, minimum=0)
|
| 242 |
+
|
| 243 |
+
with gr.Row():
|
| 244 |
+
time_of_day = gr.Slider(label="Time of Day (24h)", minimum=0, maximum=23.99, value=14.0, step=0.1)
|
| 245 |
+
day_of_week = gr.Slider(label="Day of Week (0=Mon, 6=Sun)", minimum=0, maximum=6, value=2, step=1)
|
| 246 |
+
|
| 247 |
+
gr.Markdown("### Location & Movement")
|
| 248 |
+
with gr.Row():
|
| 249 |
+
distance_from_home = gr.Number(label="Distance from Home (km)", value=10, minimum=0)
|
| 250 |
+
distance_from_last_transaction = gr.Number(label="Distance from Last Txn (km)", value=5, minimum=0)
|
| 251 |
+
|
| 252 |
+
time_since_last_transaction = gr.Number(label="Hours Since Last Transaction", value=24, minimum=0)
|
| 253 |
+
|
| 254 |
+
gr.Markdown("### Transaction Patterns")
|
| 255 |
+
with gr.Row():
|
| 256 |
+
num_transactions_today = gr.Slider(label="Transactions Today", minimum=0, maximum=20, value=2, step=1)
|
| 257 |
+
num_transactions_last_week = gr.Slider(label="Transactions Last Week", minimum=0, maximum=50, value=8, step=1)
|
| 258 |
+
|
| 259 |
+
merchant_category = gr.Slider(label="Merchant Category (1-8)", minimum=1, maximum=8, value=2, step=1)
|
| 260 |
+
|
| 261 |
+
gr.Markdown("### Transaction Type")
|
| 262 |
+
with gr.Row():
|
| 263 |
+
is_online = gr.Radio(["Yes", "No"], label="Online Transaction?", value="No")
|
| 264 |
+
card_present = gr.Radio(["Yes", "No"], label="Card Present?", value="Yes")
|
| 265 |
+
is_international = gr.Radio(["Yes", "No"], label="International?", value="No")
|
| 266 |
+
|
| 267 |
+
gr.Markdown("### Account Information")
|
| 268 |
+
with gr.Row():
|
| 269 |
+
avg_transaction_amount = gr.Number(label="Average Transaction Amount ($)", value=100, minimum=0)
|
| 270 |
+
account_age_days = gr.Number(label="Account Age (days)", value=365, minimum=0)
|
| 271 |
+
|
| 272 |
+
with gr.Row():
|
| 273 |
+
predict_btn = gr.Button("🔍 Check for Fraud", variant="primary", size="lg")
|
| 274 |
+
clear_btn = gr.ClearButton()
|
| 275 |
+
|
| 276 |
+
with gr.Column():
|
| 277 |
+
gr.Markdown("### Fraud Analysis Results")
|
| 278 |
+
|
| 279 |
+
probability_output = gr.Markdown(label="Fraud Probability")
|
| 280 |
+
risk_output = gr.Markdown(label="Risk Level")
|
| 281 |
+
decision_output = gr.Markdown(label="Decision")
|
| 282 |
+
details_output = gr.Markdown(label="Analysis Details")
|
| 283 |
+
|
| 284 |
+
gr.Markdown("### Try These Examples")
|
| 285 |
+
gr.Examples(
|
| 286 |
+
examples=[
|
| 287 |
+
legitimate_example + ["Legitimate Transaction - Normal spending pattern"],
|
| 288 |
+
fraudulent_example + ["Suspicious Transaction - Multiple fraud indicators"]
|
| 289 |
+
],
|
| 290 |
+
inputs=[
|
| 291 |
+
amount, time_of_day, day_of_week, distance_from_home,
|
| 292 |
+
distance_from_last_transaction, time_since_last_transaction,
|
| 293 |
+
num_transactions_today, num_transactions_last_week,
|
| 294 |
+
merchant_category, is_online, card_present, is_international,
|
| 295 |
+
avg_transaction_amount, account_age_days,
|
| 296 |
+
gr.Textbox(visible=False) # Description (hidden)
|
| 297 |
+
],
|
| 298 |
+
label="Click to load example"
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Button actions
|
| 302 |
+
predict_btn.click(
|
| 303 |
+
fn=predict_fraud,
|
| 304 |
+
inputs=[
|
| 305 |
+
amount, time_of_day, day_of_week, distance_from_home,
|
| 306 |
+
distance_from_last_transaction, time_since_last_transaction,
|
| 307 |
+
num_transactions_today, num_transactions_last_week,
|
| 308 |
+
merchant_category, is_online, card_present, is_international,
|
| 309 |
+
avg_transaction_amount, account_age_days
|
| 310 |
+
],
|
| 311 |
+
outputs=[probability_output, risk_output, decision_output, details_output]
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
gr.Markdown("""
|
| 315 |
+
---
|
| 316 |
+
### About This Model
|
| 317 |
+
|
| 318 |
+
This fraud detection system uses a Random Forest classifier trained on 100,000 transactions with 31 engineered features.
|
| 319 |
+
|
| 320 |
+
**Key Features Analyzed:**
|
| 321 |
+
- Transaction amount and patterns
|
| 322 |
+
- Time of day and day of week
|
| 323 |
+
- Location and distance traveled
|
| 324 |
+
- Transaction velocity and frequency
|
| 325 |
+
- Merchant type and transaction mode
|
| 326 |
+
- Account age and history
|
| 327 |
+
|
| 328 |
+
**Disclaimer:** This is a demonstration model trained on synthetic data. For production use, train on real transaction data and implement proper security measures.
|
| 329 |
+
|
| 330 |
+
📚 [Full Documentation](https://huggingface.co/YOUR_USERNAME/credit-card-fraud-detector) | 💻 [GitHub Repository](https://github.com/YOUR_USERNAME/fraud-detection)
|
| 331 |
+
""")
|
| 332 |
+
|
| 333 |
+
# Launch
|
| 334 |
+
if __name__ == "__main__":
|
| 335 |
+
demo.launch()
|