luciancotolan's picture
added neural net
75fefe4
raw
history blame
3.68 kB
import gradio as gr
import pandas as pd
import joblib
treemodel = joblib.load('decision_tree.pkl')
nnmodel = joblib.load('neural_network.pkl')
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_dfnn = pd.DataFrame(y_prednn, columns = ['predictedFraud'])
#append the predictions to the original dataframe
df_originalnn = pd.read_csv(file_obj.name)
pred_dfnn = pd.concat([df_originalnn, 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 isFraud - Etichetele reale")
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 isFraud - Etichetele reale")
tree_interface = gr.Interface(
fn=tree,
inputs=file,
outputs=tree_output,
title="Fraud Detection - DECISION TREE EXPERT SYSTEM",
description='<h2>Sistem expert bazat pe un model de clasificare pentru detectarea fraudelor in tranzactii bancare.<h2><h3>predictedFraud - Predictii bazate pe modelul de clasificare. isFraud - Etichetele reale<h3>'
)
nn_interface = gr.Interface(
fn=nn,
inputs=file,
outputs=nn_output,
title="Fraud Detection - NEURAL NETWORK EXPERT SYSTEM",
description='<h2>Sistem expert bazat pe un model de clasificare pentru detectarea fraudelor in tranzactii bancare.<h2><h3>predictedFraud - Predictii bazate pe modelul de clasificare. isFraud - Etichetele reale<h3>'
)
gr.Parallel(tree_interface, nn_interface).launch()