Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import joblib | |
# Load the trained model and preprocessing objects | |
model = joblib.load("fraud_detection_model.joblib") | |
label_encoders = joblib.load("label_encoders.joblib") | |
scaler = joblib.load("scaler.joblib") | |
# Streamlit app | |
st.title("Credit Card Fraud Detection") | |
# Input fields for user | |
st.header("Enter Transaction Details") | |
amount = st.number_input("Amount", min_value=0.0) | |
merchant_id = st.text_input("Merchant ID") | |
transaction_type = st.selectbox("Transaction Type", ["purchase", "refund"]) | |
location = st.text_input("Location") | |
# Preprocess input data | |
if st.button("Predict"): | |
# Create a DataFrame from the input | |
input_data = pd.DataFrame({ | |
"Amount": [amount], | |
"MerchantID": [merchant_id], | |
"TransactionType": [transaction_type], | |
"Location": [location] | |
}) | |
# Apply label encoding to categorical columns | |
for col, le in label_encoders.items(): | |
input_data[col] = le.transform(input_data[col]) | |
# Scale the "Amount" column | |
input_data["Amount"] = scaler.transform(input_data[["Amount"]]) | |
# Make prediction | |
prediction = model.predict(input_data) | |
prediction_proba = model.predict_proba(input_data) | |
# Display the result | |
if prediction[0] == 1: | |
st.error("Fraudulent Transaction Detected!") | |
else: | |
st.success("Legitimate Transaction") | |
st.write(f"Probability: {prediction_proba[0][1]:.2f}") |