Model Deployment -Deploy a Keras Model as a Python Web Application

Source Node: 1068819

This article was published as a part of the Data Science Blogathon

Introduction

Building a cool machine learning project is one thing, and another when you need other people to see it too. Sure, you can put the entire project on GitHub, but how will your grandparents figure out what you’ve done? No, we need to deploy our deep learning model as a web application that anyone in the world can access.

In this article, I am going to teach you how to create a web application that uses a trained Keras RNN(Recurrent Neural Network) in the form of input and allows the creation of new patent annotations to the users. In general, you can take it as, we will set up an initial sequence, and it outputs a brand new patent annotation that can be viewed in a browser!

Typically, data scientists develop models and front-end developers show them to the world. In this project, we’ll have to play both roles and dive into web development.

This project will require combining several tools:

  • Flask: for building a basic Python web application.

  • Keras: Deploying an already trained recurrent neural network

  • Templates from the Jinja template engine ;

  • HTML and CSS for creating web pages.

We will end up with a web application that will allow users to create entirely new patent annotations using a trained recurrent neural network.

final web application | model deployment

Image 1

Model Deployment – The Approach

The only goal was the deployment of web applications as soon as possible. Flask is chosen for the same, as it allows writing applications in Python. Overall, this project follows my design principles: create a prototype that works quickly and then just repeating the same to improve the product.

Basic Flask web application

We can use the following code to create our own application,:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "

Not Much Going On Here


app.run(host=’0.0.0.0′, port=50000)

You can view your own application at localhost: 50000, after running this code. Of course, we need something more, so we’ll use a more complex function that basically does the same thing: it processes requests from your browser and serves some content as HTML.

User input form 

After reaching the index page of the application the user can see a form with three options:

  1. The input of the initial sequence for RNN or its random generation.

  2. Choice of “variety” of RNN forecasts.

  3. Choice of the number of words at the exit from the RNN.

To create a Python form, we use web forms. You will see the code for creating it below:

from wtforms import (Form, TextField, validators, SubmitField, DecimalField, IntegerField)
class ReusableForm(Form):

# Starting seed

 seed = TextField("Enter 'random':", validators=[ validators.InputRequired()]) # Diversity of predictions diversity = DecimalField('Enter diversity:', default=0.8, validators=[validators.InputRequired(), validators.NumberRange(min=0.5, max=5.0, message='Div must be in 0.5 and 5. range')])

# Number of words

 words = IntegerField(' no. of words to generate:', default=50, validators=[validators.InputRequired(), validators.NumberRange(min=10, max=100, message='Number of words must be between 10 and 100')])

# Submit button

 submit = SubmitField("Enter")

After running it we are able to design a form that is shown below:

Input form | model deployment

Image 2

Validation in the code is needed to make sure that the user enters the correct information. For example, we check that all fields are filled and that the diversity value is between 0.5 and 5. These conditions must be met to start working.

validation |

Image 3

An error occurred while checking the correctness of the entered data.

Templates for Model Deployment

A template is a file with a ready-made “skeleton” that you just need to fill in a certain way. For a Flask web application, we can use the Jinja templating library to translate Python code into an HTML document. For example, in our main function, we will submit the content of the form to a template named index.html.

from flask import render_template
# Home page
@app.route("/", methods=['GET', 'POST'])
def home(): """Home page of app with form""" # Create form form = ReusableForm(request.form) # Send template information to index.html
return render_template('index.html', form=form)

When the user navigates to the home page, our application will provide index.html input from the form. A template is a complete HTML structure in which we refer to Python variables using syntax {{variable}}.

<!DOCTYPE html>
<html>
<head> <title>RNN Patent Writing</title> <link rel="stylesheet" href="/static/css/main.css"> <link rel="shortcut icon" href="/static/images/lstm.ico">
</head>
<body> <div class="container"> <h1> <center> Patent Abstracts with RNN</center> </h1> {% block content %} {% for message in form.seed.errors %} <div class="flash">{{ message }}</div> {% endfor %} {% for message in form.diversity.errors %} <div class="flash">{{ message }}</div> {% endfor %} {% for message in form.words.errors %} <div class="flash">{{ message }}</div> {% endfor %} <form method=post> {{ form.seed.label }} {{ form.seed }} {{ form.diversity.label }} {{ form.diversity }} {{ form.words.label }} {{ form.words }} {{ form.submit }} </form> {% endblock %} </div>
</body>
</html>

When an error occurs in the form (input data that cannot be validated), an error message will flash. Apart from that, the form will be displayed as in the file above.

If the user enters all the information correctly and finally clicks on submit, the input is redirected to the appropriate function to make more appropriate predictions using the trained RNN.

from flask import request
# User defined utility functions
from utils import generate_random_start, generate_from_seed

# Home page

@app.route("/", methods=['GET', 'POST'])
def home(): """Home page of app with form""" # Create form form = ReusableForm(request.form)
if request.method == 'POST' and form.validate():

# Extract information

 seed = request.form['seed'] diversity = float(request.form['diversity']) words = int(request.form['words'])

# Generate a random sequence

if seed == 'random':
return render_template('random.html', input=generate_random_start(model=model, graph=graph, new_words=words, diversity=diversity))
else:
return render_template('seeded.html', input=generate_from_seed(model=model, graph=graph, seed=seed, new_words=words, diversity=diversity)) # Send template information to index.html
return render_template('index.html', form=form)

Now when the user clicks on the submit button and the entered information is absolutely correct, on the basis of input, the input is sent to generate_from_seed. The trained Keras model is used by these functions for creating a new patent with out-built diversity and parameters num_words. The output of this function is sent randomly to templates random.html as it is displayed in the form of a web page.

Making Predictions with a Keras Pretrained Model

The parameter model contains a pre-trained Keras model, which is loaded as follows:

from keras.models import load_model
import tensorflow as tf
def load_keras_model(): """Load in the pre-trained model"""
global model model = load_model('../models/train-embeddings-rnn.h5') # Required for model to work
global graph graph = tf.get_default_graph()
load_keras_model()

The output of both the functions is a string in Python with formatted HTML. This string is sent to another template, which will be rendered as a web page. For example, it generate_random_startreturns formatted HTML, which is sent to random.html:

<!DOCTYPE html>
<html>
<header> <title>Random Starting Abstract </title> <link rel="stylesheet" href="/static/css/main.css"> <link rel="shortcut icon" href="/static/images/lstm.ico"> <ul> <li><a href="/">Home</a></li> </ul>
</header>
<body> <div class="container"> {% block content %} {{input|safe}} {% endblock %} </div>
</body>

Here we again use the templating engine Jinjato to display the formatted HTML file. Since the Python string is already translated to HTML, all we need to do to display it is use {{input|safe}}(where input is a Python variable).

Result of work

The function generate_random_start takes a random patent annotation as an input sequence and makes predictions based on it. It then shows the input sequence, the RNN-generated, and the actual output:

Result

Image 4

Output based on a random input sequence.

The function generate_from_seed takes a user-supplied initial sequence and makes predictions based on it. The output looks like this:

predictions

Image 5

Output based on a custom input sequence.

Despite the fact that the results do not always form logical sequences, they nevertheless show that the neural network is familiar with the basics of the English language. She was trained to predict every next word based on the previous 50, and thus select a convincing annotation. Depending on the parameter, the diversity output can be completely random or cyclical.

Model Deployment – The Application Launch

To run the app on your PC on your own, all you need to do is download the repository, just navigate towards the directory where the actual deployment takes place, and enter into run_keras_server.py. The availability of applications is at localhost:10000.

Depending on how your home Wi-Fi is configured, you should be able to access the app from any computer on the network using your IP address.

The web application                                                                                                     Image 6

Conclusion

In this article, we have learned how to deploy a pre-trained Keras deep learning model as a web application. This required the amalgamation of a number of different technologies, including recurrent neural networks, web applications, templates, HTML, CSS, and of course Python.

Even though this is an easy and beginner-friendly application, it displays that you can build web apps using deep learning with relatively less effort. Few people would say they were able to deploy the deep learning model as a web application, but if you follow the instructions in this article, you can become one of them!

Image 1 – https://www.google.com/url?sa=i&url=https%3A%2F%2Ftowardsdatascience.com%2Fdeploying-a-python-web-app-on-aws-57ed772b2319&psig=AOvVaw3SnrIIWYu5b4vzdh0ZjaTu&ust=1630662259539000&source=images&cd=vfe&ved=2ahUKEwiXvoy3gODyAhULA7cAHRkrBsgQjRx6BAgAEAk

Image 2 – https://miro.medium.com/max/2000/1*9b1B9ByHGnhemHFQDjze8Q.png

Image 3 –  https://miro.medium.com/max/2000/1*9b1B9ByHGnhemHFQDjze8Q.png

Image 4 – https://miro.medium.com/max/2000/1*9b1B9ByHGnhemHFQDjze8Q.png

Image 5 – https://miro.medium.com/max/2000/1*9b1B9ByHGnhemHFQDjze8Q.png

Image 6 – https://miro.medium.com/max/2000/1*9b1B9ByHGnhemHFQDjze8Q.png

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Source: https://www.analyticsvidhya.com/blog/2021/09/model-deployment-keras-model-as-a-python-web-application/

Time Stamp:

More from Analytics Vidhya