Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
-
import seaborn as sns
|
5 |
-
import os
|
6 |
-
import uuid
|
7 |
import joblib
|
|
|
|
|
8 |
|
9 |
-
# Load
|
10 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
11 |
|
12 |
-
# Define the features expected by the model
|
13 |
-
expected_features = ["amount"] # Update this list as per your trained model
|
14 |
-
|
15 |
def detect_anomalies(df):
|
16 |
-
|
17 |
-
df['
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
anomalies[["transaction_id", "merchant", "location", "amount", "is_anomalous"]],
|
26 |
-
csv_filename
|
27 |
-
)
|
28 |
|
29 |
-
|
30 |
-
total_transactions = len(df)
|
31 |
-
total_anomalies = df['is_anomalous'].sum()
|
32 |
-
percent_anomalies = round((total_anomalies / total_transactions) * 100, 2)
|
33 |
-
return f"Total Transactions: {total_transactions}\nTotal Anomalies: {total_anomalies}\nAnomaly Rate: {percent_anomalies}%"
|
34 |
-
|
35 |
-
def generate_charts(df):
|
36 |
-
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
ax[0].set_title('Transaction Amount Distribution')
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
ax[1].set_title('Top 10 Merchants with Anomalies')
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
def app_interface(file):
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
60 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
61 |
-
gr.Markdown(""
|
62 |
-
# π Elder Financial Abuse Detection Tool
|
63 |
-
Upload a transaction dataset to identify potential financial abuse patterns in elderly individuals.
|
64 |
-
""")
|
65 |
|
66 |
with gr.Row():
|
67 |
-
file_input = gr.File(label="
|
68 |
-
|
69 |
|
70 |
with gr.Row():
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
chart_output = gr.Image(label="π Analysis Charts")
|
75 |
-
csv_download = gr.File(label="π Download Anomalies CSV")
|
76 |
|
77 |
-
|
78 |
-
fn=app_interface,
|
79 |
-
inputs=[file_input],
|
80 |
-
outputs=[anomalies_output, summary_output, chart_output, csv_download]
|
81 |
-
)
|
82 |
|
83 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
|
|
|
|
|
|
4 |
import joblib
|
5 |
+
import datetime
|
6 |
+
import os
|
7 |
|
8 |
+
# Load trained model
|
9 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
10 |
|
|
|
|
|
|
|
11 |
def detect_anomalies(df):
|
12 |
+
# Feature Engineering (must match training phase)
|
13 |
+
df['datetime'] = pd.to_datetime(df['timestamp'])
|
14 |
+
df['hour'] = df['datetime'].dt.hour
|
15 |
+
df['day_of_week'] = df['datetime'].dt.dayofweek
|
16 |
+
df['is_weekend'] = df['day_of_week'].isin([5, 6]).astype(int)
|
17 |
+
df['log_amount'] = np.log1p(df['amount'])
|
18 |
+
df['amount_zscore'] = (df['amount'] - df['amount'].mean()) / df['amount'].std()
|
19 |
|
20 |
+
expected_features = ['amount', 'log_amount', 'amount_zscore', 'hour', 'day_of_week', 'is_weekend']
|
|
|
|
|
|
|
21 |
|
22 |
+
df['is_anomalous'] = model.predict(df[expected_features])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
anomalies = df[df['is_anomalous'] == 1]
|
25 |
+
anomalies_display = anomalies[['transaction_id', 'merchant', 'location', 'amount']].reset_index(drop=True)
|
|
|
26 |
|
27 |
+
# Save CSV for download
|
28 |
+
csv_path = "/tmp/anomalies.csv"
|
29 |
+
anomalies_display.to_csv(csv_path, index=False)
|
|
|
30 |
|
31 |
+
return anomalies_display, csv_path, {
|
32 |
+
"Total Transactions": len(df),
|
33 |
+
"Anomalies Detected": len(anomalies),
|
34 |
+
"Anomaly %": f"{(len(anomalies)/len(df)*100):.2f}%"
|
35 |
+
}
|
36 |
|
37 |
def app_interface(file):
|
38 |
+
try:
|
39 |
+
df = pd.read_csv(file.name)
|
40 |
+
anomalies, csv_path, summary = detect_anomalies(df)
|
41 |
+
return anomalies, csv_path, summary
|
42 |
+
except Exception as e:
|
43 |
+
return pd.DataFrame(), None, {"Error": str(e)}
|
44 |
+
|
45 |
+
# Gradio UI
|
46 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
47 |
+
gr.Markdown("# π§ Financial Anomaly Detector\nUpload transaction data to detect anomalies using ML.")
|
|
|
|
|
|
|
48 |
|
49 |
with gr.Row():
|
50 |
+
file_input = gr.File(label="π€ Upload CSV", file_types=[".csv"])
|
51 |
+
download_button = gr.File(label="π₯ Download Anomalies CSV")
|
52 |
|
53 |
with gr.Row():
|
54 |
+
output_table = gr.Dataframe(label="π¨ Detected Anomalies", wrap=True, height=300)
|
55 |
+
summary_box = gr.JSON(label="π Summary")
|
|
|
|
|
|
|
56 |
|
57 |
+
file_input.change(fn=app_interface, inputs=file_input, outputs=[output_table, download_button, summary_box])
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
# If running locally
|
60 |
+
if __name__ == "__main__":
|
61 |
+
demo.launch()
|