RMHalak's picture
Update app.py
8f827eb verified
raw
history blame
5.6 kB
import streamlit as st
import pandas as pd
import pickle
from utils import create_new_features, normalize, init_new_pred
with open('./trained_model.pkl', 'rb') as file:
model = pickle.load(file)
# Define min and max values from the dictionaries
min_dict = {
'bedrooms': 0,
'bathrooms': 0,
'sqft_living': 370,
'sqft_lot': 638,
'floors': 1,
'waterfront': 0,
'view': 0,
'condition': 1,
'sqft_above': 370,
'sqft_basement': 0,
'yr_built': 1900,
'yr_renovated': 0,
'house_age': 0,
'years_since_renovation': 0
}
max_dict = {
'bedrooms': 9,
'bathrooms': 8,
'sqft_living': 13540,
'sqft_lot': 1074218,
'floors': 3,
'waterfront': 1,
'view': 4,
'condition': 5,
'sqft_above': 9410,
'sqft_basement': 4820,
'yr_built': 2014,
'yr_renovated': 2014,
'house_age': 114,
'years_since_renovation': 2014
}
# Create two columns: one for the city and one for the map
col1, col2 = st.columns([1, 2]) # Adjust the width ratios as needed
# Display city dropdown in the first column
with col1:
# Placeholder for displaying the predicted price at the top
price_placeholder = st.empty()
st.subheader('City Selection')
city = st.selectbox(
'Select City',
['Algona', 'Auburn', 'Beaux Arts Village', 'Bellevue',
'Black Diamond', 'Bothell', 'Burien', 'Carnation', 'Clyde Hill',
'Covington', 'Des Moines', 'Duvall', 'Enumclaw', 'Fall City',
'Federal Way', 'Inglewood-Finn Hill', 'Issaquah', 'Kenmore',
'Kent', 'Kirkland', 'Lake Forest Park', 'Maple Valley', 'Medina',
'Mercer Island', 'Milton', 'Newcastle', 'Normandy Park',
'North Bend', 'Pacific', 'Preston', 'Ravensdale', 'Redmond',
'Renton', 'Sammamish', 'SeaTac', 'Seattle', 'Shoreline',
'Skykomish', 'Snoqualmie', 'Snoqualmie Pass', 'Tukwila', 'Vashon',
'Woodinville', 'Yarrow Point'],
)
# Create sliders for each item in the dictionaries
bedrooms = st.slider('Bedrooms', min_value=min_dict['bedrooms'], max_value=max_dict['bedrooms'], value=min_dict['bedrooms'])
bathrooms = st.slider('Bathrooms', min_value=min_dict['bathrooms'], max_value=max_dict['bathrooms'], value=min_dict['bathrooms'])
sqft_living = st.slider('Square Feet (Living)', min_value=min_dict['sqft_living'], max_value=max_dict['sqft_living'], value=min_dict['sqft_living'])
sqft_lot = st.slider('Square Feet (Lot)', min_value=min_dict['sqft_lot'], max_value=max_dict['sqft_lot'], value=min_dict['sqft_lot'])
floors = st.slider('Floors', min_value=min_dict['floors'], max_value=max_dict['floors'], value=min_dict['floors'])
waterfront = st.checkbox('Waterfront', value=False, )
view = st.slider('View', min_value=min_dict['view'], max_value=max_dict['view'], value=min_dict['view'])
condition = st.slider('Condition', min_value=min_dict['condition'], max_value=max_dict['condition'], value=min_dict['condition'])
sqft_above = st.slider('Square Feet (Above)', min_value=min_dict['sqft_above'], max_value=max_dict['sqft_above'], value=min_dict['sqft_above'])
sqft_basement = st.slider('Square Feet (Basement)', min_value=min_dict['sqft_basement'], max_value=max_dict['sqft_basement'], value=min_dict['sqft_basement'])
yr_built = st.slider('Year Built', min_value=min_dict['yr_built'], max_value=max_dict['yr_built'], value=min_dict['yr_built'])
yr_renovated = st.slider('Year Renovated', min_value=min_dict['yr_renovated'], max_value=max_dict['yr_renovated'], value=min_dict['yr_renovated'])
# if (city != 'Algona' or # Assuming Algona as the default value
# bedrooms != min_dict['bedrooms'] or
# bathrooms != min_dict['bathrooms'] or
# sqft_living != min_dict['sqft_living'] or
# sqft_lot != min_dict['sqft_lot'] or
# floors != min_dict['floors'] or
# int(waterfront) != 0 or # Assuming default value for waterfront is 0
# view != min_dict['view'] or
# condition != min_dict['condition'] or
# sqft_above != min_dict['sqft_above'] or
# sqft_basement != min_dict['sqft_basement'] or
# yr_built != min_dict['yr_built'] or
# yr_renovated != min_dict['yr_renovated']):
new_pred = init_new_pred()
new_pred['bedrooms'] = bedrooms
new_pred['bathrooms'] = bathrooms
new_pred['sqft_living'] = sqft_living
new_pred['sqft_lot'] = sqft_lot
new_pred['floors'] = floors
new_pred['waterfront'] = int(waterfront)
new_pred['view'] = view
new_pred['condition'] = condition
new_pred['sqft_above'] = sqft_above
new_pred['sqft_basement'] = sqft_basement
new_pred['yr_built'] = yr_built
new_pred['yr_renovated'] = yr_renovated
new_pred[f'city_{city}'] = 1
# Process the prediction
new_pred = pd.DataFrame([new_pred])
new_pred = create_new_features(new_pred)
new_pred = normalize(new_pred)
# Predict the price
predicted_price = model.predict(new_pred)
# Display the predicted price at the top of the app
price_placeholder.write(f"Predicted Price: ${predicted_price[0][0]:,.2f}")
# Display the map in the second column
with col2:
st.subheader('Map')
if city == 'Algona':
map_data = pd.DataFrame({
'latitude': [47.6097, 47.6205, 47.6762],
'longitude': [-122.3331, -122.3493, -122.3198]
})
elif city == 'Auburn':
map_data = pd.DataFrame({
'latitude': [47.6101, 47.6183],
'longitude': [-122.2015, -122.2046]
})
st.map(map_data)