KarinaCardozo commited on
Commit
d2e141b
1 Parent(s): 9148a00

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +201 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import pickle
4
+
5
+ import os
6
+ MAIN_FOLDER = os.path.dirname(__file__)
7
+
8
+ # Define params names
9
+ PARAMS_NAME = [
10
+ "orderAmount",
11
+ "orderState",
12
+ "paymentMethodRegistrationFailure",
13
+ "paymentMethodType",
14
+ "paymentMethodProvider",
15
+ "paymentMethodIssuer",
16
+ "transactionAmount",
17
+ "transactionFailed",
18
+ "emailDomain",
19
+ "emailProvider",
20
+ "customerIPAddressSimplified",
21
+ "sameCity"
22
+ ]
23
+
24
+ # Load model
25
+ with open("model/modelo_proyecto_final.pkl", "rb") as f:
26
+ model = pickle.load(f)
27
+
28
+ # Columnas
29
+ COLUMNS_PATH = "model/categories_ohe_without_fraudulent.pickle"
30
+ with open(COLUMNS_PATH, 'rb') as handle:
31
+ ohe_tr = pickle.load(handle)
32
+
33
+ BINS_ORDER = os.path.join(MAIN_FOLDER, "model/saved_bins_order.pickle")
34
+ with open(BINS_ORDER, 'rb') as handle:
35
+ new_saved_bins_order = pickle.load(handle)
36
+
37
+ BINS_TRANSACTION = os.path.join(MAIN_FOLDER, "model/saved_bins_transaction.pickle")
38
+ with open(BINS_TRANSACTION, 'rb') as handle:
39
+ new_saved_bins_transaction = pickle.load(handle)
40
+
41
+ def predict(*args):
42
+ answer_dict = {}
43
+
44
+ for i in range(len(PARAMS_NAME)):
45
+ answer_dict[PARAMS_NAME[i]] = [args[i]]
46
+
47
+ # Crear dataframe
48
+ single_instance = pd.DataFrame.from_dict(answer_dict)
49
+
50
+ # Manejar puntos de corte o bins
51
+ single_instance["orderAmount"] = single_instance["orderAmount"].astype(float)
52
+ single_instance["orderAmount"] = pd.cut(single_instance['orderAmount'],
53
+ bins=new_saved_bins_order,
54
+ include_lowest=True)
55
+
56
+ single_instance["transactionAmount"] = single_instance["transactionAmount"].astype(int)
57
+ single_instance["transactionAmount"] = pd.cut(single_instance['transactionAmount'],
58
+ bins=new_saved_bins_order,
59
+ include_lowest=True)
60
+
61
+ # One hot encoding
62
+ single_instance_ohe = pd.get_dummies(single_instance).reindex(columns = ohe_tr).fillna(0)
63
+
64
+ prediction = model.predict(single_instance_ohe)
65
+
66
+ # Cast numpy.int64 to just a int
67
+ type_of_fraud = int(prediction[0])
68
+
69
+ # Adaptaci贸n respuesta
70
+ response = "Error parsing value"
71
+ if type_of_fraud == 0:
72
+ response = "False"
73
+ if type_of_fraud == 1:
74
+ response = "True"
75
+ if type_of_fraud == 2:
76
+ response = "Warning"
77
+
78
+ return response
79
+
80
+
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown(
83
+ """
84
+ # Prevenci贸n de Fraude 馃攳 馃攳
85
+ """
86
+ )
87
+
88
+ with gr.Row():
89
+ with gr.Column():
90
+
91
+ gr.Markdown(
92
+ """
93
+ ## Predecir si un cliente es fraudulento o no.
94
+ """
95
+ )
96
+
97
+ orderAmount = gr.Slider(label="Order amount", minimum=0, maximum=355, step=2, randomize=True)
98
+
99
+ orderState = gr.Radio(
100
+ label="Order state",
101
+ choices=["fulfilled", "failed", "pending"],
102
+ value="failed"
103
+ )
104
+
105
+ paymentMethodRegistrationFailure = gr.Radio(
106
+ label="Payment method registration failure",
107
+ choices=["False", "True"],
108
+ value="True"
109
+ )
110
+
111
+ paymentMethodType = gr.Radio(
112
+ label="Payment method type",
113
+ choices=["card", "apple pay ", "paypal", "bitcoin"],
114
+ value="bitcoin"
115
+ )
116
+
117
+ paymentMethodProvider = gr.Dropdown(
118
+ label="Payment method provider",
119
+ choices=["JCB 16 digit", "VISA 16 digit", "Voyager", "Diners Club / Carte Blanche", "Maestro", "VISA 13 digit", "Discover", "American Express", "JCB 15 digit", "Mastercard"],
120
+ multiselect=False,
121
+ value="American Express"
122
+ )
123
+
124
+ paymentMethodIssuer = gr.Dropdown(
125
+ label="Payment method issuer",
126
+ choices=["Her Majesty Trust", "Vertex Bancorp", "Fountain Financial Inc.", "His Majesty Bank Corp.", "Bastion Banks", "Bulwark Trust Corp.", "weird", "Citizens First Banks", "Grand Credit Corporation", "Solace Banks", "Rose Bancshares"],
127
+ multiselect=False,
128
+ value="Bastion Banks"
129
+ )
130
+
131
+ transactionAmount = gr.Slider(label="Transaction amount", minimum=0, maximum=355, step=2, randomize=True)
132
+
133
+ transactionFailed = gr.Radio(
134
+ label="Transaction failed",
135
+ choices=["False", "True"],
136
+ value="False"
137
+ )
138
+
139
+ emailDomain = gr.Radio(
140
+ label="Email domain",
141
+ choices=["com", "biz", "org", "net", "info", "weird"],
142
+ value="com"
143
+ )
144
+
145
+ emailProvider = gr.Radio(
146
+ label="Email provider",
147
+ choices=["gmail", "hotmail", "yahoo", "other", "weird"],
148
+ value="gmail"
149
+ )
150
+
151
+ customerIPAddressSimplified = gr.Radio(
152
+ label="Customer IP Address",
153
+ choices=["only_letters", "digits_and_letters"],
154
+ value="only_letter"
155
+ )
156
+
157
+ sameCity = gr.Radio(
158
+ label="Same city",
159
+ choices=["unknown", "no", "yes"],
160
+ value="unknown"
161
+ )
162
+
163
+ with gr.Column():
164
+
165
+ gr.Markdown(
166
+ """
167
+ ## Predicci贸n
168
+ """
169
+ )
170
+
171
+ label = gr.Label(label="Score")
172
+ predict_btn = gr.Button(value="Evaluar")
173
+ predict_btn.click(
174
+ predict,
175
+ inputs=[
176
+ orderAmount,
177
+ orderState,
178
+ paymentMethodRegistrationFailure,
179
+ paymentMethodType,
180
+ paymentMethodProvider,
181
+ paymentMethodIssuer,
182
+ transactionAmount,
183
+ transactionFailed,
184
+ emailDomain,
185
+ emailProvider,
186
+ customerIPAddressSimplified,
187
+ sameCity,
188
+ ],
189
+ outputs=[label],
190
+ api_name="prediccion"
191
+ )
192
+ gr.Markdown(
193
+ """
194
+ <p style='text-align: center'>
195
+ <a >Proyecto Final Kari
196
+ </a>
197
+ </p>
198
+ """
199
+ )
200
+
201
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ scikit-learn
4
+