File size: 1,444 Bytes
aaddfff
53290b0
 
aaddfff
53290b0
 
 
 
aaddfff
53290b0
 
aaddfff
53290b0
 
 
 
 
 
aaddfff
53290b0
 
 
 
 
 
 
 
 
aaddfff
53290b0
 
 
aaddfff
53290b0
 
 
 
 
 
 
 
 
 
aaddfff
53290b0
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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}")