Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,59 +4,52 @@ import pickle
|
|
4 |
import streamlit.components.v1 as components
|
5 |
from sklearn.preprocessing import LabelEncoder
|
6 |
|
7 |
-
|
8 |
def load_model():
|
9 |
-
|
|
|
10 |
|
11 |
-
|
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 |
-
|
19 |
-
return
|
20 |
|
21 |
def transform(le, text):
|
22 |
-
|
23 |
-
return text[0]
|
24 |
|
25 |
def app_design(le):
|
26 |
-
# Add input fields for High, Open, and Low values
|
27 |
-
# image = 'Ramdevs2'
|
28 |
-
# st.image(image, use_column_width=True)
|
29 |
-
|
30 |
st.subheader("Enter the following values:")
|
31 |
|
32 |
-
step = st.number_input("Step: represents a unit of time where 1 step equals 1 hour")
|
33 |
typeup = st.selectbox('Type of online transaction', ('PAYMENT', 'TRANSFER', 'CASH_OUT', 'DEBIT', 'CASH_IN'))
|
34 |
typeup = transform(le, [typeup])
|
35 |
-
amount = st.number_input("The amount of the transaction")
|
|
|
36 |
nameOrig = st.text_input("Transaction ID")
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
nameDest = st.text_input("Recipient ID")
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
isFlaggedFraud = st.selectbox('IsFlaggedFraud', ('Yes', 'No'))
|
45 |
isFlaggedFraud = transform(le, [isFlaggedFraud])
|
|
|
|
|
46 |
|
47 |
-
# Create a feature list from the user inputs
|
48 |
-
features = [[step, typeup, amount, nameOrig, oldbalanceOrg, newbalanceOrig, nameDest, 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("""
|
|
|
4 |
import streamlit.components.v1 as components
|
5 |
from sklearn.preprocessing import LabelEncoder
|
6 |
|
7 |
+
@st.cache_resource
|
8 |
def load_model():
|
9 |
+
with open('online_payment_fraud_detection_randomforest.pkl', 'rb') as f:
|
10 |
+
return pickle.load(f)
|
11 |
|
12 |
+
@st.cache_resource
|
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 |
+
features = np.array(features)
|
19 |
+
return str(model.predict(features)[0])
|
20 |
|
21 |
def transform(le, text):
|
22 |
+
return le.transform(text)[0]
|
|
|
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", value=0)
|
28 |
typeup = st.selectbox('Type of online transaction', ('PAYMENT', 'TRANSFER', 'CASH_OUT', 'DEBIT', 'CASH_IN'))
|
29 |
typeup = transform(le, [typeup])
|
30 |
+
amount = st.number_input("The amount of the transaction", value=0.0)
|
31 |
+
|
32 |
nameOrig = st.text_input("Transaction ID")
|
33 |
+
oldbalanceOrg = st.number_input("Balance before the transaction", value=0.0)
|
34 |
+
newbalanceOrig = st.number_input("Balance after the transaction", value=0.0)
|
35 |
+
|
36 |
nameDest = st.text_input("Recipient ID")
|
37 |
+
oldbalanceDest = st.number_input("Initial balance of recipient before the transaction", value=0.0)
|
38 |
+
newbalanceDest = st.number_input("The new balance of recipient after the transaction", value=0.0)
|
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("🚨 Online payment fraud detected!")
|
51 |
else:
|
52 |
+
st.success("✅ No online payment fraud detected!")
|
53 |
|
54 |
def about_RamDevs():
|
55 |
components.html("""
|