-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (121 loc) · 4.81 KB
/
app.py
File metadata and controls
139 lines (121 loc) · 4.81 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import streamlit as st
import pandas as pd
import joblib
from sklearn.preprocessing import StandardScaler
import pickle
# Cache the model loading process
@st.cache_resource
def load_model():
# Load the pre-trained XGBoost model
with open('xgboost_model.pkl', 'rb') as model_file:
model = pickle.load(model_file)
return model
# Load model and scaler using cached functions
xgb_classifier = load_model()
# Function to predict anomalies using the loaded model and scaler
def predict_anomaly(input_data):
# Convert input data to a DataFrame
input_df = pd.DataFrame(input_data, index=[0])
# Predict anomalies using the loaded XGBoost model
prediction = xgb_classifier.predict(input_df)
return prediction
# Streamlit App Interface
st.title("Anomaly Detection Web App")
st.write("""
### Input your sensor data to check for anomalies
""")
# Define input fields for features based on your dataset
input_feature_1 = st.number_input('Feature 1')
input_feature_2 = st.number_input('Feature 2')
input_feature_3 = st.number_input('Feature 3')
input_feature_4 = st.number_input('Feature 4')
input_feature_5 = st.number_input('Feature 5')
input_feature_6 = st.number_input('Feature 6')
input_feature_7 = st.number_input('Feature 7')
input_feature_8 = st.number_input('Feature 8')
input_feature_9 = st.number_input('Feature 9')
input_feature_10 = st.number_input('Feature 10')
input_feature_11 = st.number_input('Feature 11')
input_feature_12 = st.number_input('Feature 12')
input_feature_13 = st.number_input('Feature 13')
input_feature_14 = st.number_input('Feature 14')
input_feature_15 = st.number_input('Feature 15')
input_feature_16 = st.number_input('Feature 16')
input_feature_17 = st.number_input('Feature 17')
input_feature_18 = st.number_input('Feature 18')
input_feature_19 = st.number_input('Feature 19')
input_feature_20 = st.number_input('Feature 20')
input_feature_21 = st.number_input('Feature 21')
input_feature_22 = st.number_input('Feature 22')
input_feature_23 = st.number_input('Feature 23')
input_feature_24 = st.number_input('Feature 24')
input_feature_25 = st.number_input('Feature 25')
input_feature_26 = st.number_input('Feature 26')
input_feature_27 = st.number_input('Feature 27')
input_feature_28 = st.number_input('Feature 28')
input_feature_29 = st.number_input('Feature 29')
input_feature_30 = st.number_input('Feature 30')
input_feature_31 = st.number_input('Feature 31')
input_feature_32 = st.number_input('Feature 32')
input_feature_33 = st.number_input('Feature 33')
input_feature_34 = st.number_input('Feature 34')
input_feature_35 = st.number_input('Feature 35')
input_feature_36 = st.number_input('Feature 36')
input_feature_37 = st.number_input('Feature 37')
input_feature_38 = st.number_input('Feature 38')
input_feature_39 = st.number_input('Feature 39')
input_feature_40 = st.number_input('Feature 40')
# Add more input fields as per your dataset's features
# When the user clicks "Predict"
if st.button('Predict'):
# Collect the input data
input_data = {
'feature_1': input_feature_1,
'feature_2': input_feature_2,
'feature_3': input_feature_3,
'feature_4': input_feature_4,
'feature_5': input_feature_5,
'feature_6': input_feature_6,
'feature_7': input_feature_7,
'feature_8': input_feature_8,
'feature_9': input_feature_9,
'feature_10': input_feature_10,
'feature_11': input_feature_11,
'feature_12': input_feature_12,
'feature_13': input_feature_13,
'feature_14': input_feature_14,
'feature_15': input_feature_15,
'feature_16': input_feature_16,
'feature_17': input_feature_17,
'feature_18': input_feature_18,
'feature_19': input_feature_19,
'feature_20': input_feature_20,
'feature_21': input_feature_21,
'feature_22': input_feature_22,
'feature_23': input_feature_23,
'feature_24': input_feature_24,
'feature_25': input_feature_25,
'feature_26': input_feature_26,
'feature_27': input_feature_27,
'feature_28': input_feature_28,
'feature_29': input_feature_29,
'feature_30': input_feature_30,
'feature_31': input_feature_31,
'feature_32': input_feature_32,
'feature_33': input_feature_33,
'feature_34': input_feature_34,
'feature_35': input_feature_35,
'feature_36': input_feature_36,
'feature_37': input_feature_37,
'feature_38': input_feature_38,
'feature_39': input_feature_39,
'feature_40': input_feature_40,
# Add more features here
}
# Make a prediction using the model
prediction = predict_anomaly(input_data)
# Display the result
if prediction == -1:
st.error("Anomaly Detected!")
else:
st.success("No Anomaly Detected.")