cloud-sean's picture
Create app.py
fbfc3ee verified
import time
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
# Set page configuration
st.set_page_config(
page_title="Enhanced Fraud Detection Demo",
page_icon="πŸ”",
layout="wide",
initial_sidebar_state="expanded",
)
# Title of the app
st.title("πŸ” Enhanced Fraud Detection Simulation Demo")
# Security and Compliance Information
st.markdown("""
---
**Security & Compliance:**
- **Data Protection:** All data is encrypted and securely stored.
- **Access Control:** Role-based access control implemented for all users.
---
""")
# Sidebar for user inputs
st.sidebar.header("Simulate Submission Data")
applicant_id = st.sidebar.text_input("Applicant ID", "A12345")
name = st.sidebar.text_input("Name", "John Doe")
age = st.sidebar.number_input("Age", 18, 100, 34)
income = st.sidebar.number_input("Income ($)", 0, 10000, 2500)
benefits = st.sidebar.multiselect(
"Benefits Applied For",
["Food Assistance", "Child Support", "Medical Coverage", "Temporary Assistance"],
["Food Assistance", "Child Support"]
)
address = st.sidebar.text_input("Address", "123 Elm Street")
ebt_card = st.sidebar.text_input("EBT Card Number", "EBT123456789")
app_date = st.sidebar.date_input("Application Date", value=pd.to_datetime("2024-10-25"))
# Scenario selection
scenario = st.sidebar.selectbox(
"Select Submission Scenario",
["Custom Submission", "Normal Submission", "Potential Fraud"]
)
# Adjust submission data based on scenario
if scenario == "Normal Submission":
income = 5000
benefits = ["Food Assistance"]
address_change = False
elif scenario == "Potential Fraud":
income = 2500
benefits = ["Food Assistance", "Child Support"]
address_change = True
else:
# Custom submission
address_change = True if "Elm Street" in address else False
# Help Section
with st.expander("ℹ️ Help & Documentation"):
st.markdown("""
**How to Use This Demo:**
1. **Simulate Submission:** Enter applicant details in the sidebar or select a predefined scenario.
2. **Start Simulation:** Click the "Start Fraud Detection Simulation" button to begin.
3. **View Results:** The app will display the submission data, anomaly detection results, a detailed report, and visualizations.
4. **Rerun Simulation:** Use the "Rerun Simulation" button to test different scenarios.
**Understanding the Outputs:**
- **Anomaly Score:** A higher score indicates a higher likelihood of fraud.
- **Fraud Analysis Report:** Provides detailed reasons and recommendations based on detected anomalies.
- **Visualizations:** Graphical representations of anomaly scores and distributions.
**Security Features:**
- All data is handled securely and complies with relevant standards.
""")
# Button to start the simulation
if st.button("Start Fraud Detection Simulation"):
# Step 1: Submission Intake
with st.status("Receiving submission..."):
st.write("πŸ“₯ **Submission Received:**")
# Simulated submission data
submission_data = {
"Applicant ID": applicant_id,
"Name": name,
"Age": age,
"Income": income,
"Benefits Applied For": benefits,
"Address": address,
"EBT Card Number": ebt_card,
"Application Date": str(app_date)
}
st.json(submission_data)
progress = st.progress(20)
time.sleep(1)
progress.progress(40)
time.sleep(1)
# Simulate fetching additional data from EBT vendor (FIS)
with st.status("Fetching data from EBT vendor (FIS)..."):
st.write("πŸ”— **Connecting to EBT Vendor (FIS):**")
# Simulated external data
ebt_vendor_data = {
"EBT Card Number": "EBT123456789",
"Balance": 1500,
"Last Transaction": "2024-10-24",
"Transaction History": [
{"Date": "2024-10-20", "Amount": 100, "Merchant": "SuperMart"},
{"Date": "2024-10-22", "Amount": 200, "Merchant": "QuickStop"},
]
}
st.json(ebt_vendor_data)
progress.progress(60)
time.sleep(1)
progress.progress(80)
time.sleep(1)
# Step 2: Anomaly Detection with ML
with st.status("Analyzing submission for anomalies..."):
st.write("πŸ€– **Running ML Anomaly Detection:**")
# Simulated pattern data
existing_submissions = pd.DataFrame([
{"Applicant ID": "A12346", "Name": "Jane Smith", "Income": 5200, "Benefits": ["Food Assistance"], "Address": "456 Oak Street", "EBT Card Number": "EBT987654321"},
{"Applicant ID": "A12347", "Name": "Bob Johnson", "Income": 3000, "Benefits": ["Child Support"], "Address": "789 Pine Street", "EBT Card Number": "EBT192837465"},
# Add more submissions as needed
])
terminal_usage = pd.DataFrame([
{"Terminal ID": "T001", "Location": "SuperMart Downtown", "Anomaly Score": 0.1},
{"Terminal ID": "T002", "Location": "QuickStop Uptown", "Anomaly Score": 0.3},
{"Terminal ID": "T003", "Location": "MegaStore Central", "Anomaly Score": 0.7},
# Add more terminals as needed
])
call_center_patterns = pd.DataFrame([
{"Call Center ID": "C001", "Activity": "High volume of calls", "Anomaly Score": 0.4},
{"Call Center ID": "C002", "Activity": "Repeated failed verifications", "Anomaly Score": 0.6},
{"Call Center ID": "C003", "Activity": "Unusual call times", "Anomaly Score": 0.2},
# Add more patterns as needed
])
# Simple anomaly detection logic across patterns
def detect_patterns(income, benefits, address_change, ebt_balance, terminal_id, call_center_id):
anomaly_score = 0
reasons = []
# Income anomaly
if income < 3000:
anomaly_score += 0.2
reasons.append("Income significantly lower than average for applicants.")
# Multiple benefits
if len(benefits) > 1:
anomaly_score += 0.2
reasons.append("Multiple benefits applied for simultaneously.")
# Address change
if address_change:
anomaly_score += 0.1
reasons.append("Recent change in address detected.")
# EBT balance anomaly
if ebt_balance < 500:
anomaly_score += 0.2
reasons.append("EBT balance unusually low.")
# Terminal anomaly
terminal = terminal_usage[terminal_usage["Terminal ID"] == terminal_id]
if not terminal.empty and terminal["Anomaly Score"].values[0] > 0.5:
anomaly_score += 0.2
reasons.append(f"Terminal {terminal_id} at {terminal['Location'].values[0]} shows high anomaly score.")
# Call center pattern anomaly
call_center = call_center_patterns[call_center_patterns["Call Center ID"] == call_center_id]
if not call_center.empty and call_center["Anomaly Score"].values[0] > 0.5:
anomaly_score += 0.1
reasons.append(f"Call Center {call_center_id} shows suspicious activity: {call_center['Activity'].values[0]}.")
# Insider threat simulation (e.g., unusual internal access)
insider_threat = False # Hardcoded for simplicity
if insider_threat:
anomaly_score += 0.1
reasons.append("Insider threat detected: Unusual internal access patterns.")
return round(anomaly_score, 2), reasons
# Simulate terminal and call center IDs
simulated_terminal_id = "T003" # High anomaly score terminal
simulated_call_center_id = "C002" # High anomaly score call center
# EBT balance from vendor data
ebt_balance = ebt_vendor_data["Balance"]
# Calculate anomaly score
anomaly_score, reasons = detect_patterns(
income=income,
benefits=benefits,
address_change=address_change,
ebt_balance=ebt_balance,
terminal_id=simulated_terminal_id,
call_center_id=simulated_call_center_id
)
ml_response = {
"Anomaly Detected": anomaly_score > 0.5,
"Anomaly Score": anomaly_score,
"Reasons": reasons
}
# Simulate processing steps
st.write("πŸ” **Evaluating applicant data and detecting patterns...")
time.sleep(1)
st.write("πŸ“Š **Calculating comprehensive anomaly score...")
time.sleep(1)
st.write("βœ… **Anomaly Detection Complete.")
st.json(ml_response)
progress.progress(100)
time.sleep(0.5)
# Step 3: Report Generation with AI
with st.status("Generating fraud analysis report..."):
st.write("πŸ“ **Generating Report:**")
# Generate AI report based on ML response
def generate_report(applicant_id, name, anomaly_score, reasons):
report = f"""
### Fraud Analysis Report
**Applicant ID:** {applicant_id}
**Name:** {name}
**Anomaly Score:** {anomaly_score}
**Reasons for Potential Fraud:**
"""
for reason in reasons:
report += f"- **{reason}**\n"
report += """
**Recommendations:**
- Conduct a detailed investigation of the applicant's financial records.
- Verify the authenticity of the provided documents.
- Monitor EBT card transactions for any suspicious activities.
- Review terminal usage patterns and secure high-risk terminals.
- Investigate call center activities associated with the applicant.
"""
return report
ai_report = generate_report(applicant_id, name, ml_response["Anomaly Score"], ml_response["Reasons"])
# Simulate report generation
st.markdown(ai_report)
time.sleep(1)
# Step 4: Visualization
with st.status("Creating visualization of detected anomalies..."):
st.write("πŸ“ˆ **Generating Visualization:**")
# Simulated data for visualization
# Anomaly Scores by Category
data = {
"Category": ["Income", "Benefits Applied", "Address Changes", "EBT Transactions", "Terminal Usage", "Call Center Activity"],
"Score": [0.2, 0.2, 0.1, 0.2, 0.2, 0.1]
}
df = pd.DataFrame(data)
# Create a bar chart with Matplotlib
fig, ax = plt.subplots(figsize=(10, 5))
bars = ax.bar(df["Category"], df["Score"], color=['red', 'orange', 'yellow', 'red', 'orange', 'yellow'])
ax.set_ylim(0, 1)
ax.set_ylabel("Anomaly Score")
ax.set_title("Anomaly Scores by Category")
# Add score labels on top of bars
for bar in bars:
height = bar.get_height()
ax.annotate(f'{height:.2f}',
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
st.pyplot(fig)
time.sleep(1)
# Additional Visualization: Anomaly Score Distribution
st.write("πŸ“Š **Anomaly Score Distribution:**")
fig_pie = px.pie(
values=[1 - anomaly_score, anomaly_score],
names=["Normal", "Anomaly"],
title="Anomaly Score Distribution",
color_discrete_sequence=["#4CAF50", "#F44336"]
)
st.plotly_chart(fig_pie)
time.sleep(1)
# Terminal Anomaly Heatmap
st.write("πŸ“ **Terminal Anomaly Heatmap:**")
# Simulated terminal locations and anomaly scores
terminal_map_data = terminal_usage.copy()
fig_map = px.scatter_geo(
terminal_map_data,
lat=[47.6062, 47.6097, 47.6205], # Example latitudes
lon=[-122.3321, -122.3416, -122.3493], # Example longitudes
text=terminal_map_data["Location"],
size=terminal_map_data["Anomaly Score"] * 20,
color=terminal_map_data["Anomaly Score"],
color_continuous_scale="Reds",
title="Terminal Anomaly Heatmap"
)
st.plotly_chart(fig_map)
time.sleep(1)
# Final Status
st.success("βœ… **Fraud Detection Simulation Completed Successfully!**")
# Feedback Section
with st.expander("πŸ’¬ Provide Feedback"):
feedback = st.text_area("We value your feedback. Please share your thoughts on this simulation.")
if st.button("Submit Feedback"):
st.success("Thank you for your feedback!")
# Option to rerun
st.markdown("---")
st.button("Rerun Simulation")
else:
st.info("Click the **Start Fraud Detection Simulation** button to begin.")