Rendering ML Model Deployment with FastAPI on Render #render #ml #fastapi

Posted by


Deploying a Machine Learning (ML) model in production can be a complex and challenging task. In this tutorial, we will walk you through the process of deploying an ML model using FastAPI in Render, a cloud platform for building and scaling web applications.

FastAPI is a modern web framework for building APIs with Python that is known for its speed and ease of use. Render, on the other hand, is a cloud platform that allows you to deploy and scale web applications easily.

Prerequisites:

  • Basic knowledge of Python programming
  • A trained Machine Learning model (in this tutorial, we will use a simple linear regression model as an example)
  • An account on Render (sign up at https://render.com/ if you don’t have one)

Step 1: Set up your project
Create a new Python project directory and navigate to it in your terminal. Install the required dependencies with the following command:

pip install fastapi uvicorn numpy

Step 2: Create your FastAPI app
Create a new file called app.py in your project directory and import the necessary modules:

from fastapi import FastAPI
import numpy as np

Next, define your FastAPI app and create an endpoint for serving predictions. For this tutorial, we will create a simple linear regression model to predict the target variable based on two input features:

app = FastAPI()

# Load your trained ML model
model = # Load your trained model here

@app.post("/predict")
async def predict(input_data: dict):
    features = [input_data['feature1'], input_data['feature2']]
    prediction = model.predict(np.array([features]))
    return {"prediction": prediction[0]}

Step 3: Deploy your app on Render
Create a new file called render.yaml in your project directory to configure your Render deployment:

services:
  - name: fastapi
    env: python
    dockerfile: Dockerfile

Create a new file called Dockerfile in your project directory to define the configuration of your Docker image:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
COPY ./app.py /app
COPY ./model.pkl /app

The model.pkl file contains your trained machine learning model. Make sure to include it in your project directory.

Next, deploy your app on Render by following these steps:

  1. Log in to your Render account and create a new web service.
  2. Choose the GitHub repository where your project is hosted.
  3. Configure the deployment settings as follows:
    • Build command: leave it empty
    • Start command: uvicorn app:app --host 0.0.0.0 --port $PORT
  4. Click "Create Web Service" to deploy your app.

Step 4: Test your deployed ML model
Once your app is deployed, you can test it by sending a POST request to the /predict endpoint with sample input data:

curl -X 'POST' 
  'https://your-app-name.onrender.com/predict' 
  -d '{
    "feature1": 1.5,
    "feature2": 2.0
  }'

You should receive a response with the predicted value from your ML model.

Congratulations! You have successfully deployed a Machine Learning model using FastAPI in Render. You can now build upon this tutorial to deploy more complex ML models or create additional endpoints for your API. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@tharunps8048
2 hours ago

Which is best option to deploy ML model ?…..AWS Lambda or EC2 ?…..I will be getting 10k hits per month

1
0
Would love your thoughts, please comment.x
()
x