Spaces:
Runtime error
Runtime error
| 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(file_obj): | |
| creport_tree = pd.read_csv('report_tree.csv') | |
| return creport_tree | |
| def report_nn(file_obj): | |
| 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 = file, | |
| outputs = cr_tree, | |
| css="body {background-color: #f5f5f5; width: 100%; height: 100%; margin: 0; padding: 0;}" | |
| ) | |
| r_nn = gr.Interface( | |
| fn=report_nn, | |
| inputs = file, | |
| outputs = cr_nn, | |
| css="body {background-color: #f5f5f5; width: 100%; height: 100%; margin: 0; padding: 0;}" | |
| ) | |
| #tree_interface.launch(inline=True) | |
| gr.Parallel(tree_interface, nn_interface, r_tree, r_nn).launch(inline=True) |