forked from sammyifelse/hackth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_ai_demo.py
More file actions
379 lines (301 loc) · 14.1 KB
/
quick_ai_demo.py
File metadata and controls
379 lines (301 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python3
"""
Quick AI Demo - Shows AI-enhanced fraud detection without requiring API keys
This demonstrates the complete system functionality with mock responses
"""
import pandas as pd
import numpy as np
from datetime import datetime
import random
# Mock AI responses to demonstrate functionality
MOCK_AI_RESPONSES = {
"fraud_explanation": """
🚨 FRAUD DETECTED - High Risk Transaction Alert
🔍 Key Risk Factors:
• Unusual timing: Transaction at 2 AM (off-hours pattern)
• High amount: ₹5,000 exceeds typical P2P transfer limits
• Weekend activity: Fraudsters often target weekends
• Device risk: Android device with suspicious location patterns
• VPA mismatch: Payer and payee using different payment apps
🧠 AI Pattern Analysis:
This transaction exhibits 5/7 high-risk indicators commonly seen in P2P fraud. The combination of late-night timing, high amount, and cross-platform transfer suggests potential account takeover or social engineering attack.
💡 Recommended Actions:
1. Immediate: Block transaction and verify with customer
2. Investigation: Check recent login patterns and device history
3. Prevention: Implement additional verification for off-hours transfers
Risk Score: 95% | Confidence: Very High
""",
"query_responses": {
"What percentage of transactions are fraudulent?": """
📊 Fraud Rate Analysis:
Based on the current dataset of 200 transactions:
• Fraud cases: 43 transactions (21.5%)
• Legitimate cases: 157 transactions (78.5%)
This is significantly higher than the typical 0.1-0.2% fraud rate in production systems, indicating this is a test dataset with artificially elevated fraud cases for model training purposes.
""",
"What are the peak hours for fraud?": """
⏰ Fraud Timing Patterns:
Peak fraud hours (based on analysis):
• 2-4 AM: 35% of fraud cases (highest risk)
• 10 PM - 12 AM: 28% of fraud cases
• 6-8 AM: 15% of fraud cases
🎯 Key Insights:
Fraudsters prefer off-hours when victims are less likely to notice unauthorized transactions immediately. Weekend nights show 40% higher fraud rates.
""",
"What's the average amount of fraudulent transactions?": """
💰 Fraud Amount Analysis:
• Average fraud amount: ₹4,247
• Median fraud amount: ₹3,500
• Typical range: ₹2,000 - ₹8,000
🎯 Pattern Analysis:
Fraudsters often target amounts just below common daily limits (₹5,000-₹10,000) to avoid triggering automatic blocks while maximizing theft value.
""",
"What patterns do you see in the fraud data?": """
🔍 Comprehensive Fraud Pattern Analysis:
📱 Device Patterns:
• Android devices: 67% of fraud cases
• iOS devices: 33% of fraud cases
• Rooted/Jailbroken devices: 89% fraud correlation
🏛️ Banking Patterns:
• Cross-bank transfers: 78% fraud rate
• Same-bank transfers: 12% fraud rate
• Digital-only banks: Higher fraud targeting
🌍 Geographic Patterns:
• Tier-1 cities: 65% of fraud cases
• Late-night + metro areas: Highest risk combination
• Rural areas: Lower volume but higher success rate
⚡ Behavioral Patterns:
• Rapid sequential transactions: 85% fraud indicator
• First-time payee transfers: 45% fraud rate
• Weekend + holiday combinations: 60% higher risk
"""
},
"pattern_analysis": """
🧠 AI-Powered Fraud Intelligence Report
Generated: {} | Analyzed: 43 fraud cases from 200 transactions
🎯 EXECUTIVE SUMMARY:
Our AI analysis reveals sophisticated fraud patterns indicating organized cybercrime activity with clear behavioral signatures.
🔍 CRITICAL PATTERNS DETECTED:
1. 🕐 TEMPORAL EXPLOITATION:
• 72% of fraud occurs between 10 PM - 6 AM
• Weekend fraud rate 3.2x higher than weekdays
• Holiday periods show 450% spike in attempts
2. 💰 AMOUNT OPTIMIZATION:
• Sweet spot: ₹2,500 - ₹7,500 (below alert thresholds)
• Micro-testing: Small amounts before large theft
• Daily limit exploitation: Multiple transactions near limits
3. 📱 DEVICE FINGERPRINTING:
• Compromised Android devices: Primary attack vector
• VPN/Proxy usage: 89% of fraud cases
• Device switching: 67% use multiple devices
4. 🏦 BANKING ECOSYSTEM ABUSE:
• Cross-platform exploitation (Paytm → PhonePe)
• New payee exploitation: 78% target unknown recipients
• Account aging: Prefer 6-12 month old accounts
5. 🌐 GEOGRAPHIC INTELLIGENCE:
• Urban concentration: Mumbai, Delhi, Bangalore (71%)
• Transit hubs: Airport/railway area exploitation
• Remote execution: Rural IPs for urban accounts
💡 AI RECOMMENDATIONS:
🛡️ IMMEDIATE ACTIONS:
• Deploy ML models for cross-platform pattern detection
• Implement velocity checks for new payee transactions
• Enhanced verification for off-hours + high-amount combinations
🔧 ADVANCED COUNTERMEASURES:
• Behavioral biometrics for device fingerprinting
• Graph analysis for payee relationship mapping
• Real-time location intelligence with device correlation
📈 BUSINESS IMPACT:
• Potential savings: ₹2.3M annually with 85% detection rate
• Customer trust: 94% satisfaction with proactive fraud prevention
• Regulatory compliance: Exceeds RBI guidelines by 340%
🎯 NEXT STEPS:
1. Deploy enhanced ML models with these patterns
2. Integrate real-time AI explanations for investigators
3. Implement customer education based on fraud vectors
Confidence Level: 96% | Pattern Strength: Very High
""".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
"feature_suggestions": """
🔧 AI-Powered Feature Engineering Recommendations
📊 CURRENT FEATURES ANALYSIS:
Your existing features provide good baseline coverage. Here are AI-suggested enhancements:
🚀 HIGH-IMPACT ADDITIONS:
1. 🕐 TEMPORAL INTELLIGENCE:
• hour_sin/hour_cos: Cyclical time encoding
• time_since_last_transaction: Velocity patterns
• is_business_hours: 9 AM - 6 PM indicator
• weekend_night_combo: High-risk time combinations
2. 💰 AMOUNT SOPHISTICATION:
• amount_zscore_user: User-specific amount deviation
• amount_percentile_daily: Daily amount ranking
• round_amount_flag: Suspicious round numbers
• micro_amount_flag: Testing transactions (< ₹100)
3. 📱 BEHAVIORAL PATTERNS:
• new_payee_flag: First-time recipient indicator
• transaction_frequency_1h: Rapid-fire detection
• cross_platform_flag: Different apps usage
• device_location_mismatch: Geographic inconsistency
4. 🏦 BANKING INTELLIGENCE:
• bank_risk_score: Historical bank fraud rates
• cross_bank_penalty: Inter-bank transfer risk
• account_age_days: New account vulnerability
• vpa_similarity_score: Similar VPA patterns
5. 🌍 LOCATION ANALYTICS:
• location_velocity: Impossible travel detection
• high_risk_area_flag: Known fraud hotspots
• location_consistency_score: Historical patterns
• metro_area_flag: Urban vs rural risk
🎯 FEATURE IMPORTANCE PREDICTION:
1. time_since_last_transaction (0.23)
2. amount_zscore_user (0.19)
3. new_payee_flag (0.17)
4. cross_platform_flag (0.15)
5. weekend_night_combo (0.12)
💡 IMPLEMENTATION PRIORITY:
• Phase 1: Temporal and amount features (80% impact)
• Phase 2: Behavioral patterns (15% additional lift)
• Phase 3: Advanced location analytics (5% final optimization)
🔬 ADVANCED ML SUGGESTIONS:
• Graph Neural Networks: For payee relationship modeling
• Transformer models: For sequence pattern detection
• Ensemble methods: Combine multiple fraud signals
• Explainable AI: For regulatory compliance
Expected Performance Gain: +12-18% in fraud detection accuracy
Implementation Complexity: Medium | ROI: Very High
"""
}
class MockAIFraudDetector:
"""Mock AI fraud detector that demonstrates functionality without API keys"""
def __init__(self):
self.provider = "mock_ai"
print("🤖 Mock AI Fraud Detection System Initialized")
print(" Provider: Advanced AI (Demo Mode)")
print(" Status: Ready for intelligent fraud analysis\n")
def explain_fraud_decision(self, transaction_data, prediction, confidence):
"""Generate intelligent fraud explanation"""
print("🧠 Generating AI fraud explanation...")
print(" Analysis type: Deep pattern recognition")
print(" Processing time: 0.8s (optimized)")
return MOCK_AI_RESPONSES["fraud_explanation"]
def answer_query(self, question, data):
"""Answer natural language questions about fraud data"""
print(f"🤖 Processing query: '{question}'")
print(" AI model: GPT-4 equivalent analysis")
# Find best matching response
for key, response in MOCK_AI_RESPONSES["query_responses"].items():
if any(word in question.lower() for word in key.lower().split()):
return response
return "I can help you analyze fraud patterns, timing, amounts, and detection strategies. Try asking about fraud percentages, peak hours, or common patterns."
def analyze_fraud_patterns(self, fraud_data):
"""Comprehensive fraud pattern analysis"""
print("🧠 Running advanced fraud pattern analysis...")
print(f" Analyzing {len(fraud_data)} fraud cases")
print(" AI techniques: Pattern mining, behavioral analysis, risk modeling")
return MOCK_AI_RESPONSES["pattern_analysis"]
def suggest_features(self, current_features):
"""AI-powered feature engineering suggestions"""
print("🔧 Analyzing current feature set...")
print(f" Current features: {len(current_features)}")
print(" AI recommendation engine: Active")
return MOCK_AI_RESPONSES["feature_suggestions"]
def create_demo_transaction():
"""Create a realistic demo transaction"""
return {
'transaction_id': f'TXN_{random.randint(100000, 999999)}',
'amount': random.choice([500, 1500, 3000, 5000, 7500, 10000]),
'transaction_type': random.choice(['P2P', 'P2M', 'M2P']),
'hour': random.randint(0, 23),
'day_of_week': random.randint(0, 6),
'is_weekend': random.choice([0, 1]),
'payer_bank': random.choice(['SBI', 'HDFC', 'ICICI', 'Axis']),
'payee_bank': random.choice(['SBI', 'HDFC', 'ICICI', 'Axis']),
'device_type': random.choice(['Android', 'iOS']),
'location': random.choice(['Mumbai', 'Delhi', 'Bangalore', 'Chennai']),
'payer_vpa': f'user{random.randint(1000, 9999)}@paytm',
'payee_vpa': f'merchant{random.randint(100, 999)}@phonepe'
}
def simulate_fraud_detection(transaction):
"""Simulate ML fraud detection"""
# Simple risk scoring based on common fraud indicators
risk_score = 0
# Time-based risk
if transaction['hour'] < 6 or transaction['hour'] > 22:
risk_score += 30
# Amount-based risk
if transaction['amount'] > 5000:
risk_score += 25
# Weekend risk
if transaction['is_weekend']:
risk_score += 15
# Cross-bank risk
if transaction['payer_bank'] != transaction['payee_bank']:
risk_score += 20
# Cross-platform risk (different VPA providers)
payer_provider = transaction['payer_vpa'].split('@')[1]
payee_provider = transaction['payee_vpa'].split('@')[1]
if payer_provider != payee_provider:
risk_score += 10
is_fraud = risk_score > 50
confidence = min(95, risk_score + random.randint(10, 20))
return is_fraud, confidence
def main():
print("🛡️ FraudGuard AI-Enhanced Detection Demo")
print("=" * 60)
print("Demonstrating AI-powered fraud detection capabilities\n")
# Initialize AI system
ai_detector = MockAIFraudDetector()
# Demo 1: Real-time fraud detection with AI explanation
print("🎯 DEMO 1: Real-time Fraud Detection with AI Explanation")
print("=" * 60)
transaction = create_demo_transaction()
is_fraud, confidence = simulate_fraud_detection(transaction)
print("📊 Transaction Analysis:")
for key, value in transaction.items():
print(f" {key}: {value}")
print(f"\n🚨 ML Prediction: {'FRAUD' if is_fraud else 'LEGITIMATE'}")
print(f"🎯 Confidence: {confidence}%")
if is_fraud:
print("\n🤖 AI Explanation:")
explanation = ai_detector.explain_fraud_decision(transaction, is_fraud, confidence)
print(explanation)
# Demo 2: Natural Language Queries
print("\n\n💬 DEMO 2: Natural Language Query Interface")
print("=" * 60)
queries = [
"What percentage of transactions are fraudulent?",
"What are the peak hours for fraud?",
"What's the average amount of fraudulent transactions?",
"What patterns do you see in the fraud data?"
]
for query in queries:
print(f"\n❓ Query: {query}")
response = ai_detector.answer_query(query, None)
print(f"🤖 AI Response:\n{response}")
# Demo 3: Advanced Pattern Analysis
print("\n\n📈 DEMO 3: AI-Powered Pattern Analysis")
print("=" * 60)
# Generate some mock fraud data
fraud_data = [create_demo_transaction() for _ in range(43)]
pattern_report = ai_detector.analyze_fraud_patterns(fraud_data)
print(pattern_report)
# Demo 4: Feature Engineering Suggestions
print("\n\n🔧 DEMO 4: AI Feature Engineering")
print("=" * 60)
current_features = ['amount', 'transaction_type', 'hour', 'day_of_week',
'payer_bank', 'payee_bank', 'device_type', 'location']
feature_suggestions = ai_detector.suggest_features(current_features)
print(feature_suggestions)
# Summary
print("\n\n🎉 DEMO COMPLETE!")
print("=" * 60)
print("✅ Real-time fraud detection with AI explanations")
print("✅ Natural language query interface")
print("✅ Advanced pattern analysis and intelligence")
print("✅ AI-powered feature engineering recommendations")
print("\n🚀 Next Steps:")
print("1. Set up actual LLM provider (see LLM_INTEGRATION_GUIDE.md)")
print("2. Run: python ai_enhanced_fraud_ui.py")
print("3. Open: http://localhost:5000")
print("4. Experience the complete AI-enhanced fraud detection system!")
if __name__ == "__main__":
main()