import gradio as gr import pandas as pd import joblib # Load the trained model model = joblib.load("fraud_detector.pkl") def predict_fraud(file): try: # Read uploaded CSV file df = pd.read_csv(file.name) # Make sure the CSV has all columns the model expects required_columns = set(model.feature_names_in_) if not required_columns.issubset(df.columns): missing = required_columns - set(df.columns) return f"CSV is missing required columns: {missing}" # Select the features for prediction X = df[list(model.feature_names_in_)] # Run prediction preds = model.predict(X) # Map 0 and 1 to labels labels = ["Legitimate" if p == 0 else "Fraud" for p in preds] # Add predictions to dataframe and show first 10 rows df["Prediction"] = labels return df.head(10).to_html() except Exception as e: return f"Error: {str(e)}" # Build Gradio interface iface = gr.Interface( fn=predict_fraud, inputs=gr.File(label="Upload your CSV"), outputs=gr.HTML(), title="Credit Card Fraud Detector", description="Upload a CSV with transactions, and get fraud detection predictions." ) iface.launch()