RMHalak's picture
Update app.py
a168a39 verified
raw
history blame
4.04 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)
# Placeholder for displaying the predicted price at the top
price_placeholder = st.empty()
# 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 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.slider('Waterfront', min_value=min_dict['waterfront'], max_value=max_dict['waterfront'], value=min_dict['waterfront'])
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 (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
waterfront != min_dict['waterfront'] or
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'] = 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['city_Bellevue'] = 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}")