Spaces:
Configuration error
Configuration error
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]) | |
fts_min_max = {'Amount': [0.0, 25691.16], | |
'Time': [0.0, 172792.0], | |
'V1': [-56.407509631329, 2.45492999121121], | |
'V10': [-24.5882624372475, 23.7451361206545], | |
'V11': [-4.79747346479757, 12.0189131816199], | |
'V12': [-18.6837146333443, 7.8483920756446], | |
'V13': [-5.79188120632084, 7.12688295859376], | |
'V14': [-19.2143254902614, 10.5267660517847], | |
'V15': [-4.49894467676621, 8.87774159774277], | |
'V16': [-14.1298545174931, 17.3151115176278], | |
'V17': [-25.1627993693248, 9.25352625047285], | |
'V18': [-9.49874592104677, 5.04106918541184], | |
'V19': [-7.21352743017759, 5.59197142733558], | |
'V2': [-72.7157275629303, 22.0577289904909], | |
'V20': [-54.497720494566, 39.4209042482199], | |
'V21': [-34.8303821448146, 27.2028391573154], | |
'V22': [-10.933143697655, 10.5030900899454], | |
'V23': [-44.8077352037913, 22.5284116897749], | |
'V24': [-2.83662691870341, 4.58454913689817], | |
'V25': [-10.2953970749851, 7.51958867870916], | |
'V26': [-2.60455055280817, 3.5173456116238], | |
'V27': [-22.5656793207827, 31.6121981061363], | |
'V28': [-15.4300839055349, 33.8478078188831], | |
'V3': [-48.3255893623954, 9.38255843282114], | |
'V4': [-5.68317119816995, 16.8753440335975], | |
'V5': [-113.743306711146, 34.8016658766686], | |
'V6': [-26.1605059358433, 73.3016255459646], | |
'V7': [-43.5572415712451, 120.589493945238], | |
'V8': [-73.2167184552674, 20.0072083651213], | |
'V9': [-13.4340663182301, 15.5949946071278]} | |
def infer(seed): | |
data = pd.DataFrame({ | |
col: round(np.random.uniform(fts_min_max[col][0], fts_min_max[col][1]), 0) | |
if col =='Time' | |
else np.random.uniform(fts_min_max[col][0], fts_min_max[col][1]) | |
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', | |
'V22', 'V23', 'V24', 'V25', 'V26', 'V27', 'V28', '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, 2)}%", data.values.tolist() | |
# get the inputs | |
inputs = [gr.Slider(minimum=0, maximum=3000, step=1, label='Choose a random number', value=5)] | |
# the app outputs two segmented images | |
output = [ | |
gr.Textbox(label='Probability of this transaction is fraudulent:'), | |
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', | |
'V22', 'V23', 'V24', 'V25', 'V26', 'V27', 'V28', 'Amount'], | |
max_rows = 1, row_count = 1, max_cols = 30, col_count = 30, | |
type='pandas', label='Display of generated data input for model') | |
] | |
title = 'Imbalanced Classification with Tensorflow' | |
description = 'Imbalanced Classifiication in predicting Credit card Fraud.' | |
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> " | |
examples = [123, 2022, 975] | |
gr.Interface(infer, inputs, output, examples= examples, allow_flagging='never', | |
title=title, description=description, article=article, live=False).launch(enable_queue=True, debug=False) | |