cmasukume commited on
Commit
75570c3
·
verified ·
1 Parent(s): 04fd727

Update slapp.py

Browse files
Files changed (1) hide show
  1. slapp.py +101 -96
slapp.py CHANGED
@@ -1,96 +1,101 @@
1
- import streamlit as st
2
- import pickle
3
- import pandas as pd
4
-
5
- # Load the saved model
6
- try:
7
- model = pickle.load(open('model.pkl', 'rb'))
8
- except Exception as e:
9
- st.error(f"Error loading model: {e}")
10
- model = None
11
-
12
- # Streamlit app
13
- st.title("Fraud Detection API")
14
- st.write("Enter the transaction details to check if it's acceptable or fraudulent.")
15
-
16
- # Create input fields for the features
17
- time = st.number_input('Time')
18
- v1 = st.number_input('V1')
19
- v2 = st.number_input('V2')
20
- v3 = st.number_input('V3')
21
- v4 = st.number_input('V4')
22
- v5 = st.number_input('V5')
23
- v6 = st.number_input('V6')
24
- v7 = st.number_input('V7')
25
- v8 = st.number_input('V8')
26
- v9 = st.number_input('V9')
27
- v10 = st.number_input('V10')
28
- v11 = st.number_input('V11')
29
- v12 = st.number_input('V12')
30
- v13 = st.number_input('V13')
31
- v14 = st.number_input('V14')
32
- v15 = st.number_input('V15')
33
- v16 = st.number_input('V16')
34
- v17 = st.number_input('V17')
35
- v18 = st.number_input('V18')
36
- v19 = st.number_input('V19')
37
- v20 = st.number_input('V20')
38
- v21 = st.number_input('V21')
39
- v22 = st.number_input('V22')
40
- v23 = st.number_input('V23')
41
- v24 = st.number_input('V24')
42
- v25 = st.number_input('V25')
43
- v26 = st.number_input('V26')
44
- v27 = st.number_input('V27')
45
- v28 = st.number_input('V28')
46
- amount = st.number_input('Amount')
47
-
48
- # Prepare a button for prediction
49
- if st.button('Predict'):
50
- try:
51
- # Create a DataFrame from the input data
52
- transaction_data = pd.DataFrame({
53
- 'Time': [time],
54
- 'V1': [v1],
55
- 'V2': [v2],
56
- 'V3': [v3],
57
- 'V4': [v4],
58
- 'V5': [v5],
59
- 'V6': [v6],
60
- 'V7': [v7],
61
- 'V8': [v8],
62
- 'V9': [v9],
63
- 'V10': [v10],
64
- 'V11': [v11],
65
- 'V12': [v12],
66
- 'V13': [v13],
67
- 'V14': [v14],
68
- 'V15': [v15],
69
- 'V16': [v16],
70
- 'V17': [v17],
71
- 'V18': [v18],
72
- 'V19': [v19],
73
- 'V20': [v20],
74
- 'V21': [v21],
75
- 'V22': [v22],
76
- 'V23': [v23],
77
- 'V24': [v24],
78
- 'V25': [v25],
79
- 'V26': [v26],
80
- 'V27': [v27],
81
- 'V28': [v28],
82
- 'Amount': [amount]
83
- })
84
-
85
- # Perform prediction using the loaded model
86
- prediction = model.predict(transaction_data)
87
-
88
- # Prepare response
89
- if prediction[0] == 0:
90
- st.success('Prediction: Acceptable transaction')
91
- else:
92
- st.error('Prediction: Fraudulent transaction')
93
-
94
- except Exception as e:
95
- st.error(f'Error: {str(e)}')
96
-
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import pandas as pd
4
+
5
+ # Load the saved model
6
+ @st.cache_resource # Cache the model loading to avoid reloading on each run
7
+ def load_model():
8
+ try:
9
+ model = pickle.load(open('model.pkl', 'rb'))
10
+ return model
11
+ except Exception as e:
12
+ st.error(f"Error loading model: {e}")
13
+ return None
14
+
15
+ model = load_model()
16
+
17
+ # Title of the app
18
+ st.title("Fraud Detection API")
19
+ st.markdown("Welcome to the Fraud Detection API! Please enter the transaction details below:")
20
+
21
+ # Input fields for the transaction features
22
+ time = st.number_input("Time", min_value=0.0, step=0.1)
23
+ v1 = st.number_input("V1", step=0.01)
24
+ v2 = st.number_input("V2", step=0.01)
25
+ v3 = st.number_input("V3", step=0.01)
26
+ v4 = st.number_input("V4", step=0.01)
27
+ v5 = st.number_input("V5", step=0.01)
28
+ v6 = st.number_input("V6", step=0.01)
29
+ v7 = st.number_input("V7", step=0.01)
30
+ v8 = st.number_input("V8", step=0.01)
31
+ v9 = st.number_input("V9", step=0.01)
32
+ v10 = st.number_input("V10", step=0.01)
33
+ v11 = st.number_input("V11", step=0.01)
34
+ v12 = st.number_input("V12", step=0.01)
35
+ v13 = st.number_input("V13", step=0.01)
36
+ v14 = st.number_input("V14", step=0.01)
37
+ v15 = st.number_input("V15", step=0.01)
38
+ v16 = st.number_input("V16", step=0.01)
39
+ v17 = st.number_input("V17", step=0.01)
40
+ v18 = st.number_input("V18", step=0.01)
41
+ v19 = st.number_input("V19", step=0.01)
42
+ v20 = st.number_input("V20", step=0.01)
43
+ v21 = st.number_input("V21", step=0.01)
44
+ v22 = st.number_input("V22", step=0.01)
45
+ v23 = st.number_input("V23", step=0.01)
46
+ v24 = st.number_input("V24", step=0.01)
47
+ v25 = st.number_input("V25", step=0.01)
48
+ v26 = st.number_input("V26", step=0.01)
49
+ v27 = st.number_input("V27", step=0.01)
50
+ v28 = st.number_input("V28", step=0.01)
51
+ amount = st.number_input("Amount", min_value=0.0, step=0.1)
52
+
53
+ # Button to make predictions
54
+ if st.button("Predict"):
55
+ if model:
56
+ # Create a DataFrame from the input data
57
+ transaction_data = pd.DataFrame({
58
+ 'Time': [time],
59
+ 'V1': [v1],
60
+ 'V2': [v2],
61
+ 'V3': [v3],
62
+ 'V4': [v4],
63
+ 'V5': [v5],
64
+ 'V6': [v6],
65
+ 'V7': [v7],
66
+ 'V8': [v8],
67
+ 'V9': [v9],
68
+ 'V10': [v10],
69
+ 'V11': [v11],
70
+ 'V12': [v12],
71
+ 'V13': [v13],
72
+ 'V14': [v14],
73
+ 'V15': [v15],
74
+ 'V16': [v16],
75
+ 'V17': [v17],
76
+ 'V18': [v18],
77
+ 'V19': [v19],
78
+ 'V20': [v20],
79
+ 'V21': [v21],
80
+ 'V22': [v22],
81
+ 'V23': [v23],
82
+ 'V24': [v24],
83
+ 'V25': [v25],
84
+ 'V26': [v26],
85
+ 'V27': [v27],
86
+ 'V28': [v28],
87
+ 'Amount': [amount]
88
+ })
89
+
90
+ # Perform prediction
91
+ prediction = model.predict(transaction_data)
92
+
93
+ # Display results
94
+ if prediction[0] == 0:
95
+ st.success("✅ Acceptable transaction")
96
+ else:
97
+ st.error("🚨 Fraudulent transaction")
98
+ else:
99
+ st.error("Model not loaded.")
100
+
101
+