-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
328 lines (310 loc) · 11 KB
/
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# importing required libraries
import pandas as pd
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import plotly.graph_objs as go
import pickle
import xgboost
# initiate the app
external_stylesheets=["./assets/typography.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
# read more about inline-block & flex
# https://www.geeksforgeeks.org/what-is-the-difference-between-inline-flex-and-inline-block-in-css/
# format the app
colors = {"background": "#111111", "text": "#009E73", "box": "#7F7F7F"}
app.layout = html.Div(
children=[
html.H1(
"Fraud Detection",
style={
"textAlign": "center",
"color": colors["text"],
"paddingTop": "10px",
},
),
# the first line of menus
html.Div(
[
# step
html.Div(
[
html.H3(
"OTP Requested (times):", style={"paddingRight": "30px"},
), # this style controls the title
dcc.Input(
id="OTP_Req",
type="number",
min=0,
value=1,
style={
"fontsize": 15,
"width": 60,
"height": 25,
"color": colors[
"text"
], # this style controls the input box
},
),
],
style={"display": "inline-block"},
),
# Amount in Transaction
html.Div(
[
html.H3(
"Amount in Transaction:", style={"paddingRight": "30px"}
),
dcc.Input(
id="amount",
type="number",
min=0,
value=10000,
style={
"fontsize": 15,
"width": 60,
"height": 25,
"color": colors["text"],
},
),
],
style={"display": "inline-block"},
),
# Flagged Site
html.Div(
[
html.H3("Flagged Site:", style={"paddingRight": "30px"},),
dcc.Dropdown(
id="isFlaggedSite",
options=[
{"label": "No", "value": 0},
{"label": "Yes", "value": 1},
],
value=1,
clearable=False,
style={
"fontsize": 15,
"width": 60,
"height": 30,
"color": colors["text"],
},
),
],
style={"display": "inline-block"},
),
# unrecognized Device
html.Div(
[
html.H3(
"Unrecongnized Device: ", style={"paddingRight": "30px"},
),
dcc.Dropdown(
id="isUnrecognizedDevice",
options=[
{"label": "No", "value": 0},
{"label": "Yes", "value": 1},
],
value=1,
clearable=False,
style={
"fontsize": 15,
"width": 60,
"height": 30,
"color": colors["text"],
},
),
],
style={"display": "inline-block"},
),
# unrecognized location
html.Div(
[
html.H3(
"Unrecongnized Location: ", style={"paddingRight": "30px"},
),
dcc.Dropdown(
id="isOutsideLocation",
options=[
{"label": "No", "value": 0},
{"label": "Yes", "value": 1},
],
value=1,
clearable=False,
style={
"fontsize": 15,
"width": 60,
"height": 30,
"color": colors["text"],
},
),
],
style={"display": "inline-block"},
),
# the submit button
html.Div(
[
html.Button(
id="submit-button",
children="Find",
n_clicks=0,
style={"fontSize": 20, "color": colors["text"]},
)
],
style={"display": "inline-block"},
),
],
style={
"display": "flex",
"align-items": "center", # vertical alignment
"justify-content": "center", # horizontal alignment
"color": colors["text"],
"background-color": colors[
"background"
], # this style controls the entire first line of input
},
),
# the second line of menus
html.Div(
[],
style={
"display": "flex",
"align-items": "center",
"justify-content": "center",
"color": colors[
"text"
], # this style controls the entire second line of input
},
),
# the graphs
dcc.Graph(id="Verdict"),
dcc.Graph(id="Coef_Value"),
dcc.Graph(id="Model_Evaluation"),
],
style={"backgroundColor": colors["background"], "height": "100%", "width": "100%",},
)
# app functions
@app.callback(
[
Output(component_id="Verdict", component_property="figure"),
Output(component_id="Coef_Value", component_property="figure"),
Output(component_id="Model_Evaluation", component_property="figure"),
],
[Input("submit-button", "n_clicks")],
[
State("OTP_Req", "value"),
State("amount", "value"),
State("isFlaggedSite", "value"),
State("isUnrecognizedDevice", "value"),
State("isOutsideLocation", "value"),
],
)
# start the function
def Fraud_Verdict(
n_clicks, OTP_Req, amount, isFlaggedSite, isUnrecognizedDevice, isOutsideLocation,
):
# test case plot
test_case = [
OTP_Req,
amount,
isFlaggedSite,
isUnrecognizedDevice,
isOutsideLocation,
]
test_case = pd.DataFrame(test_case).T
test_case.columns = [
"OTP_Req",
"amount",
"isFlaggedSite",
"isUnrecognizedDevice",
"isOutsideLocation",
]
loaded_model = pickle.load(open("fraud_det.dat", "rb"))
test_case_verdict = loaded_model.predict(test_case)
verdict = np.where(
test_case_verdict == 0, "Regular Transaction", "Suspicious Transaction"
)[0]
verdict_col = np.where(test_case_verdict == 0, "#00ff00", "#ff0000")[0]
testcase_prob = loaded_model.predict_proba(test_case).tolist()[0]
verdict_plot = go.Bar(
x=["Regular Case", "Fraud Case"], y=testcase_prob, marker={"color": "#008080"}
)
verdict_layout = go.Layout(
title=dict(text="Verdict: " + verdict, font=dict(color=verdict_col)),
xaxis=dict(title="Case Category"),
yaxis=dict(title="Probability"),
)
verdict_fig = go.Figure(data=[verdict_plot], layout=verdict_layout)
verdict_fig.update_layout(
plot_bgcolor=colors["background"],
font=dict(color=colors["text"]),
paper_bgcolor=colors["background"],
)
# Model Coef Figure
ft_importance = pd.DataFrame(
loaded_model.best_estimator_.feature_importances_,
index=[
"OTP Requested",
"Amount in Transaction",
"Is the Site Flagged",
"Is the Device Unrecognized",
"Is the Location Unusual",
],
columns=["Coef"],
)
coef_data = go.Pie(
labels=ft_importance.index,
values=ft_importance["Coef"],
hole=0.3,
name="",
hovertemplate="%{label}: %{percent}",
)
coef_layout = go.Layout(
title="Coefficient Importance",
xaxis=dict(title=""),
yaxis=dict(title=""),
# width="100%",
height=600,
)
coef_fig = go.Figure(data=[coef_data], layout=coef_layout)
coef_fig.update_layout(
plot_bgcolor=colors["background"], # color of the plot background
font=dict(color=colors["text"]),
paper_bgcolor=colors["background"], # color of the outside frame of the plot
)
# model evaluation table
evaluation = pd.read_csv("report.csv")
evaluation = evaluation.rename(columns={"Unnamed: 0": "Category"})
evaluation["Category"][0:2] = ["0", "1"]
evaluation.iloc[:, 1:] = evaluation.iloc[:, 1:].round(5)
eval_table = go.Table(
header=dict(
values=list(evaluation.columns), fill_color="paleturquoise", align="left"
),
cells=dict(
values=[
evaluation["Category"],
evaluation["precision"],
evaluation["recall"],
evaluation["f1-score"],
evaluation["support"],
],
fill_color="lavender",
align="left",
),
)
eval_layout = go.Layout(
title="Model Evaluaiton", xaxis=dict(title=""), yaxis=dict(title=""),
)
eval_fig = go.Figure(data=[eval_table], layout=eval_layout)
eval_fig.update_layout(
plot_bgcolor=colors["background"],
font=dict(color=colors["text"]),
paper_bgcolor=colors["background"],
)
return (verdict_fig, coef_fig, eval_fig)
# launch the app
if __name__ == "__main__":
app.run_server(debug=False, threaded=False)