Swapnilbpatil's picture
Update app.py
b79ca46 verified
import streamlit as st
import pandas as pd
import pickle
# Load your pre-trained model
with open('house.pkl', 'rb') as file:
model = pickle.load(file)
st.markdown(
"""
<style>
.stApp {
max-width: 600px;
margin: auto;
}
.stTextInput, .stNumberInput, .stSelectbox {
font-size: 14px;
padding: 5px;
}
.stButton button {
width: 100%;
background-color: #4CAF50;
color: white;
}
</style>
""",
unsafe_allow_html=True
)
# Center the title and input fields
st.markdown("<h1 style='text-align: center;'>Real Estate Price Prediction</h1>", unsafe_allow_html=True)
# Input fields (centered in the middle)
area = st.number_input('Enter Area (in sqft)', min_value=0)
bedrooms = st.selectbox('No. of Bedrooms', [1, 2, 3, 4, 5])
gymnasium = st.selectbox('Gymnasium', ['Yes', 'No'])
swimming_pool = st.selectbox('Swimming Pool', ['Yes', 'No'])
# Location dropdown
location = st.selectbox('Select Location', ['Kharghar', 'Sector-13 Kharghar', 'Sector 18 Kharghar',
'Sector 20 Kharghar', 'Sector 15 Kharghar', 'Dombivali',
'Churchgate', 'Prabhadevi', 'Jogeshwari West', 'Kalyan East',
'Malad East', 'Virar East', 'Virar', 'Malad West', 'Borivali East',
'Mira Road East', 'Goregaon West', 'Kandivali West',
'Borivali West', 'Kandivali East', 'Andheri East', 'Goregaon East',
'Wadala', 'Ulwe', 'Dahisar', 'kandivali', 'Goregaon',
'Bhandup West', 'thakur village kandivali east', 'Santacruz West',
'Kanjurmarg', 'I C Colony', 'Dahisar W', 'Marol', 'Parel',
'Lower Parel', 'Worli', 'Jogeshwari East', 'Chembur Shell Colony',
'Central Avenue', 'Chembur East', 'Diamond Market Road', 'Mulund',
'Nalasopara West', 'raheja vihar', 'Powai Lake', 'MHADA Colony 20',
'Tolaram Colony', 'Taloja', 'Thane West', 'Vangani',
'Sector 5 Ulwe', 'Sector12 New Panvel', 'Sector 17 Ulwe',
'Sector9 Kamothe', 'Sector 19 Kharghar', 'Navi Basti'])
# Convert 'Yes'/'No' to 0/1 for Gymnasium and Swimming Pool
gymnasium = 1 if gymnasium == 'Yes' else 0
swimming_pool = 1 if swimming_pool == 'Yes' else 0
# Create a DataFrame for the inputs
input_data = {
'Area': [area],
'No. of Bedrooms': [bedrooms],
'Gymnasium': [gymnasium],
'Swimming Pool': [swimming_pool],
'Location': [location]
}
df_input = pd.DataFrame(input_data)
# One-hot encode the 'Location' column
df_input_encoded = pd.get_dummies(df_input, columns=['Location'])
# Ensure all location columns in the model are present in the input
# (add missing columns as zeros)
model_columns = [col for col in model.feature_names_in_ if col != 'Price']
for col in model_columns:
if col not in df_input_encoded.columns:
df_input_encoded[col] = 0
# Reorder the columns to match the model's training data
df_input_encoded = df_input_encoded[model_columns]
# Make prediction
if st.button('Predict Price'):
prediction = model.predict(df_input_encoded)[0]
st.markdown(f"<h2 style='text-align: center;'>Predicted Price: <strong>{prediction:.2f}</strong></h2>", unsafe_allow_html=True)