File size: 2,742 Bytes
cf86c3a
dd8c606
155de20
cf86c3a
3ec40df
 
 
dd8c606
3ec40df
dd8c606
 
3ec40df
 
dd8c606
cf86c3a
3ec40df
 
 
155de20
3ec40df
 
 
235d85c
3ec40df
 
 
235d85c
 
3ec40df
 
 
 
 
235d85c
3ec40df
 
235d85c
3ec40df
 
 
235d85c
3ec40df
 
 
 
235d85c
3ec40df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d337dd
 
3ec40df
 
5d337dd
3ec40df
 
 
5d337dd
3ec40df
 
5d337dd
3ec40df
 
 
 
 
dd8c606
3ec40df
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
import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import uuid
import joblib

# Load the model
model = joblib.load("anomaly_detector_rf_model.pkl")

# Define the features expected by the model
expected_features = ["amount"]  # Update this list as per your trained model

def detect_anomalies(df):
    df = df.copy()
    df['is_anomalous'] = model.predict(df[expected_features])
    anomalies = df[df['is_anomalous'] == 1]

    # Save anomalies to temporary CSV file
    csv_filename = f"/tmp/anomalies_{uuid.uuid4().hex}.csv"
    anomalies.to_csv(csv_filename, index=False)

    return (
        anomalies[["transaction_id", "merchant", "location", "amount", "is_anomalous"]],
        csv_filename
    )

def generate_summary(df):
    total_transactions = len(df)
    total_anomalies = df['is_anomalous'].sum()
    percent_anomalies = round((total_anomalies / total_transactions) * 100, 2)
    return f"Total Transactions: {total_transactions}\nTotal Anomalies: {total_anomalies}\nAnomaly Rate: {percent_anomalies}%"

def generate_charts(df):
    fig, ax = plt.subplots(1, 2, figsize=(12, 5))

    # Distribution of Amounts
    sns.histplot(df['amount'], bins=30, ax=ax[0], kde=True)
    ax[0].set_title('Transaction Amount Distribution')

    # Anomalies by Merchant
    anomaly_counts = df[df['is_anomalous'] == 1]['merchant'].value_counts().nlargest(10)
    sns.barplot(x=anomaly_counts.values, y=anomaly_counts.index, ax=ax[1])
    ax[1].set_title('Top 10 Merchants with Anomalies')

    plt.tight_layout()
    chart_path = f"/tmp/chart_{uuid.uuid4().hex}.png"
    plt.savefig(chart_path)
    plt.close()
    return chart_path

def app_interface(file):
    df = pd.read_csv(file.name)
    anomalies, csv_path = detect_anomalies(df)
    summary = generate_summary(df)
    chart_path = generate_charts(df)
    return anomalies, summary, chart_path, csv_path

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ” Elder Financial Abuse Detection Tool
    Upload a transaction dataset to identify potential financial abuse patterns in elderly individuals.
    """)

    with gr.Row():
        file_input = gr.File(label="πŸ“‚ Upload Transaction CSV")
        analyze_btn = gr.Button("πŸš€ Analyze")

    with gr.Row():
        anomalies_output = gr.Dataframe(label="⚠️ Detected Anomalies", wrap=True)
        summary_output = gr.Textbox(label="πŸ“Š Summary")

    chart_output = gr.Image(label="πŸ“ˆ Analysis Charts")
    csv_download = gr.File(label="πŸ“ Download Anomalies CSV")

    analyze_btn.click(
        fn=app_interface,
        inputs=[file_input],
        outputs=[anomalies_output, summary_output, chart_output, csv_download]
    )

demo.launch()