Building a Machine Learning API with Flask

Nishad Ahamed
6 min readMay 13, 2023

Machine learning models are indispensable for tackling difficult problems and creating accurate forecasts in today's data-driven environment. These models can be deployed as APIs, allowing developers to incorporate them into various applications to utilize their predictive skills. Here, we’ll show you how to back up a trained machine-learning model, build a Flask API to service the model and put it through its paces with Postman. As an illustration of the usefulness of this method, we will utilize a pre-trained model that predicts house prices from input features. This tutorial will teach you how to use Flask with Postman to store, deploy, and test your machine-learning models.

Prerequisite

Before building a machine learning API with Flask, it’s nice to grasp key ideas and skills. If you know about machine learning, Python programming, and RESTful APIs, the course will be easier to use. With this information, you’ll need several programs, libraries, and other tools. These include the Python computer language, the Flask web framework for building the API, machine learning libraries like scikit-learn or TensorFlow (depending on your model), and Postman for testing the API endpoints. With this background information and tools, you’re ready to start making a powerful machine-learning API.

For this tutorial, you’ll need to have the following software, libraries, and tools installed on your system:

  1. Python: The primary programming language for our project
  2. Flask: A lightweight web framework for building the API
  3. Scikit-learn: A popular machine learning library, which we’ll use to train our model on the Boston house price dataset
  4. Postman: A powerful API testing tool to verify the functionality of our Flask API

With these prerequisites, you’re ready to learn how to create a machine-learning API for predicting Boston house prices.

Saving the Machine Learning Model

In this lesson, we’ll use a simple linear regression model to determine how much a house in Boston will cost based on the features we give it. Beginners in machine learning often start with the Boston house price dataset because it shows how to work with real-world data and use regression methods to make predictions. The dataset has 506 examples with 13 features, such as the number of crimes, the average number of rooms per house, and the rate of property taxes. Our goal is to train a model that can use these factors to predict how much a house will cost correctly.

Training and Evaluating the Model

  1. Import necessary libraries and load the dataset
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.datasets import load_boston

boston = load_boston()

2. Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=42)

3. Create and train the linear regression model

model = LinearRegression()
model.fit(X_train, y_train)

4. Evaluate the model’s performance on the test set

y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("Mean squared error:", mse)

At this point, we have trained a Machine learning model and evaluated its performance. Next, we have to save that model in a format where we can load it into an API and use it to predict the prices when needed.

5. Saving the model as a .pkl file

A “pickle file,” often known as a “.pkl file,” stores Python objects in a binary format. Serializing machine learning models, data structures, and custom classes let them be saved to disk and reloaded without affecting their structure or state. Python simplifies object storage and transfer. Our guide saves the learned linear regression model as a .pkl file, which our Flask API may use to forecast without retraining.

import pickle
with open("boston_house_price_model.pkl", "wb") as f:
pickle.dump(model, f)

The trained linear regression model has been saved as “boston_house_price_model.pkl” and can be loaded and used in our Flask API to make predictions.

Creating the Flask API

As for now, we have trained and saved the model in a reusable format so we can start to build our API using that.

Install Flask and Numpy if you haven’t already.

pip install flask numpy

Create a new Python file, save as app.py file, and import the necessary libraries:

from flask import Flask, request, jsonify
import numpy as np
import pickle
  • Flask: Web framework for building APIs and web applications.
  • request: Access and retrieve data from incoming HTTP requests.
  • jsonify: Convert Python dictionary to JSON response.
  • numpy: Powerful library for numerical computations with support for multi-dimensional arrays.
  • pickle: Serialize and deserialize Python objects used for loading trained models.

Load the trained model using Pickle

# Load the trained model
with open('model.pkl', 'rb') as file:
model = pickle.load(file)

Ensure you have the trained model saved model.pkl in the same directory as your app.py file. Adjust the file path if necessary.

Define a route for the prediction endpoint

@app.route('/predict', methods=['POST'])
def predict():
# Get the input features from the request
features = request.json['features']

# Convert the features to a numpy array
features = np.array(features)

# Make predictions using the loaded model
predictions = model.predict(features)

# Return the predictions as a JSON response
return jsonify({'predictions': predictions.tolist()})
  • @app.route('/predict', methods=['POST']): This line defines a route for the '/predict' endpoint to handle POST requests. It specifies that the endpoint will only respond to POST requests.
  • def predict():: This line defines a function named 'predict' that will be executed when the '/predict' endpoint is called.
  • features = request.json['features']: This line retrieves the 'features' data from the JSON payload of the request. It assumes the request body contains a JSON object with a 'features' key.
  • features = np.array(features): This line converts the retrieved 'features' into a numpy array. It uses the numpy library to create a numerical array from the provided features.
  • predictions = model.predict(features): This line uses the 'model' object (the loaded trained model) to make predictions based on the input 'features'. It calls the 'predict' method of the model and assigns the resulting predictions to the 'predictions' variable.
  • return jsonify({'predictions': predictions.tolist()}): This line generates a JSON response using the ‘jsonify’ function. It transforms the predictions into a Python dictionary, wraps it in a JSON response object, and returns it as the API response. To maintain compliance with JSON serialization, the predictions are converted to a list using the ‘tolist()’ method.

Run the Flask application

if __name__ == '__main__':
app.run()

Save the app.py file and run it using the command given below

python app.py

Once the Flask application runs, send a POST request to http://localhost:5000/predict with the input features in the request body as JSON data. The predictions will be returned as a JSON response from the API.

Testing the Flask API

  1. Open Postman and create a new request.
  2. Set the request method to POST.
  3. Set the request URL to http://localhost:5000/predict (assuming the Flask API is running locally on port 5000).
  4. Go to the “Body” tab in Postman.
  5. Select the “raw” option and set the data type to JSON (application/json).
  6. In the request body, provide the input features as a JSON object.
{
"features": [0.00632, 18.0, 2.31, 0.0, 0.538, 6.575, 65.2, 4.0900, 1.0, 296.0, 15.3, 396.90, 4.98]
}

Conclusion

In conclusion, developers have an efficient platform for deploying and utilizing trained models when an API for machine learning is created with Flask and Postman is used for testing. The steps involved in storing a machine learning model, developing a Flask API to service the model, and testing the API’s operation with Postman have been covered in this tutorial. Incorporating machine learning into practical applications paves the way for more precise forecasting and the solution of more difficult issues. You can now create your own machine learning APIs and easily integrate them into your applications thanks to your familiarity with Python, ML, and RESTful APIs. You can make your machine learning models available to your consumers and provide them access to powerful prediction insights by leveraging the capabilities of Flask and Postman.

BECOME a WRITER at MLearning.ai

--

--

Nishad Ahamed

Hi, I am Nishad Ahamed, an IT undergrad. I am passionate about web development, Data Science, and Artificial intelligence.