fadzwan commited on
Commit
8680283
·
verified ·
1 Parent(s): 4fe297e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -7
app.py CHANGED
@@ -4,14 +4,30 @@ import pandas as pd
4
  from sklearn.preprocessing import StandardScaler
5
  from sklearn.decomposition import PCA
6
  import pickle
 
 
7
 
8
- # Load the trained model, scaler, and PCA
9
- with open('slump_regressor.pkl', 'rb') as f:
10
- regressor = pickle.load(f)
11
- with open('scaler.pkl', 'rb') as f:
12
- scaler = pickle.load(f)
13
- with open('pca.pkl', 'rb') as f:
14
- pca = pickle.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def preprocess_data(X):
17
  # Check if the input has 8 features
@@ -64,6 +80,20 @@ def main():
64
  st.title("Concrete Slump Strength Prediction")
65
  st.write("Enter the concrete mix parameters to predict the slump.")
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  slump_prediction = predict_slump_app()
68
  if slump_prediction is not None:
69
  st.subheader("Predicted Slump Strength")
 
4
  from sklearn.preprocessing import StandardScaler
5
  from sklearn.decomposition import PCA
6
  import pickle
7
+ from sklearn.linear_model import LinearRegression
8
+ import os
9
 
10
+ # Function to save the trained model, scaler, and PCA
11
+ def save_model_artifacts(regressor, scaler, pca):
12
+ # Save the trained model, scaler, and PCA to pickle files
13
+ with open('slump_regressor.pkl', 'wb') as f:
14
+ pickle.dump(regressor, f)
15
+ with open('scaler.pkl', 'wb') as f:
16
+ pickle.dump(scaler, f)
17
+ with open('pca.pkl', 'wb') as f:
18
+ pickle.dump(pca, f)
19
+
20
+ # Function to load the trained model, scaler, and PCA
21
+ def load_model_artifacts():
22
+ # Load the trained model, scaler, and PCA
23
+ with open('slump_regressor.pkl', 'rb') as f:
24
+ regressor = pickle.load(f)
25
+ with open('scaler.pkl', 'rb') as f:
26
+ scaler = pickle.load(f)
27
+ with open('pca.pkl', 'rb') as f:
28
+ pca = pickle.load(f)
29
+
30
+ return regressor, scaler, pca
31
 
32
  def preprocess_data(X):
33
  # Check if the input has 8 features
 
80
  st.title("Concrete Slump Strength Prediction")
81
  st.write("Enter the concrete mix parameters to predict the slump.")
82
 
83
+ # Load the trained model, scaler, and PCA
84
+ try:
85
+ regressor, scaler, pca = load_model_artifacts()
86
+ except FileNotFoundError:
87
+ # Train the model, scaler, and PCA and save the artifacts
88
+ regressor = LinearRegression()
89
+ # Train the model using the data
90
+ regressor.fit(X_train, y_train)
91
+ scaler = StandardScaler()
92
+ scaler.fit(X_train)
93
+ pca = PCA(n_components=4)
94
+ pca.fit(scaler.transform(X_train))
95
+ save_model_artifacts(regressor, scaler, pca)
96
+
97
  slump_prediction = predict_slump_app()
98
  if slump_prediction is not None:
99
  st.subheader("Predicted Slump Strength")