The author | Will compile Koehrsen | Flin source | towardsdatascience

Building a great machine learning project is one thing, but at the end of the day, you want others to see your hard work. Sure, you can put the entire project on GitHub, but how do you show it to your grandparents? What we want is to deploy the deep learning model as a Web application accessible to anyone in the world.

In this article, we’ll see how to write a Web application that uses a trained Keras recursive neural network and allows users to generate new patent abstractions. This project is based on recursive neural networks, but it is not necessary to know how to create RNNS.

Now we think of it as a black box: we operate in a starting order, and it outputs a brand new patent digest that can be displayed in a browser!

Traditionally, data scientists develop models and front-end engineers present them to the world. In this project, we had to play two roles and dive into Web development (albeit almost entirely in Python).

The project needed to bring together a number of themes:

  • Flask: Create a basic Web application in Python

  • Keras: Deploy a well-trained recursive neural network

  • Create templates using the Jinja template library

  • HTML and CSS for writing web pages

  • Flask:flask.pocoo.org/

  • Keras: Keras. IO /

  • HTML:www.w3schools.com/html/

  • CSS:www.w3schools.com/html/html_c…

The end result is a network application that allows users to generate brand new patent abstractions using trained recurrent neural networks:

The full code for the project can be found on GitHub.

  • Github.com/WillKoehrse…

methods

The goal is to get the Web application up and running as quickly as possible. For this, I chose Flask, which allows us to write applications in Python. I don’t like messing with styles (this is clearly shown), so almost all CSS is copy and paste.

This article by the Keras team (blog.keras. IO/build-a -… Helpful for the basics, this article is also a useful guide.

Overall, the project follows my design principles: get prototypes up and running quickly — copy and paste as needed, then iterate to make a better product.

Basic Web application with Flask

The fastest way to build a Web application in Python is to use Flask. To make our own application, we can use the following:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello() :
    return "<h1>Not Much Going On Here</h1>"

app.run(host='0.0.0.0', port=50000)
Copy the code

If you copy and paste this code and run it, you can view your own Web application at localhost:50000. Of course, we want to do more than that, so we’ll use slightly more complex functionality that essentially does the same thing: handle requests from the browser and provide some content in HTML form. For our home page, we want to display a form to the user to enter some details.

User input table

When the user arrives at the application’s home page, we’ll show them a form with three parameters for you to choose from:

  1. Enter the starting sequence of RNN or random selection
  2. Select the diversity of RNN predictions
  3. Select the number of words output by RNN

To build forms in Python, we will use WtForms. The code to create the form is:

  • Wtforms: wtforms. Readthedocs. IO /
from wtforms import (Form, TextField, validators, SubmitField, 
DecimalField, IntegerField)

class ReusableForm(Form) :
    """User entry form for entering specifics for generation"""
    # Starting seed
    seed = TextField("Enter a seed string or '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='Diversity must be between 0.5 and 5.')])
    # Number of words
    words = IntegerField('Enter number 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")
Copy the code

This will create the form shown below (style from main.css) :

This Validator code ensures that the user enters the correct information. For example, we check that all boxes are filled in and that their diversity is between 0.5 and 5. These conditions must be met to accept the form.

The way we at Flask actually serve forms is by using templates.

The template

A template is a document that contains the basic framework that we need to fill in the details. For Flask Web applications, we can use the Jinja template library to pass Python code to HTML documents. For example, in the main function, we will send the contents of the form to a file named index.html.

  • Jinja template library: jinja.pocoo.org/
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)

Copy the code

When the user arrives at the home page, our application will provide the details on the index.html table. The template is a simple HTML framework in which we reference Python variables using the {{variable}} syntax.

<! DOCTYPEhtml>
<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>Writing Novel Patent Abstracts with Recurrent Neural Networks</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>
Copy the code

For each error in the form (those entries that cannot be verified), an error will flash. In addition, this file will display the above form.

When the user enters the information and clicks Submit (POST request), if the information is correct, we want to transfer the input to the appropriate function to make the prediction using the trained RNN. This means modifying home().

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)

    # On form entry and all conditions met
    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))
        # Generate starting from a seed sequence
        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)
Copy the code

Now, when the user clicks Submit and the information is correct, the input will be sent to either Generate_random_start or Generate_from_seed, depending on the input. These functions use trained Keras models to generate novel patents with user-specified diversity and NUM_words. The output of these functions in turn is sent to one of the templates random.html or seeded.html as a web page.

Predictions were made using pre-trained Keras models

The model parameters are trained Keras models, which are 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()
Copy the code

Tf.get_default_graph () is a solution based on this point.

I won’t show you the whole util function (here is the code), what you need to understand is that they use the trained Keras model and parameters and make predictions for the new patent digest.

These functions all return Python strings with formatted HTML. The string is sent to another template to render as a web page. For example, generate_random_start returns HTML and returns random.html:

<! DOCTYPEhtml>
<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>

</html>
Copy the code

Here we use the Jinja template engine again to display the formatted HTML. Because of the Python string have been formatted as HTML, we have to do is to use {{input | safe}} (of which the input is a Python variable) to display it. We can then style the page in main.css, just like any other HTML template.

The output

Generate_random_start Selects a random patent digest as the starting sequence and makes predictions based on that digest. It then displays the start order, the output generated by RNN and the actual output:

The function generate_FROm_seed takes the user-supplied starting sequence and then builds it with trained RNN. The output is as follows:

Although the results are not always correct, they do suggest that recursive neural networks have learned the basics of English. Trained to predict the next word in the first 50 words and master how to write a persuasive patent digest!

Depending on the variety of predictions, the output may be completely random or cyclic.

Run the application

To run the application yourself, all you need to do is download the repository, navigate to the Deployment directory and type python run_keras_server.py. This will immediately make the Web application available at localhost: 10000.

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

The next step

Web applications running on your PC are perfect for sharing with friends and family. I definitely don’t recommend opening this up to everyone on your home network! To do this, we want to set up the application on an AWS EC2 instance and make it available to the world (to be released later).

To improve the application, we can change the styling (via main.css) and possibly add more options, such as selecting the capabilities of a pre-trained network. The great thing about individual projects is that you can expand them as needed. If you want to use the application, download the code and get started.

  • Download code: github.com/WillKoehrse…

conclusion

In this article, we saw how to deploy a well-trained Keras deep learning model as a Web application. This requires a combination of different technologies, including recursive neural networks, Web applications, templates, HTML, CSS, and of course Python.

While this is just a basic application, it shows that you can start using deep learning to build Web applications without too much effort.

submit = SubmitField("Enter")
Copy the code

Load in the training model.

The original link: towardsdatascience.com/stop-worryi…

Welcome to panchuangai blog: panchuang.net/

Sklearn123.com/

Welcome to docs.panchuang.net/