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:
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:
pip install fastapi uvicorn
Create a new Python file called `app.py` and add the following code:
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()}
uvicorn app:app --reload
This will start a development server that listens for incoming requests on `http://localhost:8000`.
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.