import streamlit as st import joblib import pandas as pd from sklearn.preprocessing import LabelEncoder, StandardScaler # Load the trained model model = joblib.load('random_forest_model.pkl') # Replace with your actual model file # Define encoders for categorical columns using the actual values label_encoders = { 'Day of Week': LabelEncoder().fit(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']), 'Type of Card': LabelEncoder().fit(['Visa', 'MasterCard']), 'Entry Mode': LabelEncoder().fit(['Tap', 'PIN', 'CVC']), 'Type of Transaction': LabelEncoder().fit(['POS', 'Online', 'ATM']), 'Merchant Group': LabelEncoder().fit(['Entertainment', 'Services', 'Restaurant', 'Electronics', 'Children', 'Fashion', 'Food', 'Products', 'Subscription', 'Gaming']), 'Country of Transaction': LabelEncoder().fit(['United Kingdom', 'USA', 'India', 'Russia', 'China']), 'Shipping Address': LabelEncoder().fit(['United Kingdom', 'USA', 'India', 'Russia', 'China']), 'Country of Residence': LabelEncoder().fit(['United Kingdom', 'USA', 'India', 'Russia', 'China']), 'Gender': LabelEncoder().fit(['M', 'F']) } # Define the scaler for numerical columns (use the scaler from training if available) scaler = StandardScaler() # Define the function to make predictions def predict_fraud(day_of_week, time, type_of_card, entry_mode, amount, type_of_transaction, merchant_group, country_of_transaction, shipping_address, country_of_residence, gender, age): # Create a DataFrame for the input input_data = pd.DataFrame({ 'Day of Week': [day_of_week], 'Time': [time], 'Type of Card': [type_of_card], 'Entry Mode': [entry_mode], 'Amount': [amount], 'Type of Transaction': [type_of_transaction], 'Merchant Group': [merchant_group], 'Country of Transaction': [country_of_transaction], 'Shipping Address': [shipping_address], 'Country of Residence': [country_of_residence], 'Gender': [gender], 'Age': [age], }) # Encode categorical columns for col in label_encoders: input_data[col] = label_encoders[col].transform(input_data[col]) # Standardize numerical features numerical_cols = ['Time', 'Amount', 'Age'] input_data[numerical_cols] = scaler.fit_transform(input_data[numerical_cols]) # Use the fitted scaler from training # Make the prediction prediction = model.predict(input_data) # Convert the numeric prediction to a meaningful label return "Fraud" if prediction[0] == 1 else "Not Fraud" # Custom CSS for background, fonts, and boxes st.markdown(""" """, unsafe_allow_html=True) # Streamlit app layout st.markdown("
Prediction: {prediction}
", unsafe_allow_html=True) else: st.markdown(f"Prediction: {prediction}
", unsafe_allow_html=True)