File size: 1,389 Bytes
dd8c606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pandas as pd
import joblib
import gradio as gr

# Load the trained model
model = joblib.load("anomaly_detector_rf_model.pkl")

# Define feature columns (exclude non-numeric ones if needed)
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):
    # Read the dataset
    df = pd.read_csv(file_path)
    
    # Ensure all features exist in the dataframe
    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}"
    
    # Make predictions
    df['is_anomalous'] = model.predict(df[feature_cols])
    
    # Filter anomalous transactions
    anomalies = df[df['is_anomalous'] == 1][['transaction_id', 'merchant', 'location', 'amount']]
    
    # Save to a new CSV
    anomalies.to_csv("predicted_anomalies.csv", index=False)
    
    return anomalies

# Gradio Interface
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)