File size: 3,679 Bytes
e47faa9
 
 
 
75fefe4
 
e47faa9
 
 
3fbf080
e47faa9
 
 
 
 
75fefe4
e47faa9
1340008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75fefe4
 
 
 
 
 
1340008
1b3c491
ae70b84
 
e47faa9
 
75fefe4
 
 
 
 
 
 
 
 
 
1340008
75fefe4
 
 
 
 
 
 
 
 
 
 
 
e47faa9
75fefe4
 
 
e47faa9
 
75fefe4
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
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()