Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,92 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
import
|
5 |
-
import
|
6 |
import os
|
|
|
7 |
|
8 |
-
# Load
|
9 |
-
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def detect_anomalies(df):
|
12 |
-
# Feature Engineering
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
return
|
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 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except Exception as e:
|
43 |
-
return pd.DataFrame(), None, {"
|
44 |
|
45 |
# Gradio UI
|
46 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
47 |
-
gr.Markdown("# π§ Financial Anomaly Detector
|
|
|
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"
|
55 |
summary_box = gr.JSON(label="π Summary")
|
56 |
|
57 |
-
file_input.change(
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
if __name__ == "__main__":
|
61 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
+
import pickle
|
5 |
+
import tempfile
|
6 |
import os
|
7 |
+
from datetime import datetime
|
8 |
|
9 |
+
# Load model
|
10 |
+
with open("anomaly_detector_rf_model.pkl", "rb") as f:
|
11 |
+
model = pickle.load(f)
|
12 |
|
13 |
+
# Expected features for prediction
|
14 |
+
expected_features = [
|
15 |
+
"amount", "log_amount", "amount_zscore",
|
16 |
+
"day_of_week", "hour", "is_weekend",
|
17 |
+
]
|
18 |
+
|
19 |
+
# Anomaly Detection Logic
|
20 |
def detect_anomalies(df):
|
21 |
+
# Feature Engineering
|
|
|
|
|
|
|
|
|
22 |
df['log_amount'] = np.log1p(df['amount'])
|
23 |
df['amount_zscore'] = (df['amount'] - df['amount'].mean()) / df['amount'].std()
|
24 |
|
25 |
+
if 'timestamp' in df.columns:
|
26 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
27 |
+
df['day_of_week'] = df['timestamp'].dt.dayofweek
|
28 |
+
df['hour'] = df['timestamp'].dt.hour
|
29 |
+
df['is_weekend'] = df['day_of_week'].apply(lambda x: 1 if x >= 5 else 0)
|
30 |
+
else:
|
31 |
+
# If timestamp missing, fill with default values
|
32 |
+
df['day_of_week'] = 0
|
33 |
+
df['hour'] = 0
|
34 |
+
df['is_weekend'] = 0
|
35 |
+
|
36 |
+
# Ensure all expected features exist
|
37 |
+
for col in expected_features:
|
38 |
+
if col not in df.columns:
|
39 |
+
df[col] = 0
|
40 |
|
41 |
+
# Predict
|
42 |
df['is_anomalous'] = model.predict(df[expected_features])
|
43 |
|
44 |
+
# Extract anomaly rows
|
45 |
anomalies = df[df['is_anomalous'] == 1]
|
|
|
46 |
|
47 |
+
# Columns to return
|
48 |
+
display_cols = ['amount', 'transaction_id', 'merchant', 'location']
|
49 |
+
|
50 |
+
# CSV path
|
51 |
+
temp_dir = tempfile.mkdtemp()
|
52 |
+
csv_path = os.path.join(temp_dir, "anomalies.csv")
|
53 |
+
anomalies.to_csv(csv_path, index=False)
|
54 |
|
55 |
+
return anomalies[display_cols], csv_path
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# Gradio interface function
|
58 |
def app_interface(file):
|
59 |
try:
|
60 |
df = pd.read_csv(file.name)
|
61 |
+
anomalies_df, csv_path = detect_anomalies(df)
|
62 |
+
|
63 |
+
summary = {
|
64 |
+
"Total Transactions": len(df),
|
65 |
+
"Anomalies Detected": len(anomalies_df),
|
66 |
+
"Anomaly %": round(100 * len(anomalies_df) / len(df), 2)
|
67 |
+
}
|
68 |
+
|
69 |
+
return anomalies_df, csv_path, summary
|
70 |
except Exception as e:
|
71 |
+
return pd.DataFrame(), None, {"error": str(e)}
|
72 |
|
73 |
# Gradio UI
|
74 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
75 |
+
gr.Markdown("# π§ Financial Anomaly Detector")
|
76 |
+
gr.Markdown("Upload your transaction CSV file to detect possible financial abuse in the elderly.")
|
77 |
|
78 |
with gr.Row():
|
79 |
+
file_input = gr.File(label="π€ Upload Transaction CSV", file_types=[".csv"])
|
80 |
+
download_button = gr.File(label="π₯ Download Anomalies (CSV)")
|
81 |
|
82 |
with gr.Row():
|
83 |
+
output_table = gr.Dataframe(label="π¨ Detected Anomalies")
|
84 |
summary_box = gr.JSON(label="π Summary")
|
85 |
|
86 |
+
file_input.change(
|
87 |
+
fn=app_interface,
|
88 |
+
inputs=file_input,
|
89 |
+
outputs=[output_table, download_button, summary_box]
|
90 |
+
)
|
91 |
|
92 |
+
demo.launch()
|
|
|
|