Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,47 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
3 |
|
4 |
-
# Load
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
-
st.
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
score = prediction[0]["score"]
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
else:
|
25 |
-
st.success(
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
|
5 |
+
# Load the trained model and preprocessing objects
|
6 |
+
model = joblib.load("fraud_detection_model.joblib")
|
7 |
+
label_encoders = joblib.load("label_encoders.joblib")
|
8 |
+
scaler = joblib.load("scaler.joblib")
|
9 |
|
10 |
+
# Streamlit app
|
11 |
+
st.title("Credit Card Fraud Detection")
|
12 |
|
13 |
+
# Input fields for user
|
14 |
+
st.header("Enter Transaction Details")
|
15 |
+
amount = st.number_input("Amount", min_value=0.0)
|
16 |
+
merchant_id = st.text_input("Merchant ID")
|
17 |
+
transaction_type = st.selectbox("Transaction Type", ["purchase", "refund"])
|
18 |
+
location = st.text_input("Location")
|
19 |
|
20 |
+
# Preprocess input data
|
21 |
+
if st.button("Predict"):
|
22 |
+
# Create a DataFrame from the input
|
23 |
+
input_data = pd.DataFrame({
|
24 |
+
"Amount": [amount],
|
25 |
+
"MerchantID": [merchant_id],
|
26 |
+
"TransactionType": [transaction_type],
|
27 |
+
"Location": [location]
|
28 |
+
})
|
29 |
|
30 |
+
# Apply label encoding to categorical columns
|
31 |
+
for col, le in label_encoders.items():
|
32 |
+
input_data[col] = le.transform(input_data[col])
|
|
|
33 |
|
34 |
+
# Scale the "Amount" column
|
35 |
+
input_data["Amount"] = scaler.transform(input_data[["Amount"]])
|
36 |
+
|
37 |
+
# Make prediction
|
38 |
+
prediction = model.predict(input_data)
|
39 |
+
prediction_proba = model.predict_proba(input_data)
|
40 |
+
|
41 |
+
# Display the result
|
42 |
+
if prediction[0] == 1:
|
43 |
+
st.error("Fraudulent Transaction Detected!")
|
44 |
else:
|
45 |
+
st.success("Legitimate Transaction")
|
46 |
+
|
47 |
+
st.write(f"Probability: {prediction_proba[0][1]:.2f}")
|