Antony's Blog

Deploy Machine Learning Model for simple use.

June 29, 2020

Deploy Machine Learning Model for simple use.

3th week of blogging
Hai,Everyone 👋 ,
Machine learning models are no good lying in the Jupyter notebooks or loose python scripts, to help other people the model needs to be deployed in a way it’s usable by the general public and not just by the domain experts. For this, the model needs to embedded in API that other applications can communicate with the model.
So in this post, I am going to deploy the Emotion analysis model as an WebApp for Simple use. if you haven’t read the post don’t worry, I am going leave the specifics.

Things that have to be done are:

  1. Train the model
  2. Save the trained model as .h5 file
  3. Make the design of the Deep learning model and load weights from .h5 file
  4. Use flask to create Web-App which would encapsulate our trained model and enable it to receive inputs through GET requests and then return the output from the model.
  5. Upload the flask script along with the trained model on pythonanywhere
  6. Make requests to the hosted flask script through a website

As we already trained the model we will start with saving the model in .h5

model_json = model.to_json()
with open('/content/drive/My Drive/Emotion-Detection/lstm_model.json', 'w') as json_file:
    json_file.write(model_json)
model.save_weights('/content/drive/My Drive/Emotion-Detection/lstm_model.h5')

Code for saving the model in JSON format and saving weights in .h5 format.
We will be doing this as 2 parts :

  1. Flask setup
  2. Model embedding

Flask setup

Let’s first set up the flask server on the local host and later deploy it on pythonanywhere for free. Setting up flask-app over the localhost: Make sure the flask is installed.

pip install flask

File Structre looks like this

EmotionAI
 |
 +-- flask_app.py
 | 
 +-- decision.py
 | 
 +-- static
 | | 
 | +-- encoder.pickle
 | | 
 | +-- lstm_model.h5
 | | 
 | +-- tokenizer.pickle
 +-- templates
 | | 
 | +-- index.html

Create a file called flask_app.py in EmotionAI directory and copy the contents to it, and to run the flask server

python flask_app.py
 

	from flask import Flask, render_template, request
	from decision import Emotion
	 
	app = Flask(__name__)
	 
	@app.route("/", methods=['GET', 'POST'])
	def index():
	 emotion="Enter text to predict"
	 confidence="AI"
	 emot = Emotion()
	 print(emot,flush=True)
	 if request.method == 'POST':
	 text = request.form['text']
	 print(text,flush=True)
	 emotion,confidence = emot.test(text)
	 print(emotion,confidence)
	 return render_template("index.html", emotion=emotion, confidence=confidence)
	 else:
	 return render_template("index.html", emotion="Enter text to predict" , confidence="AI")
	 
	 
	if __name__ == '__main__':
	 app.run(debug=True) # disable in production
App Route

app.route decorator is used for specifying the flask app route over the web.“/” simply means the home that is “http://127.0.0.1:5000/”

Request

request.form[‘text’] gets the text from the HTML form in which the name is text. we give the text to the model ( emot.test(text) )to output emotion, the confidence. Emotion is the emotion of the text and confidence is the confidence with which the model predicts the emotion.

Render

we use render_template to render the output to the user, so that the user can view the result.

The following script starts the flask server on localhost and default port (5000) making the URL: http://127.0.0.1:5000/ Just paste http://127.0.0.1:5000/ on browser and press enter to see the server working.

    <div class="container-fluid home-main">
        <div class="row">
            <div class="col-12">
                <h1>Emot AI</h1>
                <h2><span class="blinker">(o.o)</span></h2>
                <button type="button" class="btn btn-default">View Code</button>
            </div>
        </div>
    </div>
    <div class="container-fluid home-content1">
        <div class="row">
            <div class="col-md-6 col-sm-12 content1-left">
                <form name="form_ai" method="POST">
                    <fieldset>
                        <div class="form-group">
                            <label for="text">Text: </label>
                            <input type="text" class="form-control" id="text" placeholder="text" name="text" required>
                        </div>
                        <br>
                        <button type="submit" class="btn btn-primary">Check</button>
                        <button type="reset" class="btn btn-danger">Clear</button>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>

HTML code for the Form. <input type="text" class="form-control" id="text" placeholder="text" name="text" required> take the input and gives it to the model, when submit button is clicked.

<div class="col-md-6 col-sm-12 content1-right">
                <h3>Predicted Emotion <span class="blinker">?</span></h3>
                <h4> {{ emotion }} </h4>
                <p>With a Confidence of {{ confidence }}</p>
</div>

The output is shown as above.