Demystifying Curl for Testing Deep Learning Models in Flask

This article assumes y’all have a basic idea about flask, python and Deep learning. Here I’ll mostly focus on how to make a request to a flask app through a Curl from the command line rather than making a UI and getting outputs there. The flask app I’ll be using in this article contains two pre-trained image classification models and I’ll make y’all understand the concept by making a Curl for this app and particularly for each model that our app contains. So I’ll start with a brief introduction about Curl.

What is cURL?

cURL, often just “curl,” is a free command line tool. It uses URL syntax to transfer data to and from servers. curl is widely used because of its ability to be flexible and complete complex tasks. For example, you can use curl for things like user authentication, HTTP post, SSL connections, proxy support, FTP uploads, and more! You can also do simple things with curl, such as download web pages and web images. It makes the requests, it gets the data, it sends data and it retrieves the information.

We’ll talk more about how to make a Curl, but first let’s the discuss the flask app around which we’ll make the Curl.

Brief of Flask app with Code

First, make a folder named flaskbag or whatever you like to name it and in this folder, we will make a new virtual environment in python3 using the following commands:

To activate this environment, use the following command:

After activating the virtual environment install these python3 framework requirements using pip3:

Now, in the same folder(i.e. flaskbag) in which we created the virtual environment and put these python files in it.

This main.py will contain 2 functions for vgg16 and vgg19 pre-trained from keras model-zoo and these functions will also be able to do the pre-processing required on the image before doing the predictions and will return the predicted label(label[1]) and percentage of accuracy for the prediction(label[2]).

Next is the app.py file.

  • First we imported the Flask class. An instance of this class will be our WSGI application.
  • Next we create an instance of this class. The first argument is the name of the application’s module or package. If you are using a single module (as in this example), you should use __name__ because depending on if it’s started as application or imported as module the name will be different ('__main__' versus the actual import name). This is needed so that Flask knows where to look for templates, static files, and so on.

Now this index.py contains 2 functions predict and prediction for vgg16 and vgg19 respectively. These functions also post the image/file on the server, run the predictions and jsonify the output label and accuracy percentage.

jsonify would be useful when you are building an API someone would query and expect json in return. The jsonify() function in flask returns a flask.Response() object that already has the appropriate content-type header 'application/json' for use with json responses. It is important to dump information in json while dealing with curl.

After putting these 3 code files in one directory(i.e. flaskbag), go to terminal and activate the virtual environment(venv) that we created above and run the index.py using the following command:

Now, we will perform predictions for both vgg16 and vgg19 by making the cURL for each of them respectively.

But, before doing that we will put an image in the UPLOAD_FOLDER (uploads) in the parent folder (flaskbag) on which we will run the predictions, for example this is the image:

elephant.jpg (Tusker)

Again if we look into the index.py file, predict function is for performing predictions on vgg16 and prediction function is for vgg19, now open a new terminal and try running this cURL for getting predictions on an image.

For vgg19
curl -X POST -H "Content-Type: multipart/form-data" http://127.0.0.1:5000/prediction -F "file=@uploads/elephant.jpg"

The HTTP POST method is used to create a resource on the server. In simple language, it sends data to the server. To send a curl POST request we use the option -X POST.

The type of the body of the request is indicated by the Content-Type header. This Content-Type can be of multiple types depending upon the data (if data is binary or not), but in this article we will only discuss about multipart/form-data.

multipart/form-data: each value is sent as a block of data ("body part"), with a user agent-defined delimiter ("boundary") separating each part. The keys are given in the Content-Disposition header of each part.

http://127.0.0.1:5000/ This consists of the localhost(http://127.0.0.1) and port number (5000). localhost:5000 is accessing localhost on the port 5000. For example if you access it from a browser, when you request localhost:5000, the browser will go to the web server on the localhost, but on port 5000, this means that if your web server is setup to work on port 5000 or one of it’s virtual hosts are setup to work on port 5000 you will get a response, otherwise you will get page not found error.

"file=@(Directory of the image)" : This arguments takes the directory of the image on which we are doing the predictions.

Here is a output Screenshot of both the curls mentioned above.

Outputs of both the curl.

So, this is how we make a curl for flask app consisting of various deep learning models. You can understand the above curls and make your own curl request while experimenting with some other REST API. This is it for this article and will just conclude with one more meme.

Data Scientist @ QpiAI

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store