Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,58 @@
|
|
1 |
import pandas as pd
|
2 |
import joblib
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
# Load the trained model
|
6 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
7 |
|
8 |
-
# Define feature
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
def detect_anomalies(
|
14 |
-
|
15 |
-
df =
|
|
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
|
|
|
35 |
|
36 |
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
interface = gr.Interface(
|
38 |
-
fn=
|
39 |
-
inputs=gr.File(label="Upload CSV
|
40 |
-
outputs=gr.Dataframe(label="
|
41 |
-
title="Anomaly Detection
|
42 |
-
description="Upload a transaction dataset to detect anomalies."
|
43 |
)
|
44 |
|
45 |
-
|
46 |
-
interface.launch(share=True)
|
|
|
1 |
import pandas as pd
|
2 |
import joblib
|
3 |
import gradio as gr
|
4 |
+
import seaborn as sns
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
|
7 |
# Load the trained model
|
8 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
9 |
|
10 |
+
# Define feature order
|
11 |
+
feature_order = ['hour', 'day_of_week', 'is_weekend', 'amount', 'merchant_avg_amount',
|
12 |
+
'amount_zscore', 'log_amount', 'type_atm_withdrawal', 'type_credit',
|
13 |
+
'type_debit', 'merchant_encoded']
|
14 |
|
15 |
+
def detect_anomalies(data):
|
16 |
+
df = pd.DataFrame(data)
|
17 |
+
df = df[feature_order] # Ensure correct feature order
|
18 |
+
df['is_anomalous'] = model.predict(df)
|
19 |
|
20 |
+
# Filter anomalies and display relevant details
|
21 |
+
anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']]
|
22 |
+
return anomalies
|
23 |
+
|
24 |
+
# Function to generate plots
|
25 |
+
def generate_plots(df):
|
26 |
+
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
|
27 |
|
28 |
+
sns.countplot(data=df, x='is_anomalous', palette='Set2', ax=axes[0, 0])
|
29 |
+
axes[0, 0].set_title("Anomaly Distribution")
|
30 |
|
31 |
+
sns.countplot(data=df, y='merchant', order=df['merchant'].value_counts().index, palette='viridis', ax=axes[0, 1])
|
32 |
+
axes[0, 1].set_title("Transactions by Merchant")
|
33 |
|
34 |
+
sns.histplot(df['amount'], bins=30, kde=True, color='blue', ax=axes[1, 0])
|
35 |
+
axes[1, 0].set_title("Transaction Amount Distribution")
|
36 |
|
37 |
+
sns.scatterplot(data=df, x='amount', y='merchant_avg_amount', hue='is_anomalous', palette='coolwarm', ax=axes[1, 1])
|
38 |
+
axes[1, 1].set_title("Amount vs. Merchant Average Amount")
|
39 |
|
40 |
+
plt.tight_layout()
|
41 |
+
return fig
|
42 |
|
43 |
# Gradio Interface
|
44 |
+
def app_interface(file):
|
45 |
+
df = pd.read_csv(file.name)
|
46 |
+
anomalies = detect_anomalies(df)
|
47 |
+
plot = generate_plots(df)
|
48 |
+
return anomalies, plot
|
49 |
+
|
50 |
interface = gr.Interface(
|
51 |
+
fn=app_interface,
|
52 |
+
inputs=[gr.File(label="Upload Transaction Data (CSV)")],
|
53 |
+
outputs=[gr.Dataframe(label="Detected Anomalies"), gr.Plot(label="Transaction Analysis Charts")],
|
54 |
+
title="Financial Anomaly Detection",
|
55 |
+
description="Upload a transaction dataset to detect financial anomalies and visualize transaction patterns."
|
56 |
)
|
57 |
|
58 |
+
interface.launch(share=True)
|
|