geninhu commited on
Commit
e8204e5
1 Parent(s): 1fc67d3

Add application file

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+
4
+ import gradio as gr
5
+ from huggingface_hub import from_pretrained_keras
6
+
7
+ model = from_pretrained_keras('keras-io/imbalanced_classification')
8
+
9
+ # normalize unseen data using the training data mena d std
10
+ mean = np.array([7.9042977e+04, -6.7173101e-02, -1.3514652e-02, 1.8250896e-01,
11
+ 4.3794613e-02, -6.3732401e-02, 3.0533234e-02, -2.6844479e-02,
12
+ 3.9848695e-03, 2.2254344e-03, -1.7062010e-03, 7.6269522e-02,
13
+ -4.4995107e-02, 1.6710665e-02, 3.2869387e-02, 4.9116377e-02,
14
+ -5.5055786e-03, 1.5153111e-02, -2.2870189e-02, -7.2876248e-03,
15
+ 9.9466369e-03, -6.6186422e-03, -2.2909872e-02, -9.9138934e-03,
16
+ 1.1062564e-03, 3.8055412e-02, 2.8393818e-03, 2.2915885e-04,
17
+ 1.9617653e-03, 9.0817749e+01])
18
+ std = np.array([3.9504547e+04, 1.9434261e+00, 1.6578650e+00, 1.4903845e+00,
19
+ 1.4112488e+00, 1.3730472e+00, 1.3213707e+00, 1.2281808e+00,
20
+ 1.2094835e+00, 1.1233834e+00, 1.0938724e+00, 1.0334861e+00,
21
+ 1.0558152e+00, 1.0195577e+00, 9.6568835e-01, 9.3387991e-01,
22
+ 8.8559818e-01, 8.7412000e-01, 8.4275919e-01, 8.1998885e-01,
23
+ 7.7898669e-01, 7.4443674e-01, 7.0863432e-01, 6.3049096e-01,
24
+ 6.0594761e-01, 5.0777191e-01, 4.8668963e-01, 4.0041801e-01,
25
+ 3.3410770e-01, 2.5052232e+02])
26
+
27
+ fts_min_max = {'Amount': [0.0, 25691.16],
28
+ 'Time': [0.0, 172792.0],
29
+ 'V1': [-56.407509631329, 2.45492999121121],
30
+ 'V10': [-24.5882624372475, 23.7451361206545],
31
+ 'V11': [-4.79747346479757, 12.0189131816199],
32
+ 'V12': [-18.6837146333443, 7.8483920756446],
33
+ 'V13': [-5.79188120632084, 7.12688295859376],
34
+ 'V14': [-19.2143254902614, 10.5267660517847],
35
+ 'V15': [-4.49894467676621, 8.87774159774277],
36
+ 'V16': [-14.1298545174931, 17.3151115176278],
37
+ 'V17': [-25.1627993693248, 9.25352625047285],
38
+ 'V18': [-9.49874592104677, 5.04106918541184],
39
+ 'V19': [-7.21352743017759, 5.59197142733558],
40
+ 'V2': [-72.7157275629303, 22.0577289904909],
41
+ 'V20': [-54.497720494566, 39.4209042482199],
42
+ 'V21': [-34.8303821448146, 27.2028391573154],
43
+ 'V22': [-10.933143697655, 10.5030900899454],
44
+ 'V23': [-44.8077352037913, 22.5284116897749],
45
+ 'V24': [-2.83662691870341, 4.58454913689817],
46
+ 'V25': [-10.2953970749851, 7.51958867870916],
47
+ 'V26': [-2.60455055280817, 3.5173456116238],
48
+ 'V27': [-22.5656793207827, 31.6121981061363],
49
+ 'V28': [-15.4300839055349, 33.8478078188831],
50
+ 'V3': [-48.3255893623954, 9.38255843282114],
51
+ 'V4': [-5.68317119816995, 16.8753440335975],
52
+ 'V5': [-113.743306711146, 34.8016658766686],
53
+ 'V6': [-26.1605059358433, 73.3016255459646],
54
+ 'V7': [-43.5572415712451, 120.589493945238],
55
+ 'V8': [-73.2167184552674, 20.0072083651213],
56
+ 'V9': [-13.4340663182301, 15.5949946071278]}
57
+
58
+ def infer(seed):
59
+
60
+ data = pd.DataFrame({
61
+ col: round(np.random.uniform(fts_min_max[col][0], fts_min_max[col][1]), 0)
62
+ if col =='Time'
63
+ else np.random.uniform(fts_min_max[col][0], fts_min_max[col][1])
64
+ for col in ['Time', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10', 'V11', 'V12', 'V13', 'V14', 'V15', 'V16', 'V17', 'V18', 'V19', 'V20', 'V21',
65
+ 'V22', 'V23', 'V24', 'V25', 'V26', 'V27', 'V28', 'Amount']
66
+ }, index=[0])
67
+
68
+ test_features = data.copy().values
69
+ test_features -= mean
70
+ test_features /= std
71
+ pred = model.predict(test_features)
72
+
73
+ data = data.round(decimals = 2)
74
+
75
+ return f"{round(pred.flatten()[0]*100, 2)}%", data.values.tolist()
76
+
77
+ # get the inputs
78
+ inputs = [gr.Slider(minimum=0, maximum=3000, step=1, label='Choose a random number', value=5)]
79
+
80
+ # the app outputs two segmented images
81
+ output = [
82
+ gr.Textbox(label='Probability of this transaction is fraudulent:'),
83
+ gr.Dataframe(headers = ['Time', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9', 'V10', 'V11', 'V12', 'V13', 'V14', 'V15', 'V16', 'V17', 'V18', 'V19', 'V20', 'V21',
84
+ 'V22', 'V23', 'V24', 'V25', 'V26', 'V27', 'V28', 'Amount'],
85
+ max_rows = 1, row_count = 1, max_cols = 30, col_count = 30,
86
+ type='pandas', label='Display of generated data input for model')
87
+ ]
88
+
89
+ title = 'Imbalanced Classification with Tensorflow'
90
+ description = 'Imbalanced Classifiication in predicting Credit card Fraud.'
91
+
92
+ article = "Author: <a href=\"https://huggingface.co/geninhu\">Nhu Hoang</a>. Based on this <a href=\"https://keras.io/examples/structured_data/imbalanced_classification/\">keras example</a> by <a href=\"https://twitter.com/fchollet\">fchollet.</a> HuggingFace Model <a href=\"https://huggingface.co/keras-io/imbalanced_classification\">here</a> "
93
+
94
+ examples = [123, 2022, 975]
95
+
96
+ gr.Interface(infer, inputs, output, examples= examples, allow_flagging='never',
97
+ title=title, description=description, article=article, live=False).launch(enable_queue=True, debug=False)