Anomaly / app.py
Ujeshhh's picture
Update app.py
dd8c606 verified
raw
history blame
1.39 kB
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)