Getting Started with Creating Machine Learning API with FastAPI and Jupyter Notebook

While Learning about Machine learning we usually start by understanding about jupyter notebooks and then how to write code inside them. What these Youtube Tutorials dont cover is how can you actually utilize the power of your newly created Machine Learning Model for actual application.

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints. It is designed to be easy to use and productive, with great performance and scalability. In this article, we will explore how to use FastAPI to create a Machine Learning API from a model created with Jupyter Notebook, utilizing the Pickle framework in the process.

Pickle is a Python library that allows you to serialize and deserialize Python objects. In other words, it allows you to save Python objects to a file and load them back into memory at a later time. This is useful for saving machine learning models that can be reused in different applications or for sharing with others.

To get started, we will assume that you have already created a machine learning model in Jupyter Notebook and saved it to a file using the Pickle library. If you have not done this yet, you can follow these steps:

  • Train a machine learning model in Jupyter Notebook using any machine learning library such as scikit-learn, TensorFlow, or PyTorch.
  • Save the trained model to a file using the Pickle library. For example, you can use the following code to save a scikit-learn model:

    import pickle
    # train a scikit-learn model
    model = ...
    # save the model to a file
    with open('model.pkl', 'wb') as f:
    pickle.dump(model, f)

    Once you have a saved model file, you can use FastAPI to create an API that can serve predictions based on the model. Here's how:

  • Install FastAPI and Uvicorn:

    pip install fastapi uvicorn

    Create a new Python file called `app.py` and add the following code:

  • Install FastAPI and Uvicorn:

    import pickle
    from fastapi import FastAPI
    # load the model from the file
    with open('model.pkl', 'rb') as f:
    model = pickle.load(f)
    # create a FastAPI instance
    app = FastAPI()
    # define a predict endpoint
    @app.post("/predict")
    async def predict(data: dict):
    # convert the data to the format expected by the model
    # for example, if the model expects a numpy array, you can use:
    # data_array = np.array(list(data.values())).reshape(1, -1)
    # make a prediction using the model
    prediction = model.predict(data_array)
    # return the prediction
    return {"prediction": prediction.tolist()}

  • Start the API using Uvicorn::

    uvicorn app:app --reload

    This will start a development server that listens for incoming requests on `http://localhost:8000`.

  • Test the API using a tool such as `curl` or Postman. For example, you can send a POST request with JSON data to the `/predict` endpoint:

    curl -X POST \
    http://localhost:8000/predict \
    -H 'Content-Type: application/json' \
    -d '{
    "feature1": 1.2,
    "feature2": 3.4,
    "feature3": 5.6
    }'

    This should return a JSON response with the prediction:

    {
    "prediction": [0.5]
    }

    In this example, we assumed that the model expects a dictionary of features as input and returns a single prediction as output. However, you may need to modify the code to fit the specific requirements of your model.

    In conclusion, FastAPI is a powerful and easy-to-use web framework that allows you to create machine learning APIs from models created in Jupyter Notebook. By utilizing the Pickle library, you can easily load and serve the trained models. With FastAPI, you can create endpoints that receive input data and return predictions, which makes it simple to build scalable and high-performance APIs for your machine learning models.