Spaces:
Runtime error
Runtime error
Commit
·
d532a4c
1
Parent(s):
2b8efee
Upload 4 files
Browse files- app.py +118 -0
- fraud.h5 +3 -0
- requirements.txt +9 -0
- samp_online.csv +6 -0
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
import pandas as pd
|
5 |
+
import requests
|
6 |
+
from streamlit_lottie import st_lottie
|
7 |
+
|
8 |
+
st.title("Automated Fraud Detection System Web app")
|
9 |
+
st.write("""
|
10 |
+
|
11 |
+
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)
|
12 |
+
,which contains historical information about fraudulent transactions which can be used to detect fraud in online payments.
|
13 |
+
""")
|
14 |
+
|
15 |
+
def load_lottieurl(url: str):
|
16 |
+
r = requests.get(url)
|
17 |
+
if r.status_code != 200:
|
18 |
+
return None
|
19 |
+
return r.json()
|
20 |
+
|
21 |
+
|
22 |
+
lottie_url = "https://assets8.lottiefiles.com/packages/lf20_yhTqG2.json"
|
23 |
+
|
24 |
+
lottie_hello = load_lottieurl(lottie_url)
|
25 |
+
|
26 |
+
with st.sidebar:
|
27 |
+
st_lottie(lottie_hello,quality='high')
|
28 |
+
|
29 |
+
st.sidebar.title('Users Features Explanation')
|
30 |
+
st.sidebar.markdown("**step**: represents a unit of time where 1 step equals 1 hour")
|
31 |
+
st.sidebar.markdown("**type**: type of online transaction")
|
32 |
+
st.sidebar.markdown('**amount**: the amount of the transaction')
|
33 |
+
st.sidebar.markdown('**oldbalanceOrg**: balance before the transaction')
|
34 |
+
st.sidebar.markdown('**newbalanceOrig**: balance after the transaction')
|
35 |
+
st.sidebar.markdown('**oldbalanceDest**: initial balance of recipient before the transaction')
|
36 |
+
st.sidebar.markdown('**newbalanceDest**: the new balance of recipient after the transaction')
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
st.header('User Input Features')
|
41 |
+
|
42 |
+
def user_input_features():
|
43 |
+
step = st.number_input('Step', 0, 3)
|
44 |
+
type = st.selectbox('Online Transaction Type', ("CASH IN", "CASH OUT", "DEBIT", "PAYMENT", "TRANSFER"))
|
45 |
+
amount = st.number_input("Amount of the transaction")
|
46 |
+
oldbalanceOrg = st.number_input("Old balance Origin")
|
47 |
+
newbalanceOrig = st.number_input("New balance Origin")
|
48 |
+
oldbalanceDest = st.number_input("Old Balance Destination")
|
49 |
+
newbalanceDest = st.number_input("New Balance Destination")
|
50 |
+
data = {'step': step,
|
51 |
+
'type': type,
|
52 |
+
'amount': amount,
|
53 |
+
'oldbalanceOrg': oldbalanceOrg,
|
54 |
+
'newbalanceOrig': newbalanceOrig,
|
55 |
+
'oldbalanceDest': oldbalanceDest,
|
56 |
+
'newbalanceDest': newbalanceDest}
|
57 |
+
features = pd.DataFrame(data, index=[0])
|
58 |
+
return features
|
59 |
+
input_df = user_input_features()
|
60 |
+
|
61 |
+
# Combines user input features with sample dataset
|
62 |
+
# This will be useful for the encoding phase
|
63 |
+
fraud_raw = pd.read_csv('samp_online.csv')
|
64 |
+
fraud = fraud_raw.drop(columns=['isFraud','nameOrig','nameDest','isFlaggedFraud'])
|
65 |
+
df = pd.concat([input_df,fraud],axis=0)
|
66 |
+
|
67 |
+
# Encoding of ordinal features
|
68 |
+
|
69 |
+
|
70 |
+
encode = ['type']
|
71 |
+
for col in encode:
|
72 |
+
dummy = pd.get_dummies(df[col], prefix=col)
|
73 |
+
df = pd.concat([df,dummy], axis=1)
|
74 |
+
del df[col]
|
75 |
+
df = df[:1] # Selects only the first row (the user input data)
|
76 |
+
|
77 |
+
# Reads in saved classification model
|
78 |
+
if st.button("Predict"):
|
79 |
+
load_clf = tf.keras.models.load_model('fraud.h5', compile=False)
|
80 |
+
load_clf.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
# Apply model to make predictions
|
86 |
+
|
87 |
+
y_probs = load_clf.predict(df)
|
88 |
+
pred = tf.round(y_probs)
|
89 |
+
pred = tf.cast(pred, tf.int32)
|
90 |
+
|
91 |
+
st.markdown(
|
92 |
+
"""
|
93 |
+
<style>
|
94 |
+
[data-testid="stMetricValue"] {
|
95 |
+
font-size: 25px;
|
96 |
+
}
|
97 |
+
</style>
|
98 |
+
""",
|
99 |
+
unsafe_allow_html=True,
|
100 |
+
)
|
101 |
+
|
102 |
+
if pred == 0:
|
103 |
+
|
104 |
+
col1, col2 = st.columns(2)
|
105 |
+
col1.metric("Prediction", value="Transaction is not fraudulent ")
|
106 |
+
col2.metric("Confidence Level", value=f"{np.round(np.max(y_probs) * 100)}%")
|
107 |
+
else:
|
108 |
+
col1, col2 = st.columns(2)
|
109 |
+
col1.metric("prediction", value="Transaction is fraudulent")
|
110 |
+
col2.metric("Confidence Level", value=f"{np.round(np.max(y_probs) * 100)}%")
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
fraud.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:29da2797ef633f1d26b6f56ebf213e16c47fc8a42db5b4a29d0ea3d88192961a
|
3 |
+
size 946904
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.23.5
|
2 |
+
streamlit==1.15.1
|
3 |
+
tensorflow_cpu==2.8.0
|
4 |
+
pandas==1.5.2
|
5 |
+
requests==2.28.1
|
6 |
+
streamlit-lottie==0.0.3
|
7 |
+
|
8 |
+
|
9 |
+
|
samp_online.csv
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
step,type,amount,nameOrig,oldbalanceOrg,newbalanceOrig,nameDest,oldbalanceDest,newbalanceDest,isFraud,isFlaggedFraud
|
2 |
+
1,PAYMENT,9839.64,C1231006815,170136,160296.36,M1979787155,0,0,0,0
|
3 |
+
1,TRANSFER,181,C1305486145,181,0,C553264065,0,0,1,0
|
4 |
+
1,CASH_OUT,181,C840083671,181,0,C38997010,21182,0,1,0
|
5 |
+
1,DEBIT,5337.77,C712410124,41720,36382.23,C195600860,41898,40348.79,0,0
|
6 |
+
1,CASH_OUT,229133.94,C905080434,15325,0,C476402209,5083,51513.44,0,0
|