import streamlit as st import numpy as np import pickle from sklearn.linear_model import LinearRegression from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA import joblib import pandas as pd def load_data(): X_train = pd.read_csv('preprocessed_slump.csv').values[:,:-1] # X_train = df.iloc[:,:-1] y_train = pd.read_csv('preprocessed_slump.csv').values[:, -1] # y_train = df.iloc[:, -1] print("X_train",X_train.shape) print("y_train",y_train.shape) return X_train, y_train def load_model_artifacts(): with open('slump_regressor.pkl', 'rb') as f: regressor = pickle.load(f) with open('scaler.pkl', 'rb') as f: scaler = pickle.load(f) with open('pca.pkl', 'rb') as f: pca = pickle.load(f) return regressor, scaler, pca def save_model_artifacts(regressor, scaler, pca): with open('slump_regressor.pkl', 'wb') as f: pickle.dump(regressor, f, protocol=pickle.HIGHEST_PROTOCOL) with open('scaler.pkl', 'wb') as f: pickle.dump(scaler, f, protocol=pickle.HIGHEST_PROTOCOL) with open('pca.pkl', 'wb') as f: pickle.dump(pca, f, protocol=pickle.HIGHEST_PROTOCOL) # def preprocess_data(X): # print('preprocess_data X',X) # # Load the trained scaler and PCA # scaler = joblib.load('scaler.pkl') # pca = joblib.load('pca.pkl') # # Check if the input has 8 features # if X.shape[1] != 8: # raise ValueError("Input data should have 8 features.") # # Scale the input data using the loaded scaler # X_scaled = scaler.transform(X) # print("X_scaled",X_scaled) # # Apply PCA using the loaded PCA transformer # # X_pca = pca.transform(X_scaled) somehow dapat 4 # # print("X_pca",X_pca) # return X_pca def predict_slump(cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, FLOW): # Prepare the input data X = np.array([[cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, FLOW]]) # Preprocess the data # X_preprocessed = preprocess_data(X) # print("predict_slump X",X) # print("predict_slump X_preprocessed",X_preprocessed) # Load the trained model regressor = joblib.load('slump_regressor.pkl') # Make the prediction slump_prediction = regressor.predict(X)[0] return slump_prediction def main(): st.set_page_config(page_title="Concrete Slump Strength Prediction") st.title("Concrete Slump Strength Prediction") st.write("Enter the concrete mix parameters to predict the slump.") try: regressor, scaler, pca = load_model_artifacts() print("main regressor", regressor,'scaler',scaler,'pca',pca) except (FileNotFoundError, pickle.UnpicklingError): X_train, y_train = load_data() regressor = LinearRegression() regressor.fit(X_train, y_train) scaler = StandardScaler() scaler.fit(X_train) pca = PCA(n_components=4) pca.fit(scaler.transform(X_train)) save_model_artifacts(regressor, scaler, pca) cement = st.number_input("Cement (kg/m^3)", min_value=0.0, step=1.0) blast_furnace_slag = st.number_input("Blast Furnace Slag (kg/m^3)", min_value=0.0, step=1.0) fly_ash = st.number_input("Fly Ash (kg/m^3)", min_value=0.0, step=1.0) water = st.number_input("Water (kg/m^3)", min_value=0.0, step=1.0) superplasticizer = st.number_input("Superplasticizer (kg/m^3)", min_value=0.0, step=1.0) coarse_aggregate = st.number_input("Coarse Aggregate (kg/m^3)", min_value=0.0, step=1.0) fine_aggregate = st.number_input("Fine Aggregate (kg/m^3)", min_value=0.0, step=1.0) FLOW = st.number_input("FLOW (cm)", min_value=0.0, step=1.0) print('cement',cement,'blast_furnace_slag', blast_furnace_slag, 'fly_ash',fly_ash,'water', water, 'superplasticizer',superplasticizer, 'coarse_aggregate',coarse_aggregate, 'fine_aggregate',fine_aggregate, 'FLOW',FLOW) # cement = 0.5532916014826834 # blast_furnace_slag =-0.005227944774632739 # fly_ash = -0.13155706473691084 # water = -0.8899452596015911 # superplasticizer = -0.8533594548721104 # coarse_aggregate = 0.7873497318642375 # fine_aggregate = 0.08018932948598978 # FLOW = 0.06250289692348374 if st.button("Predict Slump Strength"): slump_prediction = predict_slump(cement, blast_furnace_slag, fly_ash, water, superplasticizer, coarse_aggregate, fine_aggregate, FLOW) print('slump_prediction',slump_prediction) st.write(f"Predicted Slump Strength: {slump_prediction:.2f} MPA") st.title("Concrete Slump Strength Prediction and Sustainable Development Goals") st.write("""Concrete slump strength prediction is an important topic that can have implications for several Sustainable Development Goals (SDGs) set by the United Nations.""") col1, col2 = st.columns(2) with col1: st.subheader("SDG 9: Industry, Innovation, and Infrastructure") st.markdown(""" - Accurate prediction of concrete slump strength can help in the design and construction of more robust and resilient infrastructure. - Improved concrete strength prediction can lead to more efficient use of materials and resources, reducing waste and promoting sustainable construction practices. """) with col2: st.subheader("SDG 11: Sustainable Cities and Communities") st.markdown(""" - Reliable concrete slump strength prediction can contribute to the development of sustainable and resilient cities. - Accurate slump strength prediction can help in the planning and construction of affordable and accessible housing. """) if __name__ == '__main__': main() import streamlit as st