Ujeshhh commited on
Commit
14bcb8f
·
verified ·
1 Parent(s): dd8c606

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -5,21 +5,25 @@ import gradio as gr
5
  # Load the trained model
6
  model = joblib.load("anomaly_detector_rf_model.pkl")
7
 
8
- # Define feature columns (exclude non-numeric ones if needed)
9
- feature_cols = ['hour', 'day_of_week', 'is_weekend', 'amount_zscore', 'log_amount',
10
- 'type_atm_withdrawal', 'type_credit', 'type_debit', 'merchant_encoded']
 
11
 
12
  def detect_anomalies(file_path):
13
  # Read the dataset
14
  df = pd.read_csv(file_path)
15
 
16
  # Ensure all features exist in the dataframe
17
- if not all(col in df.columns for col in feature_cols):
18
- missing_cols = [col for col in feature_cols if col not in df.columns]
19
  return f"Missing columns in dataset: {missing_cols}"
20
 
 
 
 
21
  # Make predictions
22
- df['is_anomalous'] = model.predict(df[feature_cols])
23
 
24
  # Filter anomalous transactions
25
  anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']]
 
5
  # Load the trained model
6
  model = joblib.load("anomaly_detector_rf_model.pkl")
7
 
8
+ # Define feature columns (include all used during training)
9
+ feature_cols = ['hour', 'day_of_week', 'is_weekend', 'amount', 'merchant_avg_amount',
10
+ 'amount_zscore', 'log_amount', 'type_atm_withdrawal', 'type_credit',
11
+ 'type_debit', 'merchant_encoded']
12
 
13
  def detect_anomalies(file_path):
14
  # Read the dataset
15
  df = pd.read_csv(file_path)
16
 
17
  # Ensure all features exist in the dataframe
18
+ missing_cols = [col for col in feature_cols if col not in df.columns]
19
+ if missing_cols:
20
  return f"Missing columns in dataset: {missing_cols}"
21
 
22
+ # Align feature order with model training
23
+ df = df[feature_cols]
24
+
25
  # Make predictions
26
+ df['is_anomalous'] = model.predict(df)
27
 
28
  # Filter anomalous transactions
29
  anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']]