File size: 4,668 Bytes
d9259c8 ba29d97 715d7b7 0efbd32 715d7b7 ba29d97 715d7b7 ba29d97 715d7b7 ba29d97 715d7b7 ba29d97 715d7b7 ba29d97 715d7b7 d9259c8 ba29d97 d9259c8 ba29d97 d9259c8 ba29d97 d9259c8 ba29d97 715d7b7 d9259c8 715d7b7 d9259c8 715d7b7 d9259c8 ba29d97 d9259c8 ba29d97 d9259c8 ba29d97 d6abccc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import streamlit as st
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
# Custom Styling
st.markdown(
"""
<style>
.stApp {
background: url('https://static.vecteezy.com/system/resources/thumbnails/002/019/515/small_2x/house-rotating-background-free-video.jpg') no-repeat center center fixed;
background-size: cover;
}
.stApp h1 {
background-color: rgba(0, 0, 128, 0.7);
color: #ffffff;
padding: 10px;
border-radius: 5px;
font-size: 2.5em;
text-align: center;
}
.stTextArea textarea {
background-color: rgba(255, 255, 255, 0.8);
color: #000000;
font-size: 1.2em;
}
.stButton>button {
background-color: #4CAF50;
color: white;
font-size: 1.2em;
border-radius: 10px;
padding: 10px 24px;
border: none;
}
.stButton {
display: flex;
justify-content: center;
}
.output-container {
background-color: lightpink;
color: black;
font-size: 1.5em;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
max-width: 600px;
margin-left: auto;
margin-right: auto;
text-align: center;
}
</style>
""",
unsafe_allow_html=True
)
st.title("House Price Prediction")
# Define ANN Model
class ANN_Model(nn.Module):
def __init__(self, input_cols=11, hidden0=128, hidden1=128, hidden2=128,
hidden3=64, hidden4=64, hidden5=32, hidden6=16, output=1):
super().__init__()
self.f_connected0 = nn.Linear(input_cols, hidden0)
self.f_connected1 = nn.Linear(hidden0, hidden1)
self.f_connected2 = nn.Linear(hidden1, hidden2)
self.f_connected3 = nn.Linear(hidden2, hidden3)
self.f_connected4 = nn.Linear(hidden3, hidden4)
self.f_connected5 = nn.Linear(hidden4, hidden5)
self.f_connected6 = nn.Linear(hidden5, hidden6)
self.out = nn.Linear(hidden6, output)
def forward(self, x):
x = F.relu(self.f_connected0(x))
x = F.relu(self.f_connected1(x))
x = F.relu(self.f_connected2(x))
x = F.relu(self.f_connected3(x))
x = F.relu(self.f_connected4(x))
x = F.relu(self.f_connected5(x))
x = F.relu(self.f_connected6(x))
x = self.out(x)
return x
# Load the trained model
model = ANN_Model()
try:
model.load_state_dict(torch.load("ANN_model.pth", map_location=torch.device('cpu')))
model.eval()
except Exception as e:
st.error(f"❌ Failed to load the model: {e}")
# Input UI in 3 Columns
col1, col2, col3 = st.columns(3)
with col1:
year_built = st.number_input("Year Built", min_value=1800, max_value=2025, value=2000)
num_bedrooms = st.number_input("Number of Bedrooms", min_value=1, max_value=10, value=3)
garage_type = st.selectbox("Garage Type", ['attached', 'detached', 'none'])
garage_type_encoded = 2 if garage_type == 'attached' else 1 if garage_type == 'detached' else 0
has_fireplace = st.selectbox("Has Fireplace", ["True", "False"])
has_fireplace = 1 if has_fireplace == "True" else 0
with col2:
num_of_bathrooms = st.number_input("Number of Bathrooms", min_value=0, max_value=10, value=2)
total_sqft = st.number_input("Total Square Feet", min_value=100, max_value=10000, value=1800)
garage_sqft = st.number_input("Garage Square Feet", min_value=0, max_value=2000, value=400)
has_pool = st.selectbox("Has Pool", ["True", "False"])
has_pool = 1 if has_pool == "True" else 0
with col3:
has_central_heating = st.selectbox("Has Central Heating", ["True", "False"])
has_central_heating = 1 if has_central_heating == "True" else 0
has_central_cooling = st.selectbox("Has Central Cooling", ["True", "False"])
has_central_cooling = 1 if has_central_cooling == "True" else 0
zip_code = st.number_input("ZIP Code", min_value=10000, max_value=99999, value=11203)
# Prediction Logic
if st.button('Predict House Price'):
input_features = np.array([
year_built, num_bedrooms, total_sqft, garage_type_encoded, garage_sqft,
has_fireplace, has_pool, has_central_heating, has_central_cooling, zip_code, num_of_bathrooms
], dtype=np.float32)
input_tensor = torch.tensor(input_features).unsqueeze(0) # Batch dimension
with torch.no_grad():
predicted_price = model(input_tensor).item()
st.markdown(
f'<div class="output-container">Estimated House Price: <b>${predicted_price:,.2f}</b></div>',
unsafe_allow_html=True
) |