luciancotolan commited on
Commit
75fefe4
·
1 Parent(s): e5dbc77

added neural net

Browse files
Files changed (2) hide show
  1. app.py +35 -17
  2. neural_network.pkl +3 -0
app.py CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
2
  import pandas as pd
3
  import joblib
4
 
5
- model = joblib.load('decision_tree.pkl')
 
6
 
7
  def onehot(df, column):
8
  df = df.copy()
@@ -12,8 +13,7 @@ def onehot(df, column):
12
  return df
13
 
14
 
15
- def dataframe(file_obj):
16
- df = pd.read_csv(file_obj.name)
17
  df = onehot(df, column='type')
18
 
19
  #if the 'type' column doesn't have value 'CASH_OUT' then add a column 'type_CASH_OUT' with value 0
@@ -33,27 +33,45 @@ def dataframe(file_obj):
33
  df['type_PAYMENT'] = 0
34
 
35
  df = df.drop(['nameOrig','nameDest','isFraud'], axis=1)
36
- print(df.shape)
37
- y_pred = model.predict(df)
38
-
 
 
 
39
  pred_df = pd.DataFrame(y_pred, columns = ['predictedFraud'])
40
  #append the predictions to the original dataframe
41
  df_original = pd.read_csv(file_obj.name)
42
  pred_df = pd.concat([df_original, pred_df], axis=1)
43
- print(type(pred_df))
44
- print(pred_df.shape)
45
- # clr = classification_report(y_test, y_pred, target_names=['Not Fraud','Fraud'])
46
- # return 'Classification Report:\n'+ clr
47
  return pred_df
48
 
 
 
 
 
 
 
 
 
 
 
49
  file = gr.components.File(file_count="single", type="file", label="Fisierul CSV cu tranzactii", optional=False)
50
- y_pred_df = 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")
51
- interface_csv = gr.Interface(
52
- fn=dataframe,
 
 
 
 
 
 
 
 
 
53
  inputs=file,
54
- outputs=y_pred_df,
55
- title="Fraud Detection - EXPERT SYSTEM",
56
- 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><blockquote class="imgur-embed-pub" lang="en" data-id="J1aOKXd"><a href="https://imgur.com/J1aOKXd">View post on imgur.com</a></blockquote>'
57
  )
58
 
59
- interface_csv.launch(inline=False)
 
2
  import pandas as pd
3
  import joblib
4
 
5
+ treemodel = joblib.load('decision_tree.pkl')
6
+ nnmodel = joblib.load('neural_network.pkl')
7
 
8
  def onehot(df, column):
9
  df = df.copy()
 
13
  return df
14
 
15
 
16
+ def dataframe(df):
 
17
  df = onehot(df, column='type')
18
 
19
  #if the 'type' column doesn't have value 'CASH_OUT' then add a column 'type_CASH_OUT' with value 0
 
33
  df['type_PAYMENT'] = 0
34
 
35
  df = df.drop(['nameOrig','nameDest','isFraud'], axis=1)
36
+ return df
37
+
38
+ def tree(file_obj):
39
+ df = pd.read_csv(file_obj.name)
40
+ df = dataframe(df)
41
+ y_pred = treemodel.predict(df)
42
  pred_df = pd.DataFrame(y_pred, columns = ['predictedFraud'])
43
  #append the predictions to the original dataframe
44
  df_original = pd.read_csv(file_obj.name)
45
  pred_df = pd.concat([df_original, pred_df], axis=1)
 
 
 
 
46
  return pred_df
47
 
48
+ def nn(file_obj):
49
+ nn_df = pd.read_csv(file_obj.name)
50
+ nn_df = dataframe(nn_df)
51
+ y_prednn = nnmodel.predict(nn_df)
52
+ pred_dfnn = pd.DataFrame(y_prednn, columns = ['predictedFraud'])
53
+ #append the predictions to the original dataframe
54
+ df_originalnn = pd.read_csv(file_obj.name)
55
+ pred_dfnn = pd.concat([df_originalnn, pred_dfnn], axis=1)
56
+ return pred_dfnn
57
+
58
  file = gr.components.File(file_count="single", type="file", label="Fisierul CSV cu tranzactii", optional=False)
59
+ 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")
60
+ 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")
61
+ tree_interface = gr.Interface(
62
+ fn=tree,
63
+ inputs=file,
64
+ outputs=tree_output,
65
+ title="Fraud Detection - DECISION TREE EXPERT SYSTEM",
66
+ 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>'
67
+ )
68
+
69
+ nn_interface = gr.Interface(
70
+ fn=nn,
71
  inputs=file,
72
+ outputs=nn_output,
73
+ title="Fraud Detection - NEURAL NETWORK EXPERT SYSTEM",
74
+ 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>'
75
  )
76
 
77
+ gr.Parallel(tree_interface, nn_interface).launch()
neural_network.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3addbf26362e78236b30a97ff690e7c4a21f1f4fe3dfed88f42cf91bd68a5e9e
3
+ size 92390