|
import pandas as pd |
|
import joblib |
|
import gradio as gr |
|
|
|
|
|
model = joblib.load("anomaly_detector_rf_model.pkl") |
|
|
|
|
|
feature_cols = ['hour', 'day_of_week', 'is_weekend', 'amount_zscore', 'log_amount', |
|
'type_atm_withdrawal', 'type_credit', 'type_debit', 'merchant_encoded'] |
|
|
|
def detect_anomalies(file_path): |
|
|
|
df = pd.read_csv(file_path) |
|
|
|
|
|
if not all(col in df.columns for col in feature_cols): |
|
missing_cols = [col for col in feature_cols if col not in df.columns] |
|
return f"Missing columns in dataset: {missing_cols}" |
|
|
|
|
|
df['is_anomalous'] = model.predict(df[feature_cols]) |
|
|
|
|
|
anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']] |
|
|
|
|
|
anomalies.to_csv("predicted_anomalies.csv", index=False) |
|
|
|
return anomalies |
|
|
|
|
|
interface = gr.Interface( |
|
fn=detect_anomalies, |
|
inputs=gr.File(label="Upload CSV File"), |
|
outputs=gr.Dataframe(label="Predicted Anomalies"), |
|
title="Anomaly Detection System", |
|
description="Upload a transaction dataset to detect anomalies." |
|
) |
|
|
|
if __name__ == "__main__": |
|
interface.launch(share=True) |
|
|