/web-to-ai-ml-integrations

Turn ML Model into API Using FastAPI

Transform your ML model to an API with FastAPI. Follow our step-by-step guide for a quick, easy, and powerful deployment.

Book a free  consultation
4.9
Clutch rating 🌟
600+
Happy partners
17+
Countries served
190+
Team members
Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

Turn ML Model into API Using FastAPI

Loading and Preparing Your Machine Learning Model

 

  • Start by importing your trained ML model using a serialization library such as joblib or pickle (both are common in Python for saving and loading models).
  • If your model was built in scikit-learn, for instance, use joblib to load the model from a saved file. The model will be used to predict new inputs without retraining.

// Import the necessary libraries for model loading
import joblib
// Load the pre-trained model from the file system
model = joblib.load('saved\_model.pkl')
  • This step ensures that your model is ready for inference and will be accessible throughout the API lifecycle.

Integrating FastAPI to Serve the Model

 

  • Import FastAPI, a modern and fast web framework for building APIs with Python, built on top of standard Python type hints.
  • Create an instance of FastAPI and define an endpoint that accepts input data, processes it through your ML model, and returns predictions.

// Import FastAPI for creating the API
from fastapi import FastAPI
// Import pydantic for request body validation
from pydantic import BaseModel

// Create a FastAPI instance
app = FastAPI()

// Define the expected request body structure using a Pydantic model
class InputData(BaseModel):
    // Example: a list of float numbers that the model requires as input
    features: list[float]

// Define an endpoint to handle prediction requests
@app.post("/predict")
async def predict(data: InputData):
    // Convert input features to the format needed by your model (e.g., list to numpy array)
    sample = [data.features]
    // Get the prediction from the loaded model
    prediction = model.predict(sample)
    // Return the prediction in a dictionary, which FastAPI automatically serializes to JSON
    return {"prediction": prediction.tolist()}
  • This endpoint uses POST because model prediction typically requires passing data using the HTTP request body, and the request body is automatically validated using Pydantic.
  • Using async is optional but recommended for high-concurrency scenarios.

Running the API Server

 

  • FastAPI uses the Uvicorn ASGI server by default for running the application in a production or development environment.
  • You need to run the API server with Uvicorn, specifying the file name and app variable name (e.g., "app:app").

// Running the server via command line
// uvicorn app:app --reload
// The --reload flag is useful for development as it restarts the server on code changes
  • This command starts the API, making it accessible on the default port (often 8000), and the endpoint will be ready to accept requests.

Testing the Endpoint

 

  • Once the server is running, test your endpoint using tools like Postman or curl.
  • You can also visit the auto-generated documentation at /docs path, which FastAPI automatically provides.

// Example curl command to test the API
// curl -X POST "http://127.0.0.1:8000/predict" -H "Content-Type: application/json" -d '{"features": [5.1, 3.5, 1.4, 0.2]}'
  • This documentation tool auto-generates an interactive API documentation interface, allowing you to experiment with various inputs.

Final Considerations

 

  • Make sure to handle exceptions and invalid inputs in your endpoint to ensure robustness.
  • For production use, consider adding logging, more thorough data validation, and security measures such as rate limiting and authentication.
  • Deploying your API might involve additional challenges such as containerization with Docker or using cloud services like AWS, Azure, or GCP.

// Example of exception handling in the prediction endpoint
@app.post("/predict")
async def predict(data: InputData):
    try:
        sample = [data.features]
        prediction = model.predict(sample)
    except Exception as e:
        // Return an error message with a proper HTTP status code
        return {"error": str(e)}
    return {"prediction": prediction.tolist()}
  • Following these steps, you will have a robust ML model deployed as an API, ready for integration within various applications.

 


Recognized by the best

Trusted by 600+ businesses globally

From startups to enterprises and everything in between, see for yourself our incredible impact.

RapidDev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with.

They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

Arkady
CPO, Praction
Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost.

He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Donald Muir
Co-Founder, Arc
RapidDev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space.

They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Mat Westergreen-Thorne
Co-CEO, Grantify
RapidDev is an excellent developer for custom-code solutions.

We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Emmanuel Brown
Co-Founder, Church Real Estate Marketplace
Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 

This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Samantha Fekete
Production Manager, Media Production Company
The pSEO strategy executed by RapidDev is clearly driving meaningful results.

Working with RapidDev has delivered measurable, year-over-year growth. Comparing the same period, clicks increased by 129%, impressions grew by 196%, and average position improved by 14.6%. Most importantly, qualified contact form submissions rose 350%, excluding spam.

Appreciation as well to Matt Graham for championing the collaboration!

Michael W. Hammond
Principal Owner, OCD Tech

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.Â