File size: 7,349 Bytes
e8204e5 5e667af e8204e5 5e667af e8204e5 3b69916 5e667af e8204e5 5e667af e8204e5 5e667af e8204e5 3b69916 e8204e5 5e667af e8204e5 5e667af e8204e5 5e667af e8204e5 |
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 numpy as np
import pandas as pd
import gradio as gr
from huggingface_hub import from_pretrained_keras
model = from_pretrained_keras('keras-io/imbalanced_classification')
# normalize unseen data using the training data mena d std
mean = np.array([7.9042977e+04, -6.7173101e-02, -1.3514652e-02, 1.8250896e-01,
4.3794613e-02, -6.3732401e-02, 3.0533234e-02, -2.6844479e-02,
3.9848695e-03, 2.2254344e-03, -1.7062010e-03, 7.6269522e-02,
-4.4995107e-02, 1.6710665e-02, 3.2869387e-02, 4.9116377e-02,
-5.5055786e-03, 1.5153111e-02, -2.2870189e-02, -7.2876248e-03,
9.9466369e-03, -6.6186422e-03, -2.2909872e-02, -9.9138934e-03,
1.1062564e-03, 3.8055412e-02, 2.8393818e-03, 2.2915885e-04,
1.9617653e-03, 9.0817749e+01])
std = np.array([3.9504547e+04, 1.9434261e+00, 1.6578650e+00, 1.4903845e+00,
1.4112488e+00, 1.3730472e+00, 1.3213707e+00, 1.2281808e+00,
1.2094835e+00, 1.1233834e+00, 1.0938724e+00, 1.0334861e+00,
1.0558152e+00, 1.0195577e+00, 9.6568835e-01, 9.3387991e-01,
8.8559818e-01, 8.7412000e-01, 8.4275919e-01, 8.1998885e-01,
7.7898669e-01, 7.4443674e-01, 7.0863432e-01, 6.3049096e-01,
6.0594761e-01, 5.0777191e-01, 4.8668963e-01, 4.0041801e-01,
3.3410770e-01, 2.5052232e+02])
# def infer(seed):
def infer(Time, V1, V2, V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13,V14,V15,V16,V17,V18,V19,V20,V21,V22,V23,V24,V25,V26,V27,V28,Amount):
data = pd.DataFrame({
'Time': Time, 'V1': V1, 'V2':V2, 'V3':V3, 'V4':V4, 'V5':V5, 'V6':V6, 'V7':V7, 'V8':V8, 'V9':V9, 'V10':V10, 'V11':V11, 'V12':V12, 'V13':V13, 'V14':V14, 'V15':V15, 'V16':V16,
'V17':V17, 'V18':V18, 'V19':V19, 'V20':V20, 'V21':V21, 'V22':V22, 'V23':V23, 'V24':V24, 'V25':V25, 'V26':V26, 'V27':V27, 'V28':V28, 'Amount':Amount
}, index=[0])
test_features = data.copy().values
test_features -= mean
test_features /= std
pred = model.predict(test_features)
data = data.round(decimals = 2)
#return f"{round(pred.flatten()[0]*100, 5)}%", data.values.tolist()
result = f'This is fraudulent transaction with probability is {round(pred.flatten()[0]*100, 5)}%' if pred.flatten()[0] > 0.5 else f"This is real transaction with probability is {round((1-pred.flatten()[0])*100, 3)}%"
return result #, data.values.tolist()
# get the inputs
inputs = [
gr.Slider(minimum=0, maximum=172700, step=10, label='Time', value=10),
gr.Slider(minimum=-56, maximum=2, step=0.1, label='V1', value=0),
gr.Slider(minimum=-72, maximum=23, step=0.1, label='V2', value=0),
gr.Slider(minimum=-48, maximum=9, step=0.1, label='V3', value=0),
gr.Slider(minimum=-5, maximum=16, step=0.1, label='V4', value=0),
gr.Slider(minimum=-113, maximum=32, step=0.1, label='V5', value=0),
gr.Slider(minimum=-26, maximum=73, step=0.1, label='V6', value=0),
gr.Slider(minimum=-43, maximum=113, step=0.1, label='V7', value=0),
gr.Slider(minimum=-73, maximum=20, step=0.1, label='V8', value=0),
gr.Slider(minimum=-13, maximum=15, step=0.1, label='V9', value=0),
gr.Slider(minimum=-24, maximum=23, step=0.1, label='V10', value=0),
gr.Slider(minimum=-4, maximum=12, step=0.1, label='V11', value=0),
gr.Slider(minimum=-18, maximum=7, step=0.1, label='V12', value=0),
gr.Slider(minimum=-5, maximum=7, step=0.1, label='V13', value=0),
gr.Slider(minimum=-19, maximum=10, step=0.1, label='V14', value=0),
gr.Slider(minimum=-4, maximum=8, step=0.1, label='V15', value=0),
gr.Slider(minimum=-14, maximum=17, step=0.1, label='V16', value=0),
gr.Slider(minimum=-25, maximum=9, step=0.1, label='V17', value=0),
gr.Slider(minimum=-9, maximum=5, step=0.1, label='V18', value=0),
gr.Slider(minimum=-7, maximum=5, step=0.1, label='V19', value=0),
gr.Slider(minimum=-54, maximum=39, step=0.1, label='V20', value=0),
gr.Slider(minimum=-34, maximum=27, step=0.1, label='V21', value=0),
gr.Slider(minimum=-10, maximum=10, step=0.1, label='V22', value=0),
gr.Slider(minimum=-44, maximum=22, step=0.1, label='V23', value=0),
gr.Slider(minimum=-2, maximum=4, step=0.1, label='V24', value=0),
gr.Slider(minimum=-10, maximum=7, step=0.1, label='V25', value=0),
gr.Slider(minimum=-2, maximum=5, step=0.1, label='V26', value=0),
gr.Slider(minimum=-22, maximum=31, step=0.1, label='V27', value=0),
gr.Slider(minimum=-15, maximum=33, step=0.1, label='V28', value=0),
gr.Slider(minimum=0, maximum=25691, step=10, label='Amount', value=10),
]
# the app outputs two segmented images
output = [gr.Textbox(label='')]
title = 'Imbalanced Classification with Tensorflow'
description = 'Imbalanced Classification in predicting Credit card Fraud.'
article = "Author: <a href=\"https://huggingface.co/geninhu\">Nhu Hoang</a>. <br> 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> <br> HuggingFace Model <a href=\"https://huggingface.co/keras-io/imbalanced_classification\">here</a> "
examples = [[1.39271000e+05, -1.01966286e-01, -2.19383079e-01,
2.91556060e-01, -1.48187543e+00, 4.92119816e-01,
7.19159629e-01, 1.09760697e+00, -3.29724791e-01,
-5.36999746e-01, 4.44389995e-01, -6.76070884e-01,
-9.84972751e-01, -1.17935517e+00, 2.52172631e-02,
8.96468489e-01, -2.84669137e+00, 4.31550427e-01,
1.93543856e-01, -2.23790737e+00, -6.42987862e-01,
6.46948603e-02, 1.18711606e+00, -1.11801609e-01,
-1.05455472e+00, -8.00450431e-01, -1.24265267e-01,
-1.39086191e-01, -2.60693707e-01, 1.06000000e+02],
[ 2.32520000e+04, 1.29377595e+00, 3.69202958e-01,
3.09448258e-01, 6.29771401e-01, 8.24778408e-02,
-4.35559896e-01, 4.37783478e-02, -2.96998723e-01,
1.22423979e+00, -4.73446723e-01, 1.64211418e-01,
-1.84665003e+00, 3.17030274e+00, 1.41499690e+00,
2.40625127e-01, 4.69934516e-01, -5.57937991e-02,
-1.23218906e-01, -1.83562143e-02, -1.01346309e-02,
-4.41576748e-01, -1.00570303e+00, 1.84647505e-02,
-4.91389484e-01, 3.36660905e-01, 1.02774527e-01,
-5.27911412e-02, 9.04270816e-03, 1.69900000e+01],
[ 1.42659000e+05, 1.83404703e+00, -1.94875520e+00,
-1.78165816e+00, -1.56784758e+00, -1.00460825e+00,
-8.37986768e-01, -3.40342869e-01, -3.37935548e-01,
-1.96463717e+00, 1.72661800e+00, 6.57974945e-01,
-3.98308615e-01, 1.14503226e-01, 3.12088209e-01,
-4.79675451e-01, -3.73087478e-01, 1.73461298e-01,
1.71939285e-01, 1.69937838e-01, 5.18207228e-02,
-1.29125608e-01, -5.58702004e-01, 3.59553819e-02,
-4.36588035e-01, -2.80344803e-01, -3.32314616e-01,
-7.22218399e-02, -2.88505958e-02, 2.62000000e+02,]]
gr.Interface(infer, inputs, output, examples= examples, allow_flagging='never',
title=title, description=description, article=article, live=False).launch(enable_queue=True, debug=False)
|