-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfraud_detection_app.py
124 lines (93 loc) · 4.22 KB
/
fraud_detection_app.py
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
120
121
import json
import requests
import streamlit as st
# App title
st.title("Credit Card Transaction Fraud Detection App")
#some image
st.image("img/credit_card_fraud.jpg")
# Description
st.write(
"""
## About
With the growth of e-commerce websites, people and financial companies rely on online services
to carry out their transactions that have led to an exponential increase in the credit card frauds.
Fraudulent credit card transactions lead to a loss of huge amount of money. The design of an
effective fraud detection system is necessary in order to reduce the losses incurred by the
customers and financial companies.
**This Streamlit App utilizes a Machine Learning model(XGBoost) API to detect potential fraud in credit card transactions.**
The notebook, model, documentation(FastApi script, Streamlit script) are available on [Github](/~https://github.com/Luissalazarsalinas/Fraud-Detection)
**Made by Luis Fernando Salazar S.**
"""
)
###################### Funtions to transform categorical variable #############################################
def type_transaction(content):
if content == "PAYMENT":
content = 0
elif content == "TRANSFER":
content = 1
elif content == "CASH_OUT":
content = 2
elif content == "DEBIT":
content = 3
elif content == "CASH_IN":
content = 4
return content
######################################### Input elements #############################################################
st.sidebar.header("Input user and transaction information")
# User data
sender_name = st.sidebar.text_input(" Sender Name ID")
receiver_name = st.sidebar.text_input(" Receiver Name ID")
## Transaction information
type_lebels = ("PAYMENT", "TRANSFER", "CASH_OUT", "DEBIT", "CASH_IN")
type = st.sidebar.selectbox(" Type of transaction", type_lebels)
step = st.sidebar.slider("Number of Hours it took the Transaction to complete:", min_value = 0, max_value = 744)
amount = st.sidebar.number_input("Amount in $",min_value=0, max_value=110000)
oldbalanceorg = st.sidebar.number_input("""Sender Balance Before Transaction was made""",min_value=0, max_value=110000)
newbalanceorg = st.sidebar.number_input("""Sender Balance After Transaction was made""",min_value=0, max_value=110000)
oldbalancedest = st.sidebar.number_input("""Recipient Balance Before Transaction was made""",min_value=0, max_value=110000)
newbalancedest = st.sidebar.number_input("""Recipient Balance After Transaction was made""",min_value=0, max_value=110000)
## flag
isflaggedfraud = "Non fraudulent"
if amount >= 200000:
isflaggedfraud = "Fraudulent transaction"
else:
isflaggedfraud = "Non fraudulent"
result_button = st.button("Detect Result")
if result_button:
## Features
data = {
"step": step,
"type": type_transaction(type),
"amount": amount,
"oldbalanceOrg": oldbalanceorg,
"newbalanceOrg": newbalanceorg,
"oldbalanceDest": oldbalancedest,
"newbalancedDest": newbalancedest
}
## Transaction detail
st.write(
f"""
## **Transaction Details**
#### **User informantion**:
Sender Name(ID): {sender_name}\n
Receiver Name(ID): {receiver_name}
#### **Transaction information**:
Number of Hours it took to complete: {step}\n
Type of Transaction: {type}\n
Amount Sent: {amount}$\n
Sender Balance Before Transaction: {oldbalanceorg}$\n
Sender Balance After Transaction: {newbalanceorg}$\n
Recepient Balance Before Transaction: {oldbalancedest}$\n
Recepient Balance After Transaction: {newbalancedest}$\n
System Flag Fraud Status(Transaction amount greater than $200000): {isflaggedfraud}
"""
)
st.write("""## **Prediction**""")
# inference from ml api
res = requests.post("https://fraud-detector-app.herokuapp.com/prediction", json= data)
json_str = json.dumps(res.json())
respon = json.loads(json_str)
if sender_name=='' or receiver_name == '':
st.write("Error! Please input Transaction ID or Names of Sender and Receiver!")
else:
st.write(f"""### The **'{type}'** transaction that took place between {sender_name} and {receiver_name} {respon[0]}.""")