Real-Estate price prediction ML model to a working web application

House Price Prediction, Real Estate data, ML Model

Let’s turn our real estate price prediction model into a working web application.

Option 1: Flask Web App (Python + HTML)

A lightweight setup ideal if you want full control over frontend design.

What You will Build:

  • A form to enter house details

  • A backend that loads your trained model and

  • returns the predicted price

 

Step 1: Save the Trained Model

In your existing Python script, after training:

import joblib

# Save the trained model
joblib.dump(model, 'price_model.pkl')

 

 Step 2: Project Structure

real_estate_app/
│
├── app.py                  # Flask backend
├── price_model.pkl         # Trained model
├── templates/
│   └── index.html          # Frontend form
└── static/
    └── style.css           # Optional styling

 

Step 3:

app.py

(Flask Backend)
from flask import Flask, render_template, request
import pandas as pd
import joblib

app = Flask(__name__)
model = joblib.load('price_model.pkl')

# Update this list with all location dummy variables used in training
location_columns = ['Location_New York', 'Location_San Francisco', 'Location_Chicago']

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    data = request.form
    size = float(data['size'])
    bedrooms = int(data['bedrooms'])
    bathrooms = int(data['bathrooms'])
    year_built = int(data['year_built'])
    location = data['location']

    # Initialize input with zeroes for all locations
    input_data = {
        'Size (sqft)': size,
        'Bedrooms': bedrooms,
        'Bathrooms': bathrooms,
        'Year Built': year_built,
    }
    for loc in location_columns:
        input_data[loc] = 1 if loc == f'Location_{location}' else 0

    input_df = pd.DataFrame([input_data])
    prediction = model.predict(input_df)[0]
    return render_template('index.html', prediction=f"${prediction:,.2f}")

if __name__ == '__main__':
    app.run(debug=True)

templates/index.html

(Form + Result)
<!DOCTYPE html>
<html>
<head>
    <title>Real Estate Price Predictor</title>
</head>
<body>
    <h2>Enter Property Details</h2>
    <form method="POST" action="/predict">
        <label>Size (sqft):</label><input name="size" required><br>
        <label>Bedrooms:</label><input name="bedrooms" required><br>
        <label>Bathrooms:</label><input name="bathrooms" required><br>
        <label>Year Built:</label><input name="year_built" required><br>
        <label>Location:</label>
        <select name="location">
            <option value="New York">New York</option>
            <option value="San Francisco">San Francisco</option>
            <option value="Chicago">Chicago</option>
        </select><br>
        <button type="submit">Predict Price</button>
    </form>
    {% if prediction %}
        <h3>Predicted Price: {{ prediction }}</h3>
    {% endif %}
</body>
</html>

 

Option 2: Streamlit Web App

Fastest way to build interactive ML dashboards – no HTML required.

 

app.py 

(Streamlit Script)
import streamlit as st
import pandas as pd
import joblib

# Load the model
model = joblib.load('price_model.pkl')

# Define all possible locations used in training
location_columns = ['Location_New York', 'Location_San Francisco', 'Location_Chicago']

st.title(" Real Estate Price Predictor")

size = st.number_input("Size (sqft)", value=1000)
bedrooms = st.number_input("Bedrooms", min_value=0, step=1, value=3)
bathrooms = st.number_input("Bathrooms", min_value=0, step=1, value=2)
year_built = st.number_input("Year Built", min_value=1800, max_value=2025, value=2000)
location = st.selectbox("Location", ["New York", "San Francisco", "Chicago"])

# Prepare input data
input_data = {
    'Size (sqft)': size,
    'Bedrooms': bedrooms,
    'Bathrooms': bathrooms,
    'Year Built': year_built,
}
for loc in location_columns:
    input_data[loc] = 1 if loc == f'Location_{location}' else 0

input_df = pd.DataFrame([input_data])

if st.button("Predict Price"):
    prediction = model.predict(input_df)[0]
    st.success(f"Predicted Price: ${prediction:,.2f}")

 

To Run the Streamlit App:

streamlit run app.py

Requirements File (requirements.txt)

For either setup, include:

pandas
scikit-learn
joblib
flask       # For Flask version
streamlit   # For Streamlit version

 

 Recommendation

  • Use Streamlit if you want fast development, live interaction, and easy UI

  • Use Flask if you want more control over frontend and possibly host it on a website with more custom UX

Leave a Comment

Your email address will not be published. Required fields are marked *