How to Build A Machine Learning API Using Flask



Original Source Here

Implementation

Now you understand the basic concept of REST API. Let’s get into the implementation using Flask. There are several processes that we will cover:

  1. Importing libraries
  2. Load the machine learning model
  3. Build functions to preprocess and to predict the image
  4. Initialize the flask object
  5. Set the route and the function that returns something to the user’s browser
  6. Run and test the API

Importing libraries

The first step is to load the libraries. The libraries that we will import are TensorFlow, Flask, Pillow, and other supporting libraries. If those libraries are not installed, you can install them by using the pip command.

Here is the code to import libraries:

Load the model

After you load the libraries, the next step is to load the machine learning model. In this case, I will use my image classifier model, which I’ve already pre-trained before using TensorFlow. The model will predict whether the image contains food or not.

Here is the code to load the model:

Side Note:
If you train the model on GPU and want to run them on CPU, it’s okay. TensorFlow supports this, and you don’t need to tweak the model at all.

Build functions to preprocess and to predict the image

After we load the model, the next step is to create functions to process the image. Because we will get the image from the request by a client, we will use io.BytesIO function to load the image into our Python code. After that, we only need to call the .predict function from our model to predict the result.

Here is the code to preprocess and to predict the image:

Initialize the flask object

Right after you build the functions, now let’s initialize the Flask object. Also, we set the name on the Flask object parameter. This step is to make sure that the python script is read for running a web application.

Here is the command for doing that:

Set the route and the function

Now let’s set the route and the function that we want to run. A route is a way to access certain functionality on the web. When we access the route, it will run a function and returns the message to the user.

Each route has different ways to interact based on their HyperText Transfer Protocol (HTTP) method. There are several HTTP methods that we can use.

The first one is the GET method, where we only access the route without sending any data. The second one is the POST method. In this method, we can access the route and also send the data to it.

There are also routes like the PUT, PATCH, and DELETE method. But we will not talk about it further. We will use only the GET and POST method for this time.

Now let’s create two routes. The first one is the ‘/’ route path. The route will return the welcome message of the API. Therefore, we will give this route a GET method.

The second one is the ‘/predict’ route path. This route will return the prediction result from an image. We will use the POST method for sending the image into the route. Also, we need to set the key, so the app can know where the image is. We will use the ‘file’ key as the identifier of our image file.

Here is the code for setting up routes and their functions:

Run the API

After we build functions and their routes, now let’s run the API. But there is a final step before we can run the API. We need to add the .route method into our code like this:

Now go to your terminal, and run one of this command (make sure that you are on the same path with the API file):

flask runORpython app.py

If you run the command, it will return a result like this:

As you can see from above, it shows several logs from TensorFlow. Don’t worry about it. It is just a warning that we can ignore. As long as you can see the ‘Running on’ message, it means that your API is running now.

Now let’s access the API by address http://127.0.0.1:5000/, where 127.0.0.1 is your local address, and 5000 is the port number to access the API. If it is a success, it will show like this on the web:

Great! Our API starts perfectly. Let’s try the API if it can predict an image of whether it contains food or not. We can use an application called Postman to test the API that we have created before. You can download it here.

Here is the preview of the result,

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot



via WordPress https://ramseyelbasheer.io/2021/06/01/how-to-build-a-machine-learning-api-using-flask/

Popular posts from this blog

I’m Sorry! Evernote Has A New ‘Home’ Now

Jensen Huang: Racism is one flywheel we must stop

5 Best Machine Learning Books for ML Beginners