YEHTUT commited on
Commit
efea443
·
verified ·
1 Parent(s): 3ab8fc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -37
app.py CHANGED
@@ -117,43 +117,59 @@ if st.button("Predict"):
117
 
118
  # Show progress spinner while making predictions
119
  with st.spinner('Making prediction...'):
120
- # Predict based on selected row
121
- # prediction = model.predict([input_values])
122
- # Load the pre-trained scaler and model
123
- with open('scaler.pkl', 'rb') as f:
124
- scalar = pickle.load(f)
125
-
126
- with open('rf_clf.pkl', 'rb') as f:
127
- rf_clf = pickle.load(f)
128
-
129
- # Scale the new data using the pre-trained scaler
130
- X_new_scaled = scalar.transform(single_sample.reshape(1, -1))
131
-
132
- # Make predictions using the pre-trained model
133
- prediction = rf_clf.predict(X_new_scaled)
134
- y_pred_proba = rf_clf.predict_proba(X_new_scaled)
135
- # loaded_model = pickle.load(open('Random_Forest.sav', 'rb'))
136
- # prediction = loaded_model.predict(np.array(single_sample))
137
-
138
-
139
- # st.write(f"Prediction : {prediction[0]}")
140
- if prediction[0] == 0:
141
- st.success("The website is not a phishing website.")
142
- st.markdown(f'<img src="data:image/gif;base64,{data_url_ok}" alt="cat gif">', unsafe_allow_html=True,)
143
- else:
144
- st.error("The website is a phishing website.")
145
- st.markdown(f'<img src="data:image/gif;base64,{data_url_warning}" alt="cat gif">', unsafe_allow_html=True,)
146
-
147
-
148
- # Visualize prediction confidence scores as a bar chart
149
- st.write("Prediction Confidence Scores:")
150
- class_names = rf_clf.classes_
151
- plt.figure(figsize=(8, 4))
152
- sns.barplot(x=class_names, y=y_pred_proba[0])
153
- plt.title("Prediction Confidence Scores")
154
- plt.xlabel("Class")
155
- plt.ylabel("Probability")
156
- st.pyplot(plt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  else:
159
  st.error("ERROR!!! Please provide web site information for prediction !!!")
 
117
 
118
  # Show progress spinner while making predictions
119
  with st.spinner('Making prediction...'):
120
+ try:
121
+ # Load the pre-trained scaler
122
+ with open('scaler.pkl', 'rb') as f:
123
+ scaler = pickle.load(f)
124
+ # Scale the new data
125
+ X_new_scaled = scaler.transform(single_sample.reshape(1, -1))
126
+
127
+ # Load the selected model
128
+ if selected_model == 'Logistic Regression':
129
+ with open('logistic_regression_model.pkl', 'rb') as f:
130
+ model = pickle.load(f)
131
+ elif selected_model == 'Decision Tree':
132
+ with open('decision_tree_model.pkl', 'rb') as f:
133
+ model = pickle.load(f)
134
+ elif selected_model == 'KNN':
135
+ with open('knn_model.pkl', 'rb') as f:
136
+ model = pickle.load(f)
137
+ elif selected_model == 'XGBoost':
138
+ with open('xgboost_model.pkl', 'rb') as f:
139
+ model = pickle.load(f)
140
+ elif selected_model == 'Random Forest':
141
+ with open('rf_clf.pkl', 'rb') as f:
142
+ model = pickle.load(f)
143
+ elif selected_model == 'SVM':
144
+ with open('svm_model.pkl', 'rb') as f:
145
+ model = pickle.load(f)
146
+
147
+ # Make predictions
148
+ prediction = model.predict(X_new_scaled)
149
+ y_pred_proba = model.predict_proba(X_new_scaled)
150
+
151
+ # Display the prediction result
152
+ if prediction[0] == 0:
153
+ st.success("The website is not a phishing website.")
154
+ st.markdown(f'<img src="data:image/gif;base64,{data_url_ok}" alt="cat gif">', unsafe_allow_html=True,)
155
+ else:
156
+ st.error("The website is a phishing website.")
157
+ st.markdown(f'<img src="data:image/gif;base64,{data_url_warning}" alt="cat gif">', unsafe_allow_html=True,)
158
+
159
+ # Visualize prediction confidence scores as a bar chart
160
+ st.write("Prediction Confidence Scores:")
161
+ class_names = model.classes_
162
+ plt.figure(figsize=(8, 4))
163
+ sns.barplot(x=class_names, y=y_pred_proba[0])
164
+ plt.title("Prediction Confidence Scores")
165
+ plt.xlabel("Class")
166
+ plt.ylabel("Probability")
167
+ st.pyplot(plt)
168
+
169
+ except FileNotFoundError as e:
170
+ st.error(f"Model file for {selected_model} not found: {str(e)}")
171
+ except Exception as e:
172
+ st.error(f"An error occurred while loading the model: {str(e)}")
173
 
174
  else:
175
  st.error("ERROR!!! Please provide web site information for prediction !!!")