jggomez's picture
initial commit
7a684a9
import streamlit as st
import pandas as pd
import pickle
import numpy as np
st.set_page_config(layout="wide")
st.title("Concrete Compressive Strength Prediction with SVR")
st.markdown("""
<style>
.main-header {
font-size: 2.5em !important;
color: #4CAF50; /* Green color */
text-align: center;
margin-bottom: 30px;
}
.subheader {
font-size: 1.5em !important;
color: #2196F3; /* Blue color */
margin-top: 20px;
margin-bottom: 15px;
}
.prediction-box {
border: 2px solid #FF9800; /* Orange color */
padding: 20px;
border-radius: 10px;
text-align: center;
margin-top: 30px;
background-color: #FFF3E0; /* Light orange background */
}
.prediction-label {
font-size: 1.2em;
color: #333;
}
.prediction-value {
font-size: 2.5em;
font-weight: bold;
color: #E65100; /* Dark orange color */
}
.sidebar .sidebar-content {
background-color: #E8F5E9; /* Light green background for sidebar */
padding: 20px;
border-radius: 10px;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<p class="main-header">Concrete Compressive Strength Prediction with SVR</p>',
unsafe_allow_html=True)
st.write("Enter the characteristic values below to predict the compressive strength.")
# --- Example Values
data = {
'cement': [540.0, 540.0, 332.5, 332.5, 198.6],
'water': [162.0, 162.0, 228.0, 228.0, 192.0],
'superplasticizer': [2.5, 2.5, 0.0, 0.0, 0.0],
'coarse_aggregate': [1040.0, 1055.0, 932.0, 932.0, 978.4],
'fine_aggregate': [676.0, 676.0, 594.0, 594.0, 825.5],
'blast_furnace_slag': [0.0, 0.0, 143.0, 143.0, 133.0], # Original scale
'age': [28, 28, 90, 90, 90], # Original scale
'compressive_strength': [76.885537, 61.887366, 40.269535, 41.052780, 44.296075]
}
df_example = pd.DataFrame(data)
original_features = ['cement', 'water', 'superplasticizer',
'coarse_aggregate', 'fine_aggregate', 'blast_furnace_slag', 'age']
log_transformed_features = ['blast_furnace_slag_log', 'age_log']
all_model_features = ['cement', 'water', 'superplasticizer',
'coarse_aggregate', 'fine_aggregate', 'blast_furnace_slag_log', 'age_log']
target = 'compressive_strength'
# Calculate mean values from example data for input defaults
mean_values = {feature: float(df_example[feature].mean())
for feature in original_features}
@st.cache_resource
def load_model(path="./src/best_modelo_svr.pkl"):
"""Loads the trained SVR model from a pickle file."""
try:
with open(path, "rb") as f:
return pickle.load(f)
except FileNotFoundError:
st.error(
f"Error: Model file not found at {path}. Please ensure the model file 'best_modelo_svr.pkl' is in the './model/' directory.")
return None
except Exception as e:
st.error(f"An error occurred while loading the model: {e}")
return None
@st.cache_resource
def load_model_scaler(path="./src/standard_scaler_model.pkl"):
"""Loads the trained standard scaler model from a pickle file."""
try:
with open(path, "rb") as f:
return pickle.load(f)
except FileNotFoundError:
st.error(
f"Error: Model file not found at {path}. Please ensure the model file 'standard_scaler_model.pkl' is in the './model/' directory.")
return None
except Exception as e:
st.error(f"An error occurred while loading the model: {e}")
return None
model = load_model()
scaler = load_model_scaler()
st.write("Model Parameters:")
st.json(model.get_params())
st.sidebar.markdown(
'<p class="subheader">Enter Feature Values</p>', unsafe_allow_html=True)
input_data_original = {}
# Create number inputs for original scale features
for feature in original_features:
input_data_original[feature] = st.sidebar.number_input(
f"Value of {feature.replace('_', ' ').title()}",
value=mean_values[feature],
min_value=0.0,
step=0.1,
format="%.2f"
)
if st.sidebar.button("Predict Compressive Strength"):
if model is not None:
# Apply log
input_data_model = input_data_original.copy()
input_data_model['blast_furnace_slag_log'] = np.log1p(
input_data_model['blast_furnace_slag'])
input_data_model['age_log'] = np.log1p(input_data_model['age'])
# Create DataFrame for prediction, ensuring correct column order
input_df_model = pd.DataFrame([input_data_model])[all_model_features]
# --- Standardize the input data ---
input_df_scaled = scaler.transform(input_df_model)
# Convert back to DataFrame to maintain column names
input_df_scaled = pd.DataFrame(
input_df_scaled, columns=all_model_features)
try:
prediction = model.predict(input_df_scaled)
st.markdown('<p class="subheader">Prediction Result</p>',
unsafe_allow_html=True)
st.markdown(f"""
<div class="prediction-box">
<p class="prediction-label">Predicted Compressive Strength:</p>
<p class="prediction-value">{prediction[0]:.2f} MPa</p>
</div>
""", unsafe_allow_html=True)
# --- Display Input Variables ---
st.markdown(
'<p class="subheader">Input Values Used for Prediction</p>', unsafe_allow_html=True)
input_display_df = pd.DataFrame([input_data_original]).T
input_display_df.columns = ["Value"]
st.dataframe(input_display_df)
except Exception as e:
st.error(f"An error occurred during prediction: {e}")
else:
st.warning("Model not loaded. Please check the model file path.")