Spaces:
Paused
Paused
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: | |
st.subheader('Features') | |
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'], | |
) | |
waterfront = st.checkbox('Waterfront', value=False) | |
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']) | |
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']) | |
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 map in the second column | |
with col2: | |
st.subheader('Map') | |
if city == 'Seattle': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6097, 47.6205, 47.6762], | |
'longitude': [-122.3331, -122.3493, -122.3198] | |
}) | |
elif city == 'Bellevue': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6101, 47.6183], | |
'longitude': [-122.2015, -122.2046] | |
}) | |
elif city == 'Algona': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3162], | |
'longitude': [-122.2295] | |
}) | |
elif city == 'Auburn': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3073], | |
'longitude': [-122.2284] | |
}) | |
elif city == 'Beaux Arts Village': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6141], | |
'longitude': [-122.2125] | |
}) | |
elif city == 'Black Diamond': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3465], | |
'longitude': [-121.9877] | |
}) | |
elif city == 'Bothell': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7595], | |
'longitude': [-122.2056] | |
}) | |
elif city == 'Burien': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4702], | |
'longitude': [-122.3359] | |
}) | |
elif city == 'Carnation': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6460], | |
'longitude': [-121.9758] | |
}) | |
elif city == 'Clyde Hill': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6330], | |
'longitude': [-122.2107] | |
}) | |
elif city == 'Covington': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3765], | |
'longitude': [-122.0288] | |
}) | |
elif city == 'Des Moines': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3840], | |
'longitude': [-122.3061] | |
}) | |
elif city == 'Duvall': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7332], | |
'longitude': [-121.9916] | |
}) | |
elif city == 'Enumclaw': | |
map_data = pd.DataFrame({ | |
'latitude': [47.2059], | |
'longitude': [-121.9876] | |
}) | |
elif city == 'Fall City': | |
map_data = pd.DataFrame({ | |
'latitude': [47.5980], | |
'longitude': [-121.8896] | |
}) | |
elif city == 'Federal Way': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3220], | |
'longitude': [-122.3126] | |
}) | |
elif city == 'Inglewood-Finn Hill': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7338], | |
'longitude': [-122.2780] | |
}) | |
elif city == 'Issaquah': | |
map_data = pd.DataFrame({ | |
'latitude': [47.5410], | |
'longitude': [-122.0311] | |
}) | |
elif city == 'Kenmore': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7557], | |
'longitude': [-122.2416] | |
}) | |
elif city == 'Kent': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3809], | |
'longitude': [-122.2348] | |
}) | |
elif city == 'Kirkland': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6810], | |
'longitude': [-122.2087] | |
}) | |
elif city == 'Lake Forest Park': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7318], | |
'longitude': [-122.2764] | |
}) | |
elif city == 'Maple Valley': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3610], | |
'longitude': [-122.0240] | |
}) | |
elif city == 'Medina': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6357], | |
'longitude': [-122.2169] | |
}) | |
elif city == 'Mercer Island': | |
map_data = pd.DataFrame({ | |
'latitude': [47.5703], | |
'longitude': [-122.2264] | |
}) | |
elif city == 'Milton': | |
map_data = pd.DataFrame({ | |
'latitude': [47.2335], | |
'longitude': [-122.2730] | |
}) | |
elif city == 'Newcastle': | |
map_data = pd.DataFrame({ | |
'latitude': [47.5477], | |
'longitude': [-122.1711] | |
}) | |
elif city == 'Normandy Park': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4051], | |
'longitude': [-122.3376] | |
}) | |
elif city == 'North Bend': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4904], | |
'longitude': [-121.7852] | |
}) | |
elif city == 'Pacific': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3197], | |
'longitude': [-122.2786] | |
}) | |
elif city == 'Preston': | |
map_data = pd.DataFrame({ | |
'latitude': [47.5420], | |
'longitude': [-121.9214] | |
}) | |
elif city == 'Ravensdale': | |
map_data = pd.DataFrame({ | |
'latitude': [47.3485], | |
'longitude': [-121.9807] | |
}) | |
elif city == 'Redmond': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6734], | |
'longitude': [-122.1215] | |
}) | |
elif city == 'Renton': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4829], | |
'longitude': [-122.2170] | |
}) | |
elif city == 'Sammamish': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6162], | |
'longitude': [-122.0394] | |
}) | |
elif city == 'SeaTac': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4484], | |
'longitude': [-122.3085] | |
}) | |
elif city == 'Shoreline': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7554], | |
'longitude': [-122.3410] | |
}) | |
elif city == 'Skykomish': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7054], | |
'longitude': [-121.4848] | |
}) | |
elif city == 'Snoqualmie': | |
map_data = pd.DataFrame({ | |
'latitude': [47.5410], | |
'longitude': [-121.8340] | |
}) | |
elif city == 'Snoqualmie Pass': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4286], | |
'longitude': [-121.4420] | |
}) | |
elif city == 'Tukwila': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4835], | |
'longitude': [-122.2585] | |
}) | |
elif city == 'Vashon': | |
map_data = pd.DataFrame({ | |
'latitude': [47.4337], | |
'longitude': [-122.4660] | |
}) | |
elif city == 'Woodinville': | |
map_data = pd.DataFrame({ | |
'latitude': [47.7524], | |
'longitude': [-122.1576] | |
}) | |
elif city == 'Yarrow Point': | |
map_data = pd.DataFrame({ | |
'latitude': [47.6348], | |
'longitude': [-122.2218] | |
}) | |
st.map(map_data) | |
# Placeholder for displaying the predicted price at the top | |
price_placeholder = st.empty() | |
# Display the predicted price at the top of the app | |
# price_placeholder.write(f"Predicted Price: ${predicted_price[0][0]:,.2f}") | |
price_placeholder.markdown( | |
f"<h1 style='font-size: 24px;'>Predicted Price: ${predicted_price[0][0]:,.2f}</h1>", | |
unsafe_allow_html=True | |
) | |