Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import joblib
|
3 |
+
|
4 |
+
model_path = 'Best_model.joblib'
|
5 |
+
loaded_model = joblib.load(model_path)
|
6 |
+
|
7 |
+
|
8 |
+
# Preprocess input function
|
9 |
+
def preprocess_input(input_data):
|
10 |
+
age = input_data['age']
|
11 |
+
bmi = input_data.get('bmi', None)
|
12 |
+
height = input_data.get('height', None)
|
13 |
+
weight = input_data.get('weight', None)
|
14 |
+
children = input_data['children']
|
15 |
+
|
16 |
+
# Convert height to meters based on the selected unit
|
17 |
+
height_unit = input_data.get('height_unit', 'meters')
|
18 |
+
if height is not None and height_unit != 'meters':
|
19 |
+
if height_unit == 'centimeters':
|
20 |
+
height /= 100
|
21 |
+
elif height_unit == 'feet':
|
22 |
+
height *= 0.3048 # 1 foot = 0.3048 meters
|
23 |
+
|
24 |
+
# Calculate BMI if height and weight are provided and height is not zero
|
25 |
+
if height is not None and height != 0 and weight is not None:
|
26 |
+
bmi = weight / (height ** 2)
|
27 |
+
|
28 |
+
# Convert sex to binary representation
|
29 |
+
sex_0 = 1 if input_data['sex'] == 'female' else 0
|
30 |
+
sex_1 = 1 - sex_0
|
31 |
+
|
32 |
+
# Convert smoker to binary representation
|
33 |
+
smoker_0 = 1 if input_data['smoker'] == 'no' else 0
|
34 |
+
smoker_1 = 1 - smoker_0
|
35 |
+
|
36 |
+
# Map region name to numerical representation
|
37 |
+
region_mapping = {'southeast': 1, 'southwest': 2, 'northwest': 3, 'northeast': 4}
|
38 |
+
region = region_mapping.get(input_data['region'], 0)
|
39 |
+
|
40 |
+
# Create binary representations for each region
|
41 |
+
region_1 = 1 if region == 1 else 0
|
42 |
+
region_2 = 1 if region == 2 else 0
|
43 |
+
region_3 = 1 if region == 3 else 0
|
44 |
+
region_4 = 1 if region == 4 else 0
|
45 |
+
|
46 |
+
# Create the formatted input list with 11 features
|
47 |
+
formatted_input = [age, bmi, children, sex_0, sex_1, region_1, region_2, region_3, region_4, smoker_0, smoker_1]
|
48 |
+
|
49 |
+
return formatted_input
|
50 |
+
|
51 |
+
|
52 |
+
# Input page
|
53 |
+
def input_page():
|
54 |
+
st.title('HealthInsure Claim Amount Predictor')
|
55 |
+
st.write('Please fill in the following details:')
|
56 |
+
|
57 |
+
age = None
|
58 |
+
height = None
|
59 |
+
weight = None
|
60 |
+
|
61 |
+
age_warning = ''
|
62 |
+
height_warning = ''
|
63 |
+
weight_warning = ''
|
64 |
+
|
65 |
+
age = st.number_input('Age', min_value=0, step=1, value=age)
|
66 |
+
if age == 0:
|
67 |
+
age_warning = 'Please enter correct age.'
|
68 |
+
st.warning(age_warning)
|
69 |
+
sex = st.radio('Sex', ('male', 'female'))
|
70 |
+
|
71 |
+
# Side-by-side input for height unit and height
|
72 |
+
col1, col2 = st.columns(2)
|
73 |
+
with col1:
|
74 |
+
height_unit = st.selectbox('Height Unit', ('meters', 'centimeters', 'feet'))
|
75 |
+
with col2:
|
76 |
+
height = st.number_input('Height', min_value=0.0, step=0.01, value=height)
|
77 |
+
if height == 0:
|
78 |
+
height_warning = 'Please enter correct height.'
|
79 |
+
st.warning(height_warning)
|
80 |
+
weight = st.number_input('Weight (in kg)', min_value=0.0, step=0.1, value=weight)
|
81 |
+
if weight == 0:
|
82 |
+
weight_warning = 'Please enter correct weight.'
|
83 |
+
st.warning(weight_warning)
|
84 |
+
|
85 |
+
# Calculate BMI immediately after entering height and weight if height is not zero
|
86 |
+
bmi = None
|
87 |
+
if height is not None and height != 0.0 and weight is not None:
|
88 |
+
# Convert height based on selected height unit
|
89 |
+
if height_unit != 'meters':
|
90 |
+
if height_unit == 'centimeters':
|
91 |
+
height /= 100
|
92 |
+
elif height_unit == 'feet':
|
93 |
+
height *= 0.3048 # 1 foot = 0.3048 meters
|
94 |
+
|
95 |
+
# Calculate BMI
|
96 |
+
bmi = weight / (height ** 2)
|
97 |
+
st.write(f'BMI: {bmi:.2f}')
|
98 |
+
|
99 |
+
children = st.number_input('Number of Children', min_value=0, step=1)
|
100 |
+
smoker = st.selectbox('Smoker', ('yes', 'no'))
|
101 |
+
region = st.selectbox('Region', ('southeast', 'southwest', 'northwest', 'northeast'))
|
102 |
+
|
103 |
+
if st.button('Predict'):
|
104 |
+
if age_warning or height_warning or weight_warning:
|
105 |
+
st.error('Please correct the following input errors:')
|
106 |
+
if age_warning:
|
107 |
+
st.error(age_warning)
|
108 |
+
if height_warning:
|
109 |
+
st.error(height_warning)
|
110 |
+
if weight_warning:
|
111 |
+
st.error(weight_warning)
|
112 |
+
else:
|
113 |
+
input_data = {'age': age, 'sex': sex, 'height': height, 'weight': weight, 'children': children,
|
114 |
+
'smoker': smoker, 'region': region, 'bmi': bmi, 'height_unit': height_unit}
|
115 |
+
processed_input = preprocess_input(input_data)
|
116 |
+
charges = loaded_model.predict([processed_input])[0]
|
117 |
+
st.write('## Estimated Claim Amount')
|
118 |
+
st.write(f'Estimated Claim Amount: {charges:.2f}', unsafe_allow_html=True)
|
119 |
+
st.write('The following value is estimated based on historical data and predictive modeling techniques and may not represent the exact amount.')
|
120 |
+
|
121 |
+
|
122 |
+
# Main function
|
123 |
+
def main():
|
124 |
+
input_page()
|
125 |
+
|
126 |
+
|
127 |
+
if __name__ == '__main__':
|
128 |
+
main()
|
129 |
+
|