kedro-fast-api: A Kedro Plugin for Integrating Fastapi Into Your Kedro Project

M Hamid Asn
3 min readDec 27, 2022

--

In the previous article, we already knew how to use one of the Kedro plugins to track the lifecycle of our machine learning project. In this article, we will learn about kedro-fast-api, another Kedro plugin that will make our machine learning project usable.

To accomplish this, we will use an API, or application programming interface, which is one of the most prevalent methods of integrating technology services. If you are not really familiar with APIs, the main objective of an API is that it allows you to interact with an application without knowing how it was built. It’s akin to going to a restaurant, placing an order, and then receiving it without understanding how it was cooked in the kitchen.

kedro-fast-api

kedro-fast-api is a Kedro plugin created by the Indicum team to create APIs using Fast API. Because it is integrated with our Kedro project, contains a few additional configuration files, and takes advantage of all the capabilities of Kedro, this plugin makes it easy for us to establish an API.

initialization

To initialize the kedro-fast-api plugin on the kedro project that has been created first, of course make sure that the kedro-fast-api plugin is installed (which can be easily done by running pip install kedro-fast-api on the CLI), then on the CLI change directory to the intended kedro project, and run:

kedro fast-api init

This initialization will create 4 default plugin files, which are conf/api.yml, conf/base/api_pipeline/catalog.yml, src/api_pipeline/nodes.py, and src/api_pipeline/catalog.py.

#Kedro Project Structure
+---conf
| +---base
| | +---api_pipeline
| | | \---catalog.yml #This File
| \---api.yml #This File
+---data
+---docs
+---logs
+---notebooks
+---src
+---project_name
| +---pipelines
| | +---api_pipeline
| | | \---nodes.py #This File
| | | \---pipeline.py #This File

Predictor

nodes.py and pipeline.py that is created in api_pipeline will be responsible for saving the predictor. In node.py, we will creates a class called predictor (which has the predict method).

What is predictor? Predictor is an encapsulation of a model that allows it to be stored in memory (through a kedro catalog and saved in pickle format) and used with any input data in any environment. (source)

Here is the eample of the structure of the nodes.py file:

import pandas as pd

class MLPredictor:
def __init__(self, model):
self.model = model
def predict(self, args_API: pd.DataFrame):
df_args = args_API
prediction = self.model.predict(df_args)
return {“prediction”: prediction}
def save_predictor(model):
predictor = MLPredictor(model)
return predictor

The pipeline.py structure will not be too different from another pipeline.py that we’ve already created; here is an example of the pipeline.py file structure:

from kedro.pipeline import Pipeline, node
from .nodes import save_predictor
def create_pipeline(**kwargs):
return Pipeline(
[
node(
func=save_predictor,
name=”save_predictor”,
inputs=”model”, ### Replace with model’s name
outputs=”MLPredictor”,
)
]
)

Configuration files

The first configuration file we’ll look at is catalog.yml, which you’ve probably seen before. This catalog will be relevant to preserving the predictor and will be structured as follows:

MLPredictor:
type: pickle.PickleDataSet
filepath: data/09_predictor/MLPredictor.pickle
versioned: true

Aside from that, we have the api.yml file, which has the information required to establish the actual API. Its default file is structured as follows:

routes:
model_API:
# Catalog reference to .pickle model
predictor: MLPredictor
# Inputs passed as a python dictionary
# allowed types int, float, str, bool and enum
parameters:
input_1:
# enum type requires options (numbers as options won’t work)
type: enum
options:
- some
- options
- here
input_2:
type: int
input_3:
type: float
input_4:
type: bool
input_5:
type: str

That’s all! The API is now ready to be constructed after these properties have been set.

Creating the API

To create the API, we just need to run this one-line coee in CLI:

kedro fast-api run

and that’s pretty much it! Now the API will be available for accessing and creating the first predictions. The access can be done at the following URL:

localhost:8000/model_API/

That’s it for “kedro-fast-api: A Kedro Plugin for Integrating Fastapi Into Your Kedro Project.” It is seen that making APIs is not too hard if we utilize this kedro-fast-api plugin, since it is already created for your Kedro project!

--

--

No responses yet