import gradio as gr import numpy as np import pandas as pd import pickle import matplotlib.pyplot as plt # Load pre-trained models and encoders with open('is_fraud.pkl', 'rb') as f: is_fraud_model = pickle.load(f) with open('fraud_type.pkl', 'rb') as f: fraud_type_model = pickle.load(f) # Load label encoders for features encoders = {} for feature in ['card_type', 'location', 'purchase_category', 'time_of_day']: with open(f'{feature}.pkl', 'rb') as f: encoders[feature] = pickle.load(f) with open('fraud_type_le.pkl', 'rb') as f: fraud_type_encoder = pickle.load(f) # Define the prediction function def predict_fraud(amount, card_type, location, purchase_category, customer_age, time_of_day): # Preprocess input input_data = pd.DataFrame({ 'amount': [amount], 'card_type': [card_type], 'location': [location], 'purchase_category': [purchase_category], 'customer_age': [customer_age], 'time_of_day': [time_of_day] }) # Label encode the inputs for feature in ['card_type', 'location', 'purchase_category', 'time_of_day']: input_data[feature] = encoders[feature].transform(input_data[feature]) # Fraud Detection (Binary Classification) is_fraud_pred_prob = is_fraud_model.predict_proba(input_data)[0] is_fraud_class = np.argmax(is_fraud_pred_prob) # Fraud Type Classification (Multiclass Classification) fraud_type_pred_prob = fraud_type_model.predict_proba(input_data)[0] fraud_type_class = fraud_type_encoder.inverse_transform([np.argmax(fraud_type_pred_prob)])[0] # Bar charts for probabilities fig, axes = plt.subplots(1, 2, figsize=(12, 5)) # Plot Fraud Detection Probabilities axes[0].bar(['Not Fraudulent', 'Fraudulent'], is_fraud_pred_prob) axes[0].set_title('Fraud Detection Probability') axes[0].set_ylabel('Probability') if is_fraud_class==1: # Plot Fraud Type Probabilities fraud_types = fraud_type_encoder.classes_ axes[1].bar(fraud_types, fraud_type_pred_prob) axes[1].set_title('Fraud Type Probability') axes[1].set_ylabel('Probability') plt.xticks(rotation=45, ha="right") plt.tight_layout() return f"Fraud Detection( {max(is_fraud_pred_prob)*100}% Confidence): {'Fraudulent' if is_fraud_class == 1 else 'Not Fraudulent'}", f"Predicted Fraud Type: {fraud_type_class}" if fraud_type_class==1 else "", fig # Gradio Interface Setup interface = gr.Interface( fn=predict_fraud, inputs=[ gr.Number(label="Transaction Amount"), gr.Dropdown(choices=['MasterCard', 'Visa' ,'Rupay'],label="Card Type"), gr.Dropdown(choices=['Surat' ,'Hyderabad' ,'Kolkata' ,'Mumbai' ,'Delhi' ,'Chennai', 'Jaipur', 'Ahmedabad', 'Bangalore' ,'Pune'],label="Location"), gr.Dropdown(choices=['POS' ,'Digital'],label="Purchase Category"), gr.Number(label="Customer Age"), gr.Dropdown(choices=['night', 'morning', 'afternoon', 'evening'],label="Time of Day") ], outputs=[ gr.Markdown(label="Fraud Detection Result"), gr.Markdown(label="Fraud Type Result"), gr.Plot(label="Probabilities Bar Chart") ], title="Fraud Detection & Fraud Type Prediction", description="By detecting fraudulent transactions, this model helps protect users and businesses in the Indian digital landscape, fostering a safer and more trustworthy online environment.",theme=gr.themes.Soft()) # Launch Gradio Interface interface.launch()