Spaces:
Runtime error
Runtime error
File size: 3,581 Bytes
e47faa9 cd62a49 61c1e74 336ac28 e47faa9 a4894f4 336ac28 e47faa9 3fbf080 e47faa9 75fefe4 e47faa9 1340008 75fefe4 1340008 1b3c491 ae70b84 e47faa9 336ac28 61c1e74 336ac28 61c1e74 336ac28 75fefe4 1340008 61c1e74 75fefe4 61c1e74 75fefe4 336ac28 61c1e74 336ac28 61c1e74 |
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 |
import gradio as gr
import pandas as pd
import pickle
import numpy as np
from tensorflow import keras
treemodel = pickle.load(open('decision_tree.pkl', 'rb'))
nnmodel = keras.models.load_model("nnmodel.h5")
def onehot(df, column):
df = df.copy()
dummies = pd.get_dummies(df[column], prefix='type')
df = pd.concat([df,dummies], axis=1)
df = df.drop(column, axis=1)
return df
def dataframe(df):
df = onehot(df, column='type')
#if the 'type' column doesn't have value 'CASH_OUT' then add a column 'type_CASH_OUT' with value 0
if 'type_CASH_OUT' not in df.columns:
df['type_CASH_OUT'] = 0
#if the 'type' column doesn't have value 'TRANSFER' then add a column 'type_TRANSFER' with value 0
if 'type_TRANSFER' not in df.columns:
df['type_TRANSFER'] = 0
#if the 'type' column doesn't have value 'PAYMENT' then add a column 'type_PAYMENT' with value 0
if 'type_PAYMENT' not in df.columns:
df['type_PAYMENT'] = 0
#if the 'type' column doesn't have value 'DEBIT' then add a column 'type_DEBIT' with value 0
if 'type_DEBIT' not in df.columns:
df['type_DEBIT'] = 0
#if the 'type' column doesn't have value 'PAYMENT' then add a column 'type_PAYMENT' with value 0
if 'type_PAYMENT' not in df.columns:
df['type_PAYMENT'] = 0
df = df.drop(['nameOrig','nameDest','isFraud'], axis=1)
return df
def tree(file_obj):
df = pd.read_csv(file_obj.name)
df = dataframe(df)
y_pred = treemodel.predict(df)
pred_df = pd.DataFrame(y_pred, columns = ['predictedFraud'])
#append the predictions to the original dataframe
df_original = pd.read_csv(file_obj.name)
pred_df = pd.concat([df_original, pred_df], axis=1)
return pred_df
def nn(file_obj):
nn_df = pd.read_csv(file_obj.name)
nn_df = dataframe(nn_df)
y_prednn = nnmodel.predict(nn_df)
pred_proc = pd.DataFrame(y_prednn, columns = ['predictedFraudProbability'])
pred=np.where(y_prednn<0.44,0,1)
pred_dfnn = pd.DataFrame(pred, columns = ['predictedFraud'])
#append the predictions to the original dataframe
df_originalnn = pd.read_csv(file_obj.name)
pred_dfnn = pd.concat([df_originalnn, pred_proc, pred_dfnn], axis=1)
return pred_dfnn
file = gr.components.File(file_count="single", type="file", label="Fisierul CSV cu tranzactii", optional=False)
tree_output = gr.components.Dataframe(max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="predictedFraud - Predictii bazate pe modelul de clasificare DECISION TREE")
nn_output = gr.components.Dataframe(max_rows=20, max_cols=None, overflow_row_behaviour="paginate", type="pandas", label="predictedFraud - Predictii bazate pe modelul de clasificare NEURAL NETWORK")
tree_interface = gr.Interface(
fn=tree,
inputs=file,
outputs=tree_output,
)
nn_interface = gr.Interface(
fn=nn,
inputs=file,
outputs=nn_output,
)
def report_tree():
creport_tree = pd.read_csv('report_tree.csv')
return creport_tree
def report_nn():
creport_nn = pd.read_csv('report_nn.csv')
return creport_nn
cr_tree = gr.components.DataFrame(type="pandas", label="Classification Report (Decision Tree)")
cr_nn = gr.components.DataFrame(type="pandas", label="Classification Report (Neural Network)")
r_tree = gr.Interface(
fn=report_tree,
inputs = None,
outputs = cr_tree,
)
r_nn = gr.Interface(
fn=report_nn,
inputs = None,
outputs = cr_nn,
)
#tree_interface.launch(inline=True)
gr.Parallel(tree_interface, nn_interface, r_tree, r_nn).launch() |