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.
- 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:
- Install FastAPI and Uvicorn:
pip install fastapi uvicorn
- 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)}")
- 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.
- Install Nginx:
sudo apt install nginx
- 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;
}
- Test and Reload Nginx:
Test the Nginx configuration and reload:
sudo nginx -t
sudo nginx -s reload
- Deploying to AWS EC2
Now, let’s deploy the FastAPI app to AWS EC2 to make it accessible from anywhere. This involves several steps:
- 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).
- SSH into the EC2 Instance:
ssh -i "your-key.pem" ubuntu@<your-ec2-public-ip>
- 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/
- 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
- Run the Application:
Run the FastAPI app on the EC2 instance:
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
You can test the /predict endpoint using curl or Postman, or through the Swagger UI provided by FastAPI.
- 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
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:
Top comments (0)