Ade1ola's picture
Update app.py
b166aa5 verified
import gradio as gr
import pandas as pd
import xgboost as xgb
import joblib
import os
# βœ… Load the trained XGBoost model
model_path = "xgboost_fraud_model.pkl" # Ensure this file is uploaded to Hugging Face
if not os.path.exists(model_path):
raise FileNotFoundError(f"❌ Model file '{model_path}' not found!")
model = joblib.load(model_path)
# βœ… Load the sample transaction dataset
sample_file_path = "Sample_Data_Matching_Model.csv" # Ensure this file is uploaded to Hugging Face
if not os.path.exists(sample_file_path):
raise FileNotFoundError(f"❌ Sample data '{sample_file_path}' not found!")
sample_df = pd.read_csv(sample_file_path)
# βœ… Define the required feature columns
REQUIRED_COLS = [
'index', 'v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9', 'v10',
'v11', 'v12', 'v13', 'v14', 'v15', 'v16', 'v17', 'v18', 'v19', 'v20',
'v21', 'v22', 'v23', 'v24', 'v25', 'v26', 'v27', 'v28', 'dow',
'is_weekend', 'days_between', 'max_days_between', 'monthly_txns_lag_30',
'weekly_txns_lag_30', 'cls_lag_30'
]
def predict_fraud(file, use_sample):
try:
# Load data based on user input
if use_sample:
df = sample_df.copy() # Use predefined sample data
elif file is not None:
df = pd.read_csv(file) # Load user-uploaded file
else:
return "❌ No file provided!", None, None # Error, No preview, No download
# βœ… Ensure required columns exist
missing_cols = [col for col in REQUIRED_COLS if col not in df.columns]
if missing_cols:
return f"❌ Error: Missing columns: {', '.join(missing_cols)}", None, None
df = df[REQUIRED_COLS] # Keep only necessary columns
# βœ… Make fraud predictions
df["fraud_probability"] = model.predict_proba(df)[:, 1]
df["fraud_prediction"] = (df["fraud_probability"] > 0.5).astype(int)
# βœ… Select preview (First 5 Transactions)
preview_df = df[['index', 'fraud_probability', 'fraud_prediction']].head(5)
# βœ… Save full results for download
output_csv = "fraud_predictions.csv"
df.to_csv(output_csv, index=False)
return None, preview_df, output_csv # βœ… No error, preview, and download
except Exception as e:
return f"❌ Unexpected Error: {str(e)}", None, None # Ensure other outputs are `None`
# βœ… Gradio App Layout with Styling
with gr.Blocks(css="""
.gradio-container {max-width: 750px !important; margin: auto;}
#header {text-align: center; font-size: 1.5em; font-weight: bold; margin-bottom: 10px;}
#subheader {text-align: center; font-size: 1em; color: gray; margin-bottom: 20px;}
.gr-box {border-radius: 10px; padding: 15px; background: #f9f9f9;}
""") as app:
gr.HTML("<div id='header'>πŸš€ Fraud Detection System</div>")
gr.HTML("<div id='subheader'>Upload a transaction file OR click 'Use Sample Data' to test.</div>")
with gr.Group():
with gr.Row():
file_input = gr.File(label="πŸ“‚ Upload CSV File")
use_sample_toggle = gr.Checkbox(label="βœ… Use Sample Data", value=False)
submit_btn = gr.Button("πŸš€ Predict Fraud")
with gr.Group():
error_message = gr.Markdown("") # βœ… Error Message Output
preview_output = gr.DataFrame(label="πŸ” Fraud Prediction Preview (5 Transactions)")
download_output = gr.File(label="πŸ“₯ Download Full Results")
# βœ… Link button to function
submit_btn.click(
predict_fraud,
inputs=[file_input, use_sample_toggle],
outputs=[error_message, preview_output, download_output] # βœ… FIXED: Assign all outputs
)
app.launch()