-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo_llm_integration.py
More file actions
252 lines (196 loc) · 7.76 KB
/
demo_llm_integration.py
File metadata and controls
252 lines (196 loc) · 7.76 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
#!/usr/bin/env python3
"""
🤖 LLM Integration Demo for FraudGuard
Test different LLM providers and showcase AI-enhanced fraud analysis
"""
import pandas as pd
import time
from llm_integration import LLMFraudAnalyzer, LLMEnhancedFraudUI
def test_llm_providers():
"""Test different LLM providers"""
print("🤖 Testing LLM Providers")
print("="*50)
providers = ["ollama", "openai", "anthropic"]
working_providers = []
for provider in providers:
print(f"\n🔍 Testing {provider}...")
try:
llm = LLMFraudAnalyzer(api_provider=provider)
# Simple test
test_prompt = "Hello, can you help with fraud detection?"
response = llm._call_llm(test_prompt, max_tokens=100)
if "error" not in response.lower() and len(response) > 10:
print(f" ✅ {provider}: Working")
working_providers.append(provider)
else:
print(f" ❌ {provider}: {response}")
except Exception as e:
print(f" ❌ {provider}: {str(e)}")
return working_providers
def demo_fraud_explanation():
"""Demo AI-powered fraud explanation"""
print("\n🧠 AI-Powered Fraud Explanation Demo")
print("="*50)
# Try to get working LLM
working_providers = test_llm_providers()
if not working_providers:
print("❌ No LLM providers available. Please set up:")
print(" - Ollama (local): ollama pull llama3:8b")
print(" - OpenAI: export OPENAI_API_KEY=your-key")
print(" - Anthropic: export ANTHROPIC_API_KEY=your-key")
return
# Use first working provider
provider = working_providers[0]
print(f"🤖 Using {provider} for demo...")
try:
llm_analyzer = LLMFraudAnalyzer(api_provider=provider)
# Example suspicious transaction
suspicious_transaction = {
"transaction_id": "TXN_123456",
"amount": 5000.0,
"transaction_type": "P2P",
"hour": 2, # 2 AM
"is_weekend": 1,
"device_type": "Android",
"location": "Mumbai",
"payer_vpa": "user123456@paytm",
"payee_vpa": "merchant@phonepe"
}
# Simulate ML prediction
ml_prediction = 1 # Fraud detected
confidence = 0.95
feature_importance = {
"high_amount": 0.45,
"suspicious_hour": 0.30,
"weekend_transaction": 0.15,
"cross_bank_transfer": 0.10
}
print("\n📊 Transaction Details:")
for key, value in suspicious_transaction.items():
print(f" {key}: {value}")
print(f"\n🎯 ML Prediction: {'FRAUD' if ml_prediction else 'LEGITIMATE'}")
print(f"🎯 Confidence: {confidence:.1%}")
print("\n🤖 Generating AI explanation...")
start_time = time.time()
explanation = llm_analyzer.explain_fraud_decision(
suspicious_transaction, ml_prediction, confidence, feature_importance
)
end_time = time.time()
print(f"\n🧠 AI Analysis (took {end_time - start_time:.1f}s):")
print("="*60)
print(explanation)
print("="*60)
except Exception as e:
print(f"❌ Demo failed: {e}")
def demo_natural_language_queries():
"""Demo natural language queries about fraud data"""
print("\n💬 Natural Language Query Demo")
print("="*50)
# Check if test data exists
if not pd.io.common.file_exists('test_upi_transactions.csv'):
print("❌ Test data not found. Run generate_test_data.py first")
return
# Load test data
df = pd.read_csv('test_upi_transactions.csv')
print(f"📊 Loaded {len(df)} transactions")
# Try to get working LLM
working_providers = test_llm_providers()
if not working_providers:
print("❌ No LLM providers available")
return
provider = working_providers[0]
llm_analyzer = LLMFraudAnalyzer(api_provider=provider)
# Example questions
questions = [
"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 question in questions:
print(f"\n❓ Question: {question}")
print("🤖 AI Answer:", end=" ")
try:
answer = llm_analyzer.natural_language_query(question, df)
print(answer)
except Exception as e:
print(f"Error: {e}")
def demo_pattern_analysis():
"""Demo fraud pattern analysis"""
print("\n📈 Fraud Pattern Analysis Demo")
print("="*50)
# Check if test data exists
if not pd.io.common.file_exists('test_upi_transactions.csv'):
print("❌ Test data not found. Run generate_test_data.py first")
return
# Load and filter fraud cases
df = pd.read_csv('test_upi_transactions.csv')
fraud_cases = df[df['is_fraud'] == 1]
print(f"📊 Analyzing {len(fraud_cases)} fraud cases from {len(df)} total transactions")
# Try to get working LLM
working_providers = test_llm_providers()
if not working_providers:
print("❌ No LLM providers available")
return
provider = working_providers[0]
llm_analyzer = LLMFraudAnalyzer(api_provider=provider)
print("\n🤖 Generating fraud pattern analysis...")
try:
pattern_analysis = llm_analyzer.analyze_fraud_patterns(fraud_cases)
print("\n📋 Fraud Intelligence Report:")
print("="*60)
print(pattern_analysis)
print("="*60)
except Exception as e:
print(f"❌ Pattern analysis failed: {e}")
def demo_feature_suggestions():
"""Demo AI feature engineering suggestions"""
print("\n🔧 AI Feature Engineering Demo")
print("="*50)
# Try to get working LLM
working_providers = test_llm_providers()
if not working_providers:
print("❌ No LLM providers available")
return
provider = working_providers[0]
llm_analyzer = LLMFraudAnalyzer(api_provider=provider)
# Current features for UPI
current_upi_features = [
"amount", "transaction_type", "hour", "day_of_week",
"payer_bank", "payee_bank", "device_type", "location"
]
print(f"🏦 Current UPI Features: {', '.join(current_upi_features)}")
print("\n🤖 Asking AI for feature engineering suggestions...")
try:
suggestions = llm_analyzer.suggest_feature_engineering(
transaction_type="UPI",
current_features=current_upi_features
)
print("\n💡 AI Feature Suggestions:")
print("="*60)
print(suggestions)
print("="*60)
except Exception as e:
print(f"❌ Feature suggestion failed: {e}")
def main():
"""Run all LLM integration demos"""
print("🤖 FraudGuard LLM Integration Demo")
print("="*60)
print("Testing AI-enhanced fraud detection capabilities")
print()
# Test basic LLM functionality
demo_fraud_explanation()
# Test natural language queries
demo_natural_language_queries()
# Test pattern analysis
demo_pattern_analysis()
# Test feature suggestions
demo_feature_suggestions()
print("\n🎯 Demo Complete!")
print("\nTo start the AI-enhanced UI:")
print(" python ai_enhanced_fraud_ui.py")
print(" Open: http://localhost:5000")
print("\nTo configure LLM providers:")
print(" See LLM_INTEGRATION_GUIDE.md for detailed setup instructions")
if __name__ == "__main__":
main()