Developing a Web Application Using Deep Learning Models

Hello there,

Let’s see how we can develop a web application and host it for free to use and test a deep learning model that is trained with Keras.

Web Application with Python Flask

Flask is a microframework that can develop Python-based web applications.

First, let’s install Flask.

pip3 install Flask

Let’s create a new app.py python file and write the following python codes.

 

from flask import Flask
app = Flask(__name__)
@app.route("/")
def main():
    return "Hello Flask ;)"
if __name__ == "__main__":
    app.run()

 

With @ app.route (“/”), the function to be called for localhost: 5000 / address will be returned when a request is sent and the def main (): function will be returned.

Let’s run our app.py code that we created from the terminal.

python app.py

When we go to http:// localhost: 5000 / from our browser when we see Running on the Terminal running on http://127.0.0.1:5000/, we will see the string value we returned in main () function.

 

HTML Template with Flask

Create a new template and create an index.html script to create templates, and let’s write html codes.

Let us import render_template package in our app.py python file. We’re reorganizing the main () function to call our newly created index.html as follows.

 

Deep Learning Web Application Development

Let’s look at how we can run the deep learning model by developing our web application.

We will add the necessary Keras packages, our network, which we will use for image classification, the ResNet50 package and other packages required for pre-processing.

For image classification, let’s upload our trained model and weight files with ImageNet dataset.


Let’s define the predict () function, which is sent by the form post method, which is sent to the model after applying some pre-operations that bring the model to the appropriate input format, and the prediction result is taken and returned in json format as follows:

Finally start this service.

After running our application from the terminal, we post a sample image from another terminal to http://0.0.0.0:5000/predict.

For example, let’s try to classify the picture of the American parrot:

The picture we have sent as below is classified in the model and returns us to the json result.

Among the results, we see that 99% of the results are categorized as macaw.

Now, by using the API we have written, we select a picture file from the web interface and perform the classification.

To do this, add a form and file upload elements to our index.html page so that the user can upload images.

When the user selects the image file with the file upload element and click “Send” button, we send a POST request to our REST API, which calls the predict () function we defined in app.py python code with jquery ajax.

You can run source codes by downloading them to a computer that has the Keras library installed.

If you don’t have enough hardware for this application, or if you don’t have deep learning libraries installed, you can try online on Google Colab here.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *