DEV Community

JinChul Moon
JinChul Moon

Posted on

Building a House Price Prediction Model with FastAPI and Deployment

In the previous post, we built a Random Forest model to predict house prices using the Toronto housing dataset. Now, let’s move forward by creating a FastAPI web application to serve this model and deploy it so others can interact with it. Below, I’ll guide you through the process of creating the FastAPI application, setting up Nginx as a reverse proxy, and deploying the model on AWS EC2.

  1. Setting Up the FastAPI Application

First, we need to set up the FastAPI app that will serve our machine learning model. We’ll structure the app as follows:

  1. Install FastAPI and Uvicorn:

pip install fastapi uvicorn

  1. Create the FastAPI App:

In the app.py file, we will create a POST endpoint /predict that accepts the input features of the house and returns the predicted price using our trained model.

from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import pandas as pd
import numpy as np
import logging

# Load the trained model
model_path = "models/random_forest_model.pkl"
model = joblib.load(model_path)

app = FastAPI()

# Define the input data model
class HouseFeatures(BaseModel):
    sqft: int
    bedrooms: int
    bathrooms: int
    parking: int
    mean_district_income: int

@app.post("/predict")
async def predict_house_price(data: HouseFeatures):
    try:
        input_data = data.dict()
        logging.info(f"🔍 Received input: {input_data}")

        input_df = pd.DataFrame([input_data])
        input_df = input_df.reindex(columns=expected_features, fill_value=0)

        logging.info(f"📊 Processed DataFrame for prediction:\n{input_df}")

        predicted_price = model.predict(input_df)[0]

        if predicted_price < 20:
            predicted_price = np.exp(predicted_price)

        logging.info(f"💰 Predicted price: {predicted_price}")

        return {"predicted_price": predicted_price}

    except Exception as e:
        logging.error(f"❌ Prediction error: {str(e)}")
        raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
Enter fullscreen mode Exit fullscreen mode
  1. Setting Up Reverse Proxy with Nginx

After your FastAPI app is working locally, you can set it up on a server. For production, we recommend using Nginx as a reverse proxy to route requests from port 80 or 8080 to FastAPI running on port 8000.

  1. Install Nginx:
sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode
  1. Nginx Configuration:

Edit the /etc/nginx/nginx.conf file or the server block configuration to forward the requests to FastAPI.

server {
    listen 8080;
    server_name localhost;

    location / {
        proxy_pass http://127.0.0.1:8000;  # Forward requests to FastAPI server
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /predict {
        proxy_pass http://127.0.0.1:8000/predict;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    root /usr/share/nginx/html;
    index index.html;
}
Enter fullscreen mode Exit fullscreen mode
  1. Test and Reload Nginx:

Test the Nginx configuration and reload:

sudo nginx -t
sudo nginx -s reload
Enter fullscreen mode Exit fullscreen mode
  1. Deploying to AWS EC2

Now, let’s deploy the FastAPI app to AWS EC2 to make it accessible from anywhere. This involves several steps:

  1. Launch an EC2 Instance:

• Use AWS EC2 to create an instance.

• Choose an appropriate AMI, such as the latest Ubuntu.

• Ensure the security group allows inbound traffic on ports 80 (HTTP), 443 (HTTPS), and 8000 (for FastAPI).

  1. SSH into the EC2 Instance:
ssh -i "your-key.pem" ubuntu@<your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode
  1. Transfer Files to EC2:

Use scp to copy your project files to the server:

scp -i "your-key.pem" /path/to/app.py ubuntu@<your-ec2-public-ip>:/home/ubuntu/
Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies on EC2:

Inside the EC2 instance, create a virtual environment and install FastAPI, Uvicorn, and other dependencies.

python3 -m venv fastapi_env
source fastapi_env/bin/activate
pip install fastapi uvicorn joblib pandas scikit-learn
Enter fullscreen mode Exit fullscreen mode
  1. Run the Application:

Run the FastAPI app on the EC2 instance:

uvicorn app:app --host 0.0.0.0 --port 8000 --reload
Enter fullscreen mode Exit fullscreen mode

You can test the /predict endpoint using curl or Postman, or through the Swagger UI provided by FastAPI.

  1. Accessing the API

Once the app is running, you can access it via the public IP of the EC2 instance:

http://3.135.188.21:8000/docs
Enter fullscreen mode Exit fullscreen mode

You can test the /predict endpoint using curl or Postman, or through the Swagger UI provided by FastAPI.

GitHub Repository

You can access the source code for this project on my GitHub repository:

GitHub – House Price Prediction

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)