Deploying a Machine Learning Model as an
API using FastAPI on Python
1. Introduction
This report details the systematic approach to deploying a Logistic Regression model as an
API using FastAPI on Python. The API allows users to send input features and receive a
prediction about heart health.
2. Prerequisites
Before proceeding, ensure the following are installed:
pip install fastapi uvicorn pydantic pickle-mixin
Required Libraries:
FastAPI: Web framework for creating APIs.
Pydantic: Data validation and parsing for request bodies.
Pickle: Model serialization and deserialization.
JSON: Handling input and output data.
3. Loading the Model
The API loads a trained Logistic Regression model using Pickle:
import pickle
# Load the trained model
Logistic_Regression = pickle.load(open('LogisticRegression.sav', 'rb'))
Ensure that the 'LogisticRegression.sav' file exists in the working directory.
This model is used for heart health predictions based on given patient data.
4. API Implementation using FastAPI
4.1 Defining the API Structure
from fastapi import FastAPI
from pydantic import BaseModel
import json
FastAPI is used to create the API instance.
BaseModel from Pydantic is used for input validation.
4.2 Creating the Input Model
class ModelInput(BaseModel):
age: int
sex: int
cp: int
trestbps: int
chol: int
fbs: int
restecg: int
thalachh: int
exang: int
oldpeak: float
slope: int
ca: int
thal: int
target: int
This ensures that all request parameters follow the defined data structure.
4.3 Defining the Prediction Endpoint
@app.post('/predict')
def predict_heart_health(input_parameters: ModelInput):
input_data = input_parameters.dict()
input_list = [
input_data['age'], input_data['sex'], input_data['cp'],
input_data['trestbps'],
input_data['chol'], input_data['fbs'], input_data['restecg'],
input_data['thalachh'],
input_data['exang'], input_data['oldpeak'], input_data['slope'],
input_data['ca'],
input_data['thal'], input_data['target']
]
prediction = Logistic_Regression.predict([input_list])
return {"prediction": "Heart is fit" if prediction[0] == 0 else "Heart
is not fit"}
Receives JSON input, extracts values, and converts them into a list.
Passes the list to the trained model for prediction.
Returns a human-readable prediction response.
5. Running the API Locally
To run the API locally, execute:
uvicorn ML_api:app --host 0.0.0.0 --port 8000
ML_api refers to the Python script containing the FastAPI code.
The API will be available at: http://127.0.0.1:8000/docs (Swagger UI).
6. Testing the API
Using CURL Command
curl -X 'POST' \
'http://127.0.0.1:8000/predict' \
-H 'Content-Type: application/json' \
-d '{"age": 45, "sex": 1, "cp": 3, "trestbps": 130, "chol": 233, "fbs":
0, "restecg": 1, "thalachh": 150, "exang": 0, "oldpeak": 2.3, "slope": 2,
"ca": 0, "thal": 2, "target": 1}'
Using Postman
1. Open Postman.
2. Select POST request.
3. Enter API URL: http://127.0.0.1:8000/predict
4. Go to Body > raw > JSON and input:
5. {
6. "age": 45,
7. "sex": 1,
8. "cp": 3,
9. "trestbps": 130,
10. "chol": 233,
11. "fbs": 0,
12. "restecg": 1,
13. "thalachh": 150,
14. "exang": 0,
15. "oldpeak": 2.3,
16. "slope": 2,
17. "ca": 0,
18. "thal": 2,
19. "target": 1
}
20. Click Send and receive a response.