|
import streamlit as st |
|
import joblib |
|
|
|
model_path = 'Best_model.joblib' |
|
loaded_model = joblib.load(model_path) |
|
|
|
|
|
|
|
def preprocess_input(input_data): |
|
age = input_data['age'] |
|
bmi = input_data.get('bmi', None) |
|
height = input_data.get('height', None) |
|
weight = input_data.get('weight', None) |
|
children = input_data['children'] |
|
|
|
|
|
height_unit = input_data.get('height_unit', 'meters') |
|
if height is not None and height_unit != 'meters': |
|
if height_unit == 'centimeters': |
|
height /= 100 |
|
elif height_unit == 'feet': |
|
height *= 0.3048 |
|
|
|
|
|
if height is not None and height != 0 and weight is not None: |
|
bmi = weight / (height ** 2) |
|
|
|
|
|
sex_0 = 1 if input_data['sex'] == 'female' else 0 |
|
sex_1 = 1 - sex_0 |
|
|
|
|
|
smoker_0 = 1 if input_data['smoker'] == 'no' else 0 |
|
smoker_1 = 1 - smoker_0 |
|
|
|
|
|
region_mapping = {'southeast': 1, 'southwest': 2, 'northwest': 3, 'northeast': 4} |
|
region = region_mapping.get(input_data['region'], 0) |
|
|
|
|
|
region_1 = 1 if region == 1 else 0 |
|
region_2 = 1 if region == 2 else 0 |
|
region_3 = 1 if region == 3 else 0 |
|
region_4 = 1 if region == 4 else 0 |
|
|
|
|
|
formatted_input = [age, bmi, children, sex_0, sex_1, region_1, region_2, region_3, region_4, smoker_0, smoker_1] |
|
|
|
return formatted_input |
|
|
|
|
|
|
|
def input_page(): |
|
st.title('HealthInsure Claim Amount Predictor') |
|
st.write('Please fill in the following details:') |
|
|
|
age = None |
|
height = None |
|
weight = None |
|
|
|
age_warning = '' |
|
height_warning = '' |
|
weight_warning = '' |
|
|
|
age = st.number_input('Age', min_value=0, step=1, value=age) |
|
if age == 0: |
|
age_warning = 'Please enter correct age.' |
|
st.warning(age_warning) |
|
sex = st.radio('Sex', ('male', 'female')) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
with col1: |
|
height_unit = st.selectbox('Height Unit', ('meters', 'centimeters', 'feet')) |
|
with col2: |
|
height = st.number_input('Height', min_value=0.0, step=0.01, value=height) |
|
if height == 0: |
|
height_warning = 'Please enter correct height.' |
|
st.warning(height_warning) |
|
weight = st.number_input('Weight (in kg)', min_value=0.0, step=0.1, value=weight) |
|
if weight == 0: |
|
weight_warning = 'Please enter correct weight.' |
|
st.warning(weight_warning) |
|
|
|
|
|
bmi = None |
|
if height is not None and height != 0.0 and weight is not None: |
|
|
|
if height_unit != 'meters': |
|
if height_unit == 'centimeters': |
|
height /= 100 |
|
elif height_unit == 'feet': |
|
height *= 0.3048 |
|
|
|
|
|
bmi = weight / (height ** 2) |
|
st.write(f'BMI: {bmi:.2f}') |
|
|
|
children = st.number_input('Number of Children', min_value=0, step=1) |
|
smoker = st.selectbox('Smoker', ('yes', 'no')) |
|
region = st.selectbox('Region', ('southeast', 'southwest', 'northwest', 'northeast')) |
|
|
|
if st.button('Predict'): |
|
if age_warning or height_warning or weight_warning: |
|
st.error('Please correct the following input errors:') |
|
if age_warning: |
|
st.error(age_warning) |
|
if height_warning: |
|
st.error(height_warning) |
|
if weight_warning: |
|
st.error(weight_warning) |
|
else: |
|
input_data = {'age': age, 'sex': sex, 'height': height, 'weight': weight, 'children': children, |
|
'smoker': smoker, 'region': region, 'bmi': bmi, 'height_unit': height_unit} |
|
processed_input = preprocess_input(input_data) |
|
charges = loaded_model.predict([processed_input])[0] |
|
st.write('## Estimated Claim Amount') |
|
st.write(f'Estimated Claim Amount: {charges:.2f}', unsafe_allow_html=True) |
|
st.write('The following value is estimated based on historical data and predictive modeling techniques and may not represent the exact amount.') |
|
|
|
|
|
|
|
def main(): |
|
input_page() |
|
|
|
|
|
if __name__ == '__main__': |
|
main() |
|
|
|
|