|
|
|
import gradio as gr |
|
import pandas as pd |
|
import numpy as np |
|
import pickle |
|
from datetime import datetime |
|
|
|
|
|
with open("anomaly_detector_rf_model.pkl", "rb") as f: |
|
model = pickle.load(f) |
|
|
|
def detect_anomalies(df): |
|
df["log_amount"] = np.log1p(df["amount"]) |
|
df["amount_zscore"] = (df["amount"] - df["amount"].mean()) / df["amount"].std() |
|
df["transaction_date"] = pd.to_datetime(df["transaction_date"]) |
|
df["day_of_week"] = df["transaction_date"].dt.dayofweek |
|
df["hour"] = df["transaction_date"].dt.hour |
|
df["is_weekend"] = df["day_of_week"].isin([5, 6]).astype(int) |
|
|
|
features = ["amount", "log_amount", "amount_zscore", "day_of_week", "hour", "is_weekend"] |
|
df["is_anomalous"] = model.predict(df[features]) |
|
|
|
anomalies = df[df["is_anomalous"] == 1][["transaction_id", "amount", "merchant", "location", "transaction_date"]] |
|
|
|
|
|
csv_path = "/tmp/anomalies.csv" |
|
anomalies.to_csv(csv_path, index=False) |
|
|
|
return anomalies, csv_path |
|
|
|
|
|
def generate_summary(df): |
|
total = len(df) |
|
anomalous = df["is_anomalous"].sum() |
|
return f"Total Transactions: {total} | π¨ Anomalies: {anomalous} | β οΈ Rate: {anomalous/total:.2%}" |
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("## πΈ Financial Transaction Anomaly Detector") |
|
gr.Markdown("Upload your CSV with columns: `transaction_id`, `amount`, `merchant`, `location`, `transaction_date`") |
|
|
|
with gr.Row(): |
|
file_input = gr.File(file_types=[".csv"], label="π Upload Transaction File") |
|
download_button = gr.File(label="β¬οΈ Download Anomalies CSV") |
|
|
|
summary_text = gr.Textbox(label="π Summary", interactive=False) |
|
output_table = gr.Dataframe(label="π¨ Detected Anomalies") |
|
|
|
def app_interface(file): |
|
df = pd.read_csv(file.name) |
|
anomalies, csv_path = detect_anomalies(df) |
|
summary = generate_summary(df) |
|
return summary, anomalies, csv_path |
|
|
|
file_input.change(fn=app_interface, inputs=[file_input], outputs=[summary_text, output_table, download_button]) |
|
|
|
app.launch() |
|
|