Build API for a machine learning model using FLASK

Nischal Madiraju
2 min readApr 26, 2020

--

Now that you have successfully built a machine learning model and want to put it to use in your application, just follow these steps to create an API endpoint for fetching the predictions of your machine learning model:

1. Save the model using the ‘pickle’ library:

You can later open this pickle file and call the function ‘predict’ [PP1] to get a prediction on the new data.

2. ‘pickle.dump’ has two parameters:

a. The first is the model (which is represented the ‘clf’ in the image).

b. The second is the file in which you are going to save the model.

3. Once the model is saved, we use the Python Flask framework to load the model and create an API.

4. Flask is a lightweight Web Server Gateway Interface web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.

5. In your application import the Flask framework and create an API route:

6. Import pickle along with Flask into the same and open the previously saved model file.

7. Now you can create a function in such a way that when you make a Post request to the API endpoint with the new data, the data is processed accordingly and passed to the ‘.predict’ function of the model loaded above.

8. Making a simple post request to this API will give you the prediction in the response which can be used in your application accordingly.

--

--

Nischal Madiraju
Nischal Madiraju

Written by Nischal Madiraju

Writes about Artificial intelligence, Machine Learning and Deep Learning. Pursuing Msc in Artificial Intelligence at the University of Groningnen

No responses yet