Deploying Intent Classifier on Heroku

Het Pandya
Geek Culture
Published in
3 min readOct 24, 2020
Image by Author

Welcome to the sequel of my blog where I talked about training an Intent Classifier. So now that we have our intent classifier on trained.. next what? How do we use it. It must be deployed somewhere to be used, right? We shall choose Heroku as our platform to see the intent classifier live.

Before we begin, you can find all the code on my github repo if you wish to walk along with me.

Folder Structure

We shall keep the following folder structure for ease:

/your-folder
├───models
│ └───intents.h5
├───templates
│ └───index.html
├───utils
│ ├───classes.txt
│ ├───label_encoder.pkl
│ └───tokenizer.pkl
├───index.html
├───app.py
├───requirements.txt
└───Procfile

Step 1

As talked before, Heroku needs “requirements.txt” to install all the required dependencies. Add the following libraries in same:

tensorflow==1.5
h5py
scikit-learn
numpy
flask

Step 2

Now that we have our libraries installed, time to code the file where all the magic happens😉

Create a file called “app.py” and put the following code code:

import os
from flask import Flask, render_template
from flask import request

import pickle
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
import numpy as np
import tensorflow as tf

global graph

model = load_model('models/intents.h5')
graph = tf.get_default_graph()

with open('utils/tokenizer.pkl','rb') as file:
tokenizer = pickle.load(file)

with open('utils/label_encoder.pkl','rb') as file:
label_encoder = pickle.load(file)

class IntentClassifier:
def __init__(self,model,tokenizer,label_encoder):
self.classifier = model
self.tokenizer = tokenizer
self.label_encoder = label_encoder

def get_intent(self,text):
self.text = [text]
self.test_keras = self.tokenizer.texts_to_sequences(self.text)
self.test_keras_sequence = pad_sequences(self.test_keras, maxlen=16, padding='post')
with graph.as_default():
self.pred = self.classifier.predict(self.test_keras_sequence)
return self.label_encoder.inverse_transform(np.argmax(self.pred,1))[0]

app = Flask(__name__)

nlu = IntentClassifier(model,tokenizer,label_encoder)


@app.route('/', methods=['GET', 'POST'])
def index():

if request.method == 'POST':
form = request.form

result = []
sentence = form['sentence']
prediction = nlu.get_intent(sentence)

result.append(form['sentence'])
result.append(prediction)

return render_template("index.html",result = result)

return render_template("index.html")

if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)

This will create a parser class so as to classify the given intent based on the ones it has been trained on. Next, we created a flask server to receive the inputs and pass them on the to parser class.

Next, create a file called “index.html” and place it under the “templates” folder. Add the following code in the file:

<!DOCTYPE html>
<html>
<head>
<title>Intent Classifier</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" media="screen">
<style>
.container {
max-width: 1000px;
}
</style>
</head>
<body>
<div class="container">
<div class="row-sm-5 row-sm-offset-1">
<h4>Enter a sentence to test Intent Classifier</h4>
<form role="form" method='POST' action='/'>
<div class="form-group">
<input type="text" name="sentence" class="form-control" id="url-box" placeholder="Enter a sentence" style="max-width: 300px;" autofocus required>
</div>
<button type="submit" class="btn btn-default">Predict</button>
</form>
<br>
</div>


<div class="row-sm-5 row-sm-offset-1">
<p>Find the list of all classes <a href='https://github.com/horizons-ml/heroku-intent-classifier-deployment/blob/main/utils/classes.txt' target="_blank">here</a>.</p>
{% if result %}
<h4>Sentence : {{ result[0] }}</h4>
<h4>Predicted Intent : {{ result[1] }}</h4>
{% endif %}
</div>

</div>

</body>
</html>

Step 3

Create “Procfile” and add put the following code:

web: python app.py

Step 4

Make sure you have Heroku CLI and git installed. Once done, run following in your terminal to connect your machine with Heroku.:

heroku login

Next, go to this page and create a heroku application with your desired name. When you have successfully created your app, type the following commands to deploy your app to Heroku:

git init
git add .
git commit -m 'initial commit'
git push heroku master

Our app has been deployed! Time to see it in action.

Enter the following address in your browser window:

https://your_app_name.herokuapp.com

Your should see something like:

Deployed Intent Classifier

Let’s give it a test sentence. You can find the list of classes here for reference to test the model.

Intent Classifier Prediction

Yaaayy! Our classifier works! I have deployed my classifier here if you wish to give it a try.

That’s it for now. Thank you for reading😄

--

--

Het Pandya
Geek Culture

Hi, I am a machine learning enthusiast, passionate about counting stars under the sky of Deep Learning and Machine Learning.