Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
|
|
3 |
import joblib
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
|
|
6 |
|
7 |
# Load trained model
|
8 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
@@ -27,9 +28,9 @@ def detect_anomalies(df):
|
|
27 |
original_df["is_anomalous"] = preds
|
28 |
|
29 |
anomalies = original_df[original_df["is_anomalous"] == 1]
|
30 |
-
return anomalies[["transaction_id", "merchant", "location", "amount", "is_anomalous"]]
|
31 |
|
32 |
-
# Function to
|
33 |
def plot_charts(df):
|
34 |
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
|
35 |
|
@@ -58,28 +59,49 @@ def plot_charts(df):
|
|
58 |
plt.tight_layout()
|
59 |
return fig
|
60 |
|
61 |
-
#
|
62 |
def app_interface(csv_file):
|
63 |
df = pd.read_csv(csv_file)
|
64 |
-
anomalies = detect_anomalies(df)
|
65 |
-
fig = plot_charts(df)
|
66 |
-
return anomalies, fig
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
70 |
gr.Markdown("# π‘οΈ Financial Abuse & Anomaly Detection App")
|
71 |
-
gr.Markdown("Upload your **
|
|
|
|
|
|
|
|
|
72 |
|
73 |
with gr.Row():
|
74 |
-
|
75 |
-
submit_btn = gr.Button("π Detect Anomalies", variant="primary")
|
76 |
|
77 |
with gr.Tab("π Anomalies Detected"):
|
78 |
-
|
|
|
79 |
|
80 |
-
with gr.Tab("π Transaction
|
81 |
-
chart_output = gr.Plot(
|
82 |
|
83 |
-
|
|
|
84 |
|
85 |
interface.launch(share=True)
|
|
|
3 |
import joblib
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
6 |
+
import io
|
7 |
|
8 |
# Load trained model
|
9 |
model = joblib.load("anomaly_detector_rf_model.pkl")
|
|
|
28 |
original_df["is_anomalous"] = preds
|
29 |
|
30 |
anomalies = original_df[original_df["is_anomalous"] == 1]
|
31 |
+
return original_df, anomalies[["transaction_id", "merchant", "location", "amount", "is_anomalous"]]
|
32 |
|
33 |
+
# Function to plot charts
|
34 |
def plot_charts(df):
|
35 |
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
|
36 |
|
|
|
59 |
plt.tight_layout()
|
60 |
return fig
|
61 |
|
62 |
+
# Function to generate summary + charts + file
|
63 |
def app_interface(csv_file):
|
64 |
df = pd.read_csv(csv_file)
|
65 |
+
full_df, anomalies = detect_anomalies(df)
|
|
|
|
|
66 |
|
67 |
+
total = len(full_df)
|
68 |
+
anom_count = len(anomalies)
|
69 |
+
percent = (anom_count / total) * 100 if total > 0 else 0
|
70 |
+
|
71 |
+
summary = (
|
72 |
+
f"π’ **Total Transactions**: {total}\n"
|
73 |
+
f"β οΈ **Anomalies Detected**: {anom_count}\n"
|
74 |
+
f"π **Anomaly Percentage**: {percent:.2f}%"
|
75 |
+
)
|
76 |
+
|
77 |
+
# Convert anomalies to CSV bytes for download
|
78 |
+
csv_bytes = anomalies.to_csv(index=False).encode()
|
79 |
+
download = io.BytesIO(csv_bytes)
|
80 |
+
|
81 |
+
fig = plot_charts(full_df)
|
82 |
+
|
83 |
+
return summary, anomalies, fig, download
|
84 |
+
|
85 |
+
# Gradio App with UI
|
86 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
87 |
gr.Markdown("# π‘οΈ Financial Abuse & Anomaly Detection App")
|
88 |
+
gr.Markdown("Upload your **transaction CSV** to detect anomalies and view insights.")
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
file_input = gr.File(label="π Upload CSV File", file_types=[".csv"])
|
92 |
+
detect_button = gr.Button("π¨ Run Detection", variant="primary")
|
93 |
|
94 |
with gr.Row():
|
95 |
+
summary_box = gr.Markdown("")
|
|
|
96 |
|
97 |
with gr.Tab("π Anomalies Detected"):
|
98 |
+
result_table = gr.Dataframe(label="π΄ Anomalies")
|
99 |
+
download_btn = gr.File(label="β¬οΈ Download Detected Anomalies")
|
100 |
|
101 |
+
with gr.Tab("π Transaction Charts"):
|
102 |
+
chart_output = gr.Plot()
|
103 |
|
104 |
+
detect_button.click(fn=app_interface, inputs=file_input,
|
105 |
+
outputs=[summary_box, result_table, chart_output, download_btn])
|
106 |
|
107 |
interface.launch(share=True)
|