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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -65
app.py CHANGED
@@ -1,92 +1,62 @@
1
  import streamlit as st
2
  import numpy as np
3
- import pandas as pd
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
34
- if X.shape[1] != 8:
35
- raise ValueError("The input data should have 8 features (cement, blast furnace slag, fly ash, water, superplasticizer, coarse aggregate, fine aggregate, flow).")
36
-
37
- try:
38
- # Scale the input data using the loaded scaler
39
- X_scaled = scaler.transform(X)
40
-
41
- # Apply PCA using the loaded PCA transformer
42
- X_pca = pca.transform(X_scaled)
43
-
44
- return X_pca
45
- except Exception as e:
46
- raise ValueError(f"Error preprocessing the data: {str(e)}")
47
-
48
- def predict_slump_app():
49
- with st.expander("Concrete Mix Parameters"):
50
- cement = st.number_input("Cement (kg/m^3)", min_value=0.0, step=0.1)
51
- blast_furnace_slag = st.number_input("Blast Furnace Slag (kg/m^3)", min_value=0.0, step=0.1)
52
- fly_ash = st.number_input("Fly Ash (kg/m^3)", min_value=0.0, step=0.1)
53
- water = st.number_input("Water (kg/m^3)", min_value=0.0, step=0.1)
54
- superplasticizer = st.number_input("Superplasticizer (kg/m^3)", min_value=0.0, step=0.1)
55
- coarse_aggregate = st.number_input("Coarse Aggregate (kg/m^3)", min_value=0.0, step=0.1)
56
- fine_aggregate = st.number_input("Fine Aggregate (kg/m^3)", min_value=0.0, step=0.1)
57
- flow = st.number_input("FLOW (cm)", min_value=0.0, step=0.1)
58
-
59
- # Prepare the input data
60
- total_mix = cement + blast_furnace_slag + fly_ash + water
61
- if total_mix > 1000:
62
- raise ValueError("The total mix quantity should not exceed 1000 kg/m^3.")
63
-
64
- X = np.array([[cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, flow]])
65
-
66
- # Preprocess the data
67
- try:
68
- X_preprocessed = preprocess_data(X)
69
- except ValueError as e:
70
- st.error(str(e))
71
  return None
72
 
73
- # Make the prediction
74
- slump_prediction = regressor.predict(X_preprocessed)[0]
75
-
76
- return slump_prediction
77
-
78
  def main():
79
  st.set_page_config(page_title="Concrete Slump Strength Prediction")
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)
@@ -94,7 +64,7 @@ def main():
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")
100
  st.markdown(f"The predicted slump strength is **{slump_prediction:.2f} MPa**.")
 
1
  import streamlit as st
2
  import numpy as np
 
 
 
3
  import pickle
4
  from sklearn.linear_model import LinearRegression
5
+ from sklearn.preprocessing import StandardScaler
6
+ from sklearn.decomposition import PCA
7
 
8
+ def load_data():
9
+ # Replace this with your actual data loading logic
10
+ X_train = np.random.rand(100, 5)
11
+ y_train = np.random.rand(100)
12
+ return X_train, y_train
 
 
 
 
13
 
 
14
  def load_model_artifacts():
 
15
  with open('slump_regressor.pkl', 'rb') as f:
16
  regressor = pickle.load(f)
17
  with open('scaler.pkl', 'rb') as f:
18
  scaler = pickle.load(f)
19
  with open('pca.pkl', 'rb') as f:
20
  pca = pickle.load(f)
 
21
  return regressor, scaler, pca
22
 
23
+ def save_model_artifacts(regressor, scaler, pca):
24
+ with open('slump_regressor.pkl', 'wb') as f:
25
+ pickle.dump(regressor, f, protocol=pickle.HIGHEST_PROTOCOL)
26
+ with open('scaler.pkl', 'wb') as f:
27
+ pickle.dump(scaler, f, protocol=pickle.HIGHEST_PROTOCOL)
28
+ with open('pca.pkl', 'wb') as f:
29
+ pickle.dump(pca, f, protocol=pickle.HIGHEST_PROTOCOL)
30
+
31
+ def predict_slump_app(regressor, scaler, pca):
32
+ st.subheader("Enter Concrete Mix Parameters")
33
+ cement = st.number_input("Cement (kg/m³)", min_value=0.0, step=1.0)
34
+ slag = st.number_input("Slag (kg/m³)", min_value=0.0, step=1.0)
35
+ flyash = st.number_input("Fly Ash (kg/m³)", min_value=0.0, step=1.0)
36
+ water = st.number_input("Water (kg/m³)", min_value=0.0, step=1.0)
37
+ superplasticizer = st.number_input("Superplasticizer (kg/m³)", min_value=0.0, step=0.1)
38
+ coarseaggregate = st.number_input("Coarse Aggregate (kg/m³)", min_value=0.0, step=1.0)
39
+ fineaggregate = st.number_input("Fine Aggregate (kg/m³)", min_value=0.0, step=1.0)
40
+
41
+ if st.button("Predict Slump Strength"):
42
+ X_new = np.array([cement, slag, flyash, water, superplasticizer, coarseaggregate, fineaggregate])
43
+ X_new = scaler.transform([X_new])
44
+ X_new = pca.transform(X_new)
45
+ slump_prediction = regressor.predict(X_new)[0]
46
+ return slump_prediction
47
+ else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  return None
49
 
 
 
 
 
 
50
  def main():
51
  st.set_page_config(page_title="Concrete Slump Strength Prediction")
52
  st.title("Concrete Slump Strength Prediction")
53
  st.write("Enter the concrete mix parameters to predict the slump.")
54
 
 
55
  try:
56
  regressor, scaler, pca = load_model_artifacts()
57
+ except (FileNotFoundError, UnpicklingError):
58
+ X_train, y_train = load_data()
59
  regressor = LinearRegression()
 
60
  regressor.fit(X_train, y_train)
61
  scaler = StandardScaler()
62
  scaler.fit(X_train)
 
64
  pca.fit(scaler.transform(X_train))
65
  save_model_artifacts(regressor, scaler, pca)
66
 
67
+ slump_prediction = predict_slump_app(regressor, scaler, pca)
68
  if slump_prediction is not None:
69
  st.subheader("Predicted Slump Strength")
70
  st.markdown(f"The predicted slump strength is **{slump_prediction:.2f} MPa**.")