import streamlit as st
import numpy as np
import joblib
from PIL import Image
import base64
from io import BytesIO
# Page configuration
st.set_page_config(
page_title="HDD Solution Predictor",
page_icon="🔧",
layout="centered",
initial_sidebar_state="collapsed"
)
# Function to convert image to base64
def image_to_base64(image_path):
try:
with open(image_path, "rb") as img_file:
return base64.b64encode(img_file.read()).decode()
except:
return None
# Enhanced CSS with better styling
st.markdown("""
""", unsafe_allow_html=True)
# Load model function
@st.cache_resource
def load_model():
try:
model = joblib.load('decision_tree_model.pkl')
le_soil = joblib.load('dt_soil_encoder.pkl')
le_water = joblib.load('dt_water_encoder.pkl')
le_solution = joblib.load('dt_solution_encoder.pkl')
return model, le_soil, le_water, le_solution
except FileNotFoundError:
st.error("⚠️ Model files not found! Please run the training script first.")
return None, None, None, None
# Prediction function
def predict_solution(diameter, soil_type, high_water, model, le_soil, le_water, le_solution):
try:
import pandas as pd
# Encode inputs
soil_encoded = le_soil.transform([soil_type])[0]
water_encoded = le_water.transform([high_water])[0]
# Create feature DataFrame with proper column names to match training
feature_data = {
'Diameter': [diameter],
'soil_encoded': [soil_encoded],
'water_encoded': [water_encoded]
}
features_df = pd.DataFrame(feature_data)
# Make prediction
prediction_encoded = model.predict(features_df)[0]
prediction = le_solution.inverse_transform([prediction_encoded])[0]
return prediction
except Exception as e:
return f"Error: {str(e)}"
def main():
# Logo section
st.markdown('
', unsafe_allow_html=True)
# Try to display logo with base64 encoding
logo_base64 = image_to_base64('logo2.e8c5ff97.png')
if logo_base64:
st.markdown(f'''

''', unsafe_allow_html=True)
else:
# Fallback: Try direct image display
try:
st.image('logo2.e8c5ff97.png', width=200)
except:
st.markdown('''
🏢 MEA
Metropolitan Electricity Authority
''', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
# Title and subtitle
st.markdown('🔧 HDD Solution Predictor
', unsafe_allow_html=True)
st.markdown('Get instant recommendations for your drilling project
', unsafe_allow_html=True)
# Load model
model_data = load_model()
if model_data[0] is None:
st.stop()
model, le_soil, le_water, le_solution = model_data
# Input container
st.markdown('', unsafe_allow_html=True)
# Input controls with better spacing
st.markdown("### 📊 Project Parameters")
# Create two columns for better layout
col1, col2 = st.columns(2)
with col1:
diameter = st.slider(
"🔩 Pipe Diameter (m)",
min_value=0.5,
max_value=2.0,
value=1.2,
step=0.1,
help="Select the diameter of the pipe to be installed"
)
with col2:
soil_type = st.selectbox(
"🏔️ Soil Type",
options=['clay', 'sand'],
index=0,
help="Select the predominant soil type at the drilling site"
)
# Full width for water table
high_water = st.selectbox(
"💧 High Water Table",
options=['no', 'yes'],
index=0,
help="Is there a high water table present at the site?"
)
# Predict button
if st.button("🔮 Get Solution Recommendation"):
prediction = predict_solution(diameter, soil_type, high_water, model, le_soil, le_water, le_solution)
# Solution details
solution_details = {
'A': {
'name': 'Enhanced Protection',
'description': 'Sheetpile + Trench + Grouting',
'class': 'solution-a',
'icon': '🛡️'
},
'B': {
'name': 'Maximum Protection',
'description': 'Sheetpile + Trench + Grouting + Casing',
'class': 'solution-b',
'icon': '🏰'
},
'C': {
'name': 'Moderate Protection',
'description': 'Sheetpile + Trench',
'class': 'solution-c',
'icon': '🔨'
},
'D': {
'name': 'Basic Protection',
'description': 'Grouting Only',
'class': 'solution-d',
'icon': '💧'
},
'E': {
'name': 'Minimal Intervention',
'description': 'No Additional Measures',
'class': 'solution-e',
'icon': '✅'
}
}
if prediction in solution_details:
details = solution_details[prediction]
st.markdown(f'''
{prediction}
{details['name']}
{details['description']}
''', unsafe_allow_html=True)
else:
st.error(f"❌ Prediction error: {prediction}")
st.markdown('
', unsafe_allow_html=True)
# Footer
st.markdown('''
''', unsafe_allow_html=True)
if __name__ == "__main__":
main()