File size: 3,763 Bytes
d532a4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import streamlit as st
import numpy as np
import tensorflow as tf
import pandas as pd
import requests
from streamlit_lottie import st_lottie

st.title("Automated Fraud Detection System Web app")
st.write("""

This app will helps us to track what type of transactions lead to fraud. I collected a dataset from [Kaggle repositry](https://www.kaggle.com/datasets/jainilcoder/online-payment-fraud-detection)
,which contains historical information about fraudulent transactions which can be used to detect fraud in online payments.
""")

def load_lottieurl(url: str):
    r = requests.get(url)
    if r.status_code != 200:
        return None
    return r.json()


lottie_url = "https://assets8.lottiefiles.com/packages/lf20_yhTqG2.json"

lottie_hello = load_lottieurl(lottie_url)

with st.sidebar:
    st_lottie(lottie_hello,quality='high')

st.sidebar.title('Users Features Explanation')
st.sidebar.markdown("**step**: represents a unit of time where 1 step equals 1 hour")
st.sidebar.markdown("**type**: type of online transaction")
st.sidebar.markdown('**amount**: the amount of the transaction')
st.sidebar.markdown('**oldbalanceOrg**: balance before the transaction')
st.sidebar.markdown('**newbalanceOrig**: balance after the transaction')
st.sidebar.markdown('**oldbalanceDest**: initial balance of recipient before the transaction')
st.sidebar.markdown('**newbalanceDest**: the new balance of recipient after the transaction')



st.header('User Input Features')

def user_input_features():
    step = st.number_input('Step', 0, 3)
    type = st.selectbox('Online Transaction Type', ("CASH IN", "CASH OUT", "DEBIT", "PAYMENT", "TRANSFER"))
    amount = st.number_input("Amount of the transaction")
    oldbalanceOrg = st.number_input("Old balance Origin")
    newbalanceOrig = st.number_input("New balance Origin")
    oldbalanceDest = st.number_input("Old Balance Destination")
    newbalanceDest = st.number_input("New Balance Destination")
    data = {'step': step,
            'type': type,
            'amount': amount,
            'oldbalanceOrg': oldbalanceOrg,
            'newbalanceOrig': newbalanceOrig,
            'oldbalanceDest': oldbalanceDest,
            'newbalanceDest': newbalanceDest}
    features = pd.DataFrame(data, index=[0])
    return features
input_df = user_input_features()

# Combines user input features with sample dataset
# This will be useful for the encoding phase
fraud_raw = pd.read_csv('samp_online.csv')
fraud = fraud_raw.drop(columns=['isFraud','nameOrig','nameDest','isFlaggedFraud'])
df = pd.concat([input_df,fraud],axis=0)

# Encoding of ordinal features


encode = ['type']
for col in encode:
    dummy = pd.get_dummies(df[col], prefix=col)
    df = pd.concat([df,dummy], axis=1)
    del df[col]
df = df[:1] # Selects only the first row (the user input data)

# Reads in saved classification model
if st.button("Predict"):
    load_clf = tf.keras.models.load_model('fraud.h5', compile=False)
    load_clf.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])




    # Apply model to make predictions

    y_probs = load_clf.predict(df)
    pred = tf.round(y_probs)
    pred = tf.cast(pred, tf.int32)

    st.markdown(
        """
    <style>
    [data-testid="stMetricValue"] {
        font-size: 25px;
    }
    </style>
    """,
        unsafe_allow_html=True,
    )

    if pred == 0:

        col1, col2 = st.columns(2)
        col1.metric("Prediction", value="Transaction is not fraudulent ")
        col2.metric("Confidence Level", value=f"{np.round(np.max(y_probs) * 100)}%")
    else:
        col1, col2 = st.columns(2)
        col1.metric("prediction", value="Transaction is fraudulent")
        col2.metric("Confidence Level", value=f"{np.round(np.max(y_probs) * 100)}%")