Spaces:
Sleeping
Sleeping
| 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() | |