Ujeshhh commited on
Commit
c6d403e
Β·
verified Β·
1 Parent(s): 3ec40df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -62
app.py CHANGED
@@ -1,83 +1,61 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
- import seaborn as sns
5
- import os
6
- import uuid
7
  import joblib
 
 
8
 
9
- # Load the model
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
- df = df.copy()
17
- df['is_anomalous'] = model.predict(df[expected_features])
18
- anomalies = df[df['is_anomalous'] == 1]
19
-
20
- # Save anomalies to temporary CSV file
21
- csv_filename = f"/tmp/anomalies_{uuid.uuid4().hex}.csv"
22
- anomalies.to_csv(csv_filename, index=False)
23
 
24
- return (
25
- anomalies[["transaction_id", "merchant", "location", "amount", "is_anomalous"]],
26
- csv_filename
27
- )
28
 
29
- def generate_summary(df):
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
- # Distribution of Amounts
39
- sns.histplot(df['amount'], bins=30, ax=ax[0], kde=True)
40
- ax[0].set_title('Transaction Amount Distribution')
41
 
42
- # Anomalies by Merchant
43
- anomaly_counts = df[df['is_anomalous'] == 1]['merchant'].value_counts().nlargest(10)
44
- sns.barplot(x=anomaly_counts.values, y=anomaly_counts.index, ax=ax[1])
45
- ax[1].set_title('Top 10 Merchants with Anomalies')
46
 
47
- plt.tight_layout()
48
- chart_path = f"/tmp/chart_{uuid.uuid4().hex}.png"
49
- plt.savefig(chart_path)
50
- plt.close()
51
- return chart_path
52
 
53
  def app_interface(file):
54
- df = pd.read_csv(file.name)
55
- anomalies, csv_path = detect_anomalies(df)
56
- summary = generate_summary(df)
57
- chart_path = generate_charts(df)
58
- return anomalies, summary, chart_path, csv_path
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="πŸ“‚ Upload Transaction CSV")
68
- analyze_btn = gr.Button("πŸš€ Analyze")
69
 
70
  with gr.Row():
71
- anomalies_output = gr.Dataframe(label="⚠️ Detected Anomalies", wrap=True)
72
- summary_output = gr.Textbox(label="πŸ“Š Summary")
73
-
74
- chart_output = gr.Image(label="πŸ“ˆ Analysis Charts")
75
- csv_download = gr.File(label="πŸ“ Download Anomalies CSV")
76
 
77
- analyze_btn.click(
78
- fn=app_interface,
79
- inputs=[file_input],
80
- outputs=[anomalies_output, summary_output, chart_output, csv_download]
81
- )
82
 
83
- demo.launch()
 
 
 
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()