Update app.py
Browse files
app.py
CHANGED
|
@@ -2,81 +2,52 @@ import streamlit as st
|
|
| 2 |
import numpy as np
|
| 3 |
import pickle
|
| 4 |
|
|
|
|
| 5 |
model = pickle.load(open("life_expectancy_model.pkl", "rb"))
|
| 6 |
|
|
|
|
| 7 |
st.set_page_config(
|
| 8 |
page_title="Life Expectancy Prediction",
|
| 9 |
page_icon="π",
|
| 10 |
layout="centered",
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# Define
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
st.sidebar.title("Navigation")
|
| 21 |
-
|
| 22 |
-
st.sidebar.button(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
}
|
| 36 |
-
.stButton > button {
|
| 37 |
-
width: 100%;
|
| 38 |
-
background-color: #1E88E5;
|
| 39 |
-
color: white;
|
| 40 |
-
font-size: 16px;
|
| 41 |
-
border-radius: 6px;
|
| 42 |
-
}
|
| 43 |
-
.result-box {
|
| 44 |
-
text-align: center;
|
| 45 |
-
font-size: 22px;
|
| 46 |
-
font-weight: bold;
|
| 47 |
-
color: white;
|
| 48 |
-
padding: 15px;
|
| 49 |
-
border-radius: 8px;
|
| 50 |
-
margin-top: 20px;
|
| 51 |
-
background-color: #388E3C; /* Green */
|
| 52 |
-
}
|
| 53 |
-
.section-title {
|
| 54 |
-
font-size: 20px;
|
| 55 |
-
font-weight: bold;
|
| 56 |
-
color: #1E88E5;
|
| 57 |
-
margin-top: 20px;
|
| 58 |
-
}
|
| 59 |
-
</style>
|
| 60 |
-
""", unsafe_allow_html=True)
|
| 61 |
-
|
| 62 |
-
st.markdown("<h1 class='title'>Life Expectancy Prediction</h1>", unsafe_allow_html=True)
|
| 63 |
-
|
| 64 |
-
st.subheader("Select a Step:")
|
| 65 |
-
if st.button("Problem Statement"):
|
| 66 |
-
set_page("problem")
|
| 67 |
-
if st.button("Simple EDA"):
|
| 68 |
-
set_page("eda")
|
| 69 |
-
if st.button("Visualizations"):
|
| 70 |
-
set_page("visuals")
|
| 71 |
-
if st.button("Model Building"):
|
| 72 |
-
set_page("model_building")
|
| 73 |
-
if st.button("Final Model"):
|
| 74 |
-
set_page("final_model")
|
| 75 |
-
|
| 76 |
-
elif st.session_state.page == "model":
|
| 77 |
-
st.subheader("Hands-on Model")
|
| 78 |
col1, col2 = st.columns(2)
|
| 79 |
-
|
| 80 |
with col1:
|
| 81 |
year = st.slider("Year", 2000, 2015, 2008)
|
| 82 |
status = st.radio("Status", ["Developing", "Developed"], horizontal=True)
|
|
@@ -89,7 +60,7 @@ elif st.session_state.page == "model":
|
|
| 89 |
measles = st.slider("Measles Cases", 0, 212183, 2419)
|
| 90 |
bmi = st.slider("BMI", 1.0, 87.3, 38.3)
|
| 91 |
polio = st.slider("Polio Immunization (%)", 3, 99, 82)
|
| 92 |
-
|
| 93 |
with col2:
|
| 94 |
under_five_deaths = st.slider("Under-Five Deaths", 0, 2500, 4)
|
| 95 |
total_expenditure = st.slider("Total Healthcare Expenditure (%)", 0.37, 17.6, 5.92)
|
|
@@ -107,41 +78,35 @@ elif st.session_state.page == "model":
|
|
| 107 |
hepatitis_b, measles, bmi, under_five_deaths, polio, total_expenditure,
|
| 108 |
diphtheria, hiv_aids, gdp, population, thinness_1_19, thinness_5_9,
|
| 109 |
income_composition, schooling]])
|
|
|
|
| 110 |
prediction = model.predict(features)[0]
|
| 111 |
|
| 112 |
-
st.
|
| 113 |
-
f"""
|
| 114 |
-
<div class="result-box">
|
| 115 |
-
Predicted Life Expectancy: <b>{prediction:.2f} years</b>
|
| 116 |
-
</div>
|
| 117 |
-
""",
|
| 118 |
-
unsafe_allow_html=True,
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
elif st.session_state.page == "report":
|
| 122 |
-
st.subheader("Model Report")
|
| 123 |
-
st.write("Detailed model performance metrics will be shown here.")
|
| 124 |
-
|
| 125 |
-
elif st.session_state.page == "problem":
|
| 126 |
-
st.subheader("Problem Statement")
|
| 127 |
-
st.write("Details about the problem statement will go here.")
|
| 128 |
-
|
| 129 |
-
elif st.session_state.page == "eda":
|
| 130 |
-
st.subheader("Simple EDA")
|
| 131 |
-
st.write("Exploratory Data Analysis details will be here.")
|
| 132 |
-
|
| 133 |
-
elif st.session_state.page == "visuals":
|
| 134 |
-
st.subheader("Visualizations")
|
| 135 |
-
st.write("Graphs and charts will be displayed here.")
|
| 136 |
-
|
| 137 |
-
elif st.session_state.page == "model_building":
|
| 138 |
-
st.subheader("Model Building")
|
| 139 |
-
st.write("Steps for building the model will be described here.")
|
| 140 |
-
|
| 141 |
-
elif st.session_state.page == "final_model":
|
| 142 |
-
st.subheader("Final Model")
|
| 143 |
-
st.write("Details about the final model will be displayed here.")
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
# import streamlit as st
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pickle
|
| 4 |
|
| 5 |
+
# Load the model
|
| 6 |
model = pickle.load(open("life_expectancy_model.pkl", "rb"))
|
| 7 |
|
| 8 |
+
# Set page config
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="Life Expectancy Prediction",
|
| 11 |
page_icon="π",
|
| 12 |
layout="centered",
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Define pages
|
| 16 |
+
PAGES = {
|
| 17 |
+
"Model Report": "model_report",
|
| 18 |
+
"Hands-on Model": "hands_on_model",
|
| 19 |
+
"Problem Statement": "problem_statement",
|
| 20 |
+
"Simple EDA": "simple_eda",
|
| 21 |
+
"Visualizations": "visualizations",
|
| 22 |
+
"Model Building": "model_building",
|
| 23 |
+
"Final Model": "final_model"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Session state for navigation
|
| 27 |
+
if "current_page" not in st.session_state:
|
| 28 |
+
st.session_state.current_page = "Model Report"
|
| 29 |
+
|
| 30 |
+
# Function to switch pages
|
| 31 |
+
def switch_page(page):
|
| 32 |
+
st.session_state.current_page = page
|
| 33 |
+
|
| 34 |
+
# Sidebar Navigation
|
| 35 |
st.sidebar.title("Navigation")
|
| 36 |
+
for page_name in PAGES:
|
| 37 |
+
if st.sidebar.button(page_name):
|
| 38 |
+
switch_page(page_name)
|
| 39 |
+
|
| 40 |
+
# Main Page Display
|
| 41 |
+
if st.session_state.current_page == "Model Report":
|
| 42 |
+
st.title("π Model Report")
|
| 43 |
+
st.write("Welcome to the Life Expectancy Prediction App!")
|
| 44 |
+
st.write("Navigate using the buttons on the sidebar.")
|
| 45 |
+
|
| 46 |
+
elif st.session_state.current_page == "Hands-on Model":
|
| 47 |
+
st.title("π Hands-on Model")
|
| 48 |
+
st.write("Interact with the model by providing input features.")
|
| 49 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
col1, col2 = st.columns(2)
|
|
|
|
| 51 |
with col1:
|
| 52 |
year = st.slider("Year", 2000, 2015, 2008)
|
| 53 |
status = st.radio("Status", ["Developing", "Developed"], horizontal=True)
|
|
|
|
| 60 |
measles = st.slider("Measles Cases", 0, 212183, 2419)
|
| 61 |
bmi = st.slider("BMI", 1.0, 87.3, 38.3)
|
| 62 |
polio = st.slider("Polio Immunization (%)", 3, 99, 82)
|
| 63 |
+
|
| 64 |
with col2:
|
| 65 |
under_five_deaths = st.slider("Under-Five Deaths", 0, 2500, 4)
|
| 66 |
total_expenditure = st.slider("Total Healthcare Expenditure (%)", 0.37, 17.6, 5.92)
|
|
|
|
| 78 |
hepatitis_b, measles, bmi, under_five_deaths, polio, total_expenditure,
|
| 79 |
diphtheria, hiv_aids, gdp, population, thinness_1_19, thinness_5_9,
|
| 80 |
income_composition, schooling]])
|
| 81 |
+
|
| 82 |
prediction = model.predict(features)[0]
|
| 83 |
|
| 84 |
+
st.success(f"Predicted Life Expectancy: {prediction:.2f} years")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
elif st.session_state.current_page == "Problem Statement":
|
| 87 |
+
st.title("π Problem Statement")
|
| 88 |
+
st.write("Define the problem related to life expectancy prediction.")
|
| 89 |
+
|
| 90 |
+
elif st.session_state.current_page == "Simple EDA":
|
| 91 |
+
st.title("π Simple EDA")
|
| 92 |
+
st.write("Explore the dataset with some basic exploratory data analysis.")
|
| 93 |
+
|
| 94 |
+
elif st.session_state.current_page == "Visualizations":
|
| 95 |
+
st.title("π Visualizations")
|
| 96 |
+
st.write("Check the visualizations that help understand the data better.")
|
| 97 |
+
|
| 98 |
+
elif st.session_state.current_page == "Model Building":
|
| 99 |
+
st.title("π Model Building")
|
| 100 |
+
st.write("Details on how the model was built and trained.")
|
| 101 |
+
|
| 102 |
+
elif st.session_state.current_page == "Final Model":
|
| 103 |
+
st.title("β
Final Model")
|
| 104 |
+
st.write("Showcase the final selected model and its performance.")
|
| 105 |
+
|
| 106 |
+
# Go Back Button
|
| 107 |
+
if st.session_state.current_page != "Model Report":
|
| 108 |
+
if st.button("π Go Back to Home"):
|
| 109 |
+
switch_page("Model Report")
|
| 110 |
|
| 111 |
|
| 112 |
# import streamlit as st
|