Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,52 +4,59 @@ import pickle
|
|
4 |
import streamlit.components.v1 as components
|
5 |
from sklearn.preprocessing import LabelEncoder
|
6 |
|
7 |
-
|
8 |
def load_model():
|
9 |
-
|
10 |
-
return pickle.load(f)
|
11 |
|
12 |
-
|
13 |
def load_label_encoder():
|
14 |
with open('label_encoder.pkl', 'rb') as f:
|
15 |
return pickle.load(f)
|
16 |
|
|
|
17 |
def model_prediction(model, features):
|
18 |
-
|
19 |
-
return
|
20 |
|
21 |
def transform(le, text):
|
22 |
-
|
|
|
23 |
|
24 |
def app_design(le):
|
25 |
st.subheader("Enter the following values:")
|
26 |
|
27 |
-
step = st.number_input("Step: represents a unit of time where 1 step equals 1 hour"
|
|
|
28 |
typeup = st.selectbox('Type of online transaction', ('PAYMENT', 'TRANSFER', 'CASH_OUT', 'DEBIT', 'CASH_IN'))
|
29 |
typeup = transform(le, [typeup])
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
isFlaggedFraud = st.selectbox('IsFlaggedFraud', ('Yes', 'No'))
|
41 |
isFlaggedFraud = transform(le, [isFlaggedFraud])
|
42 |
|
|
|
|
|
43 |
features = [[step, typeup, amount, 0, oldbalanceOrg, newbalanceOrig, 0, oldbalanceDest, newbalanceDest, isFlaggedFraud]]
|
44 |
-
|
|
|
45 |
model = load_model()
|
46 |
-
|
|
|
47 |
if st.button('Predict Online Payment Fraud'):
|
48 |
predicted_value = model_prediction(model, features)
|
49 |
if predicted_value == '1':
|
50 |
-
st.success("
|
51 |
else:
|
52 |
-
st.success("✅ No online payment fraud detected
|
53 |
|
54 |
def about_RamDevs():
|
55 |
components.html("""
|
|
|
4 |
import streamlit.components.v1 as components
|
5 |
from sklearn.preprocessing import LabelEncoder
|
6 |
|
7 |
+
# Load the pickled model
|
8 |
def load_model():
|
9 |
+
return pickle.load(open('online_payment_fraud_detection_randomforest.pkl', 'rb'))
|
|
|
10 |
|
11 |
+
# Load the LabelEncoder
|
12 |
def load_label_encoder():
|
13 |
with open('label_encoder.pkl', 'rb') as f:
|
14 |
return pickle.load(f)
|
15 |
|
16 |
+
# Function for model prediction
|
17 |
def model_prediction(model, features):
|
18 |
+
predicted = str(model.predict(features)[0])
|
19 |
+
return predicted
|
20 |
|
21 |
def transform(le, text):
|
22 |
+
text = le.transform(text)
|
23 |
+
return text[0]
|
24 |
|
25 |
def app_design(le):
|
26 |
st.subheader("Enter the following values:")
|
27 |
|
28 |
+
step = st.number_input("Step: represents a unit of time where 1 step equals 1 hour")
|
29 |
+
|
30 |
typeup = st.selectbox('Type of online transaction', ('PAYMENT', 'TRANSFER', 'CASH_OUT', 'DEBIT', 'CASH_IN'))
|
31 |
typeup = transform(le, [typeup])
|
32 |
+
|
33 |
+
amount = st.number_input("The amount of the transaction")
|
34 |
+
|
35 |
+
nameOrig = st.text_input("Transaction ID") # Don't transform
|
36 |
+
oldbalanceOrg = st.number_input("Balance before the transaction")
|
37 |
+
newbalanceOrig = st.number_input("Balance after the transaction")
|
38 |
+
|
39 |
+
nameDest = st.text_input("Recipient ID") # Don't transform
|
40 |
+
oldbalanceDest = st.number_input("Initial balance of recipient before the transaction")
|
41 |
+
newbalanceDest = st.number_input("The new balance of recipient after the transaction")
|
42 |
+
|
43 |
isFlaggedFraud = st.selectbox('IsFlaggedFraud', ('Yes', 'No'))
|
44 |
isFlaggedFraud = transform(le, [isFlaggedFraud])
|
45 |
|
46 |
+
# Create a feature list from the user inputs
|
47 |
+
# ➔ set nameOrig and nameDest as 0
|
48 |
features = [[step, typeup, amount, 0, oldbalanceOrg, newbalanceOrig, 0, oldbalanceDest, newbalanceDest, isFlaggedFraud]]
|
49 |
+
|
50 |
+
# Load the model
|
51 |
model = load_model()
|
52 |
+
|
53 |
+
# Make a prediction when the user clicks the "Predict" button
|
54 |
if st.button('Predict Online Payment Fraud'):
|
55 |
predicted_value = model_prediction(model, features)
|
56 |
if predicted_value == '1':
|
57 |
+
st.success("⚠️ Online payment fraud detected")
|
58 |
else:
|
59 |
+
st.success("✅ No online payment fraud detected")
|
60 |
|
61 |
def about_RamDevs():
|
62 |
components.html("""
|