Deploy PyCaret Classification Models as Scalable APIs with FastAPI
Learn how to efficiently deploy your PyCaret machine learning classification models as robust and scalable APIs using FastAPI, making your predictions easily accessible.

Deploy PyCaret Classification Models as Scalable APIs with FastAPI
In the exciting world of machine learning, building a fantastic model is only half the battle. To truly bring value, these models need to be accessible and usable by other applications, often in the form of a robust and scalable API. This is where the power duo of PyCaret and FastAPI shines.
This article will guide you through the process of efficiently deploying a PyCaret machine learning model as a robust and scalable API using FastAPI. We'll cover everything from training and saving your model with PyCaret to building a lightning-fast prediction service with FastAPI.
Why PyCaret for Model Development?
PyCaret is an open-source, low-code machine learning library in Python that streamlines the end-to-end machine learning pipeline. It's built on top of popular libraries like scikit-learn, XGBoost, LightGBM, CatBoost, and many more, making it incredibly powerful yet simple to use.
Here's why PyCaret is an excellent choice for model development, especially when deployment is on the horizon:
- Rapid Prototyping: PyCaret allows you to go from data ingestion to model deployment readiness in minutes rather than hours.
- Automated ML: It automates many repetitive tasks like data preprocessing, model selection, hyperparameter tuning, and more.
- Model Comparison: Easily compare the performance of various models across different metrics with a single function call.
- Seamless Saving: PyCaret models can be saved and loaded with extreme ease, simplifying the integration into deployment pipelines.
For an api-deployment scenario, PyCaret's ability to quickly iterate and finalize a machine-learning model, and then save it in a deployable format, is a massive advantage.
Why FastAPI for API Deployment?
FastAPI is a modern, high-performance web framework for building APIs with Python 3.7+. It's known for its incredible speed, automatic interactive API documentation, and strong type-checking powered by Pydantic.
Here's why FastAPI is perfect for serving your ai models:
- Blazing Fast Performance: Built on Starlette for the web parts and Pydantic for data parts, FastAPI is one of the fastest Python web frameworks available.
- Developer Experience: It leverages Python type hints to provide fantastic editor support, auto-completion, and automatic data validation.
- Automatic Docs: Out-of-the-box interactive API documentation (Swagger UI and ReDoc) makes it easy for consumers to understand and use your API.
- Scalability: With asynchronous support (
async/await), FastAPI can handle a high volume of concurrent requests, making it inherently more scalable for demandingapi-deploymentscenarios.
Building and Saving Our PyCaret Model
Let's start by training a simple classification model using PyCaret. For this example, we'll use PyCaret's built-in diabetes dataset, aiming to predict the Class variable (whether a patient has diabetes) based on other health indicators.
First, ensure you have PyCaret installed:
pip install pycaret pandas scikit-learn
Now, create a Python script (e.g., train_model.py) to train and save your model:
# train_model.py
import pandas as pd
from pycaret.datasets import get_data
from pycaret.classification import setup, compare_models, finalize_model, save_model
print("Loading diabetes dataset...")
# Load the diabetes dataset
data = get_data('diabetes')
print("Setting up PyCaret environment...")
# Initialize setup with target variable
# session_id for reproducibility
clf_setup = setup(data=data, target='Class variable', session_id=123, silent=True, verbose=False)
print("Comparing models and finding the best one...")
# Compare all models and select the best performing one based on 'Accuracy'
best_model = compare_models()
print(f"Best model selected: {type(best_model).__name__}")
print("Finalizing the model...")
# Finalize the model to train it on the entire dataset
# This is crucial before deployment
final_model = finalize_model(best_model)
print("Saving the finalized model as 'diabetes_prediction_model.pkl'...")
# Save the model
save_model(final_model, 'diabetes_prediction_model')
print("Model training and saving complete!")
Run this script:
python train_model.py
This will output diabetes_prediction_model.pkl in your current directory, ready for deployment.
Designing Our FastAPI API
Now that we have a trained pycaret model, let's build the fastapi service. Our API will have a single POST endpoint /predict that accepts patient features and returns a diabetes prediction (0 or 1) along with the probability.
First, install FastAPI and Uvicorn (an ASGI server):
pip install fastapi uvicorn pydantic
Next, create app.py:
# app.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import uvicorn
import pandas as pd
from pycaret.classification import load_model
import os
# Initialize FastAPI app
app = FastAPI(
title="PyCaret Diabetes Prediction API",
description="A simple API to predict diabetes using a PyCaret-trained classification model.",
version="1.0.0"
)
# Global variable to hold the loaded model
model = None
# Define a Pydantic model for input data validation
# This schema directly reflects the features your model expects
class PredictionInput(BaseModel):
Pregnancies: int = Field(..., example=6, description="Number of times pregnant")
Glucose: int = Field(..., example=148, description="Plasma glucose concentration a 2 hours in an oral glucose tolerance test")
BloodPressure: int = Field(..., example=72, description="Diastolic blood pressure (mm Hg)")
SkinThickness: int = Field(..., example=35, description="Triceps skin fold thickness (mm)")
Insulin: int = Field(..., example=0, description="2-Hour serum insulin (mu U/ml)")
BMI: float = Field(..., example=33.6, description="Body mass index (weight in kg/(height in m)^2)")
DiabetesPedigreeFunction: float = Field(..., example=0.627, description="Diabetes pedigree function")
Age: int = Field(..., example=50, description="Age in years")
# Define a Pydantic model for the output data
class PredictionOutput(BaseModel):
prediction: int = Field(..., description="Predicted class (0 for No Diabetes, 1 for Diabetes)")
prediction_proba: float = Field(..., description="Probability of having Diabetes (class 1)")
@app.on_event("startup")
async def load_pycaret_model():
"""Load the PyCaret model when the FastAPI application starts."""
global model
model_path = 'diabetes_prediction_model.pkl' # Ensure this matches your saved model name
if os.path.exists(model_path):
print(f"Loading PyCaret model from {model_path}...")
model = load_model(model_path.replace('.pkl', '')) # PyCaret load_model expects name without .pkl
print("Model loaded successfully!")
else:
raise RuntimeError(f"Model file not found at {model_path}. Please train and save the model first.")
# Root endpoint for health check or information
@app.get("/", summary="Health Check", response_model=dict)
async def root():
return {"message": "PyCaret Diabetes Prediction API is running!"}
# Prediction endpoint
@app.post("/predict", response_model=PredictionOutput, summary="Predict Diabetes",
description="Predicts the likelihood of diabetes based on patient health metrics.")
async def predict(data: PredictionInput):
if model is None:
raise HTTPException(status_code=500, detail="Model not loaded. Server is still starting up or encountered an error.")
# Convert input data to a pandas DataFrame
# model_dump() is used for Pydantic v2. For v1, use .dict()
input_df = pd.DataFrame([data.model_dump()])
try:
# Make prediction using the loaded PyCaret model
predictions = model.predict(input_df)
prediction_proba = model.predict_proba(input_df)[:, 1] # Probability of positive class (1)
# Prepare response
result = {
"prediction": int(predictions[0]), # Convert numpy int to Python int
"prediction_proba": float(prediction_proba[0]) # Convert numpy float to Python float
}
return result
except Exception as e:
raise HTTPException(status_code=500, detail=f"Prediction failed: {e}")
# To run: uvicorn app:app --host 0.0.0.0 --port 8000
Running and Testing the API
To run your FastAPI application, navigate to the directory containing app.py and execute:
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
The --reload flag is great for development as it restarts the server on code changes. For production, you'd typically omit this and use a process manager like Gunicorn with Uvicorn workers for scalability.
Once the server is running, open your web browser and go to http://0.0.0.0:8000/docs. You'll be greeted by the interactive Swagger UI, automatically generated by FastAPI based on your Pydantic models and endpoint definitions. This makes testing incredibly easy!
You can also test the API using curl or a Python requests script:
curl -X 'POST' \
'http://localhost:8000/predict' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"Pregnancies": 2,
"Glucose": 100,
"BloodPressure": 70,
"SkinThickness": 25,
"Insulin": 0,
"BMI": 28.5,
"DiabetesPedigreeFunction": 0.4,
"Age": 30
}'
You should receive a JSON response similar to this:
{
"prediction": 0,
"prediction_proba": 0.215432
}
Scalability Considerations for Production
While FastAPI and Uvicorn provide a strong foundation, for a truly scalable api-deployment, consider these points:
-
Gunicorn Workers: In production, run Uvicorn with Gunicorn (a WSGI HTTP server) to manage multiple Uvicorn worker processes. This allows you to leverage multiple CPU cores and handle more concurrent requests.
pip install gunicorn gunicorn -w 4 -k uvicorn.workers.UvicornWorker app:app --bind 0.0.0.0:8000(Replace
-w 4with the number of CPU cores you want to utilize.) -
Containerization (Docker): Package your application and its dependencies into a Docker container. This ensures consistent environments across development, testing, and production, and simplifies deployment to cloud platforms.
-
Cloud Deployment: Platforms like AWS ECS, Google Cloud Run, Azure Container Instances, or Kubernetes are excellent choices for deploying containerized FastAPI applications, offering robust scaling, load balancing, and monitoring capabilities.
-
Asynchronous Model Loading: For very large models or multiple models, you might consider optimizing model loading further, perhaps by loading models in separate processes or using a dedicated model serving solution for inference.
Conclusion
You've now learned how to combine the rapid model development capabilities of PyCaret with the high-performance and developer-friendly FastAPI framework. This powerful combination enables you to efficiently deploy machine-learning models as scalable and robust AI APIs.
By following these steps, you can bridge the gap between model experimentation and real-world application, making your intelligent systems accessible and impactful. The clear structure, automatic documentation, and inherent speed of FastAPI, coupled with PyCaret's ease of use, provide a fantastic foundation for any Python developer looking to deploy their AI solutions with confidence. Now go forth and deploy!
Share
Post to your network or copy the link.
Learn more
Curated resources referenced in this article.
Related
More posts to read next.
- omi: Building Secure & Private Screen-Aware AI with Python & FastAPI
Explore the critical security and privacy challenges of developing screen-aware AI assistants like omi, and learn how Python, LLM best practices, and FastAPI can help build them responsibly.
Read - Build a Resilient LLM Backend with FastAPI and Eden AI: A European OpenRouter Alternative
Learn to build a flexible, privacy-aware LLM application backend using FastAPI and Eden AI, a European unified API gateway for managing diverse AI models and addressing regional compliance.
Read