Ujeshhh commited on
Commit
235d85c
Β·
verified Β·
1 Parent(s): 5d337dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
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 generate charts
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
- # Gradio Interface logic
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
- # Launching with UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  with gr.Blocks(theme=gr.themes.Soft()) as interface:
70
  gr.Markdown("# πŸ›‘οΈ Financial Abuse & Anomaly Detection App")
71
- gr.Markdown("Upload your **financial transaction CSV** to detect suspicious activity and view insightful visualizations.")
 
 
 
 
72
 
73
  with gr.Row():
74
- file_input = gr.File(label="πŸ“ Upload Transaction CSV", file_types=[".csv"])
75
- submit_btn = gr.Button("πŸ” Detect Anomalies", variant="primary")
76
 
77
  with gr.Tab("πŸ“‹ Anomalies Detected"):
78
- result_df = gr.Dataframe(label="πŸ”΄ Detected Suspicious Transactions")
 
79
 
80
- with gr.Tab("πŸ“Š Transaction Insights"):
81
- chart_output = gr.Plot(label="πŸ“ˆ Transaction Summary Charts")
82
 
83
- submit_btn.click(fn=app_interface, inputs=file_input, outputs=[result_df, chart_output])
 
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)