Deploy Machine Learning Model for simple use. Part II
July 13, 2020
Deploy Machine Learning Model for simple use. Part II
4th week of blogging
Hai,Everyone π ,
Without further adieu let us get started.
we have already set up the flask app and HTML to get the input, now lets put the model to work.
To put our Model we will create a file called decision.py and import it in from decision import Emotion in our flask_app.py file.
To load model
import json
import keras
import numpy as np
import sklearn
from sklearn.preprocessing import LabelBinarizer
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.layers import Input, Embedding, SpatialDropout1D, LSTM
from tensorflow.keras.layers import GlobalAveragePooling1D, GlobalMaxPooling1D
from tensorflow.keras.layers import Bidirectional, Conv1D, Dense, concatenate
from tensorflow.keras.models import Model
import pickle
from tensorflow.keras.preprocessing.sequence import pad_sequences
import os
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
def token():
file = os.path.join(THIS_FOLDER, 'static/tokenizer.pickle')
infile = open(file,'rb')
tokenizer = pickle.load(infile)
infile.close()
return tokenizer
def encoder():
labels = ['Happy','Sad','Anger','Disgust','Surprise','Fear','Bad']
encoder = LabelBinarizer()
encoder.fit(labels)
return encoder
def model():
input_length = 428
input_dim = 203169
num_classes = 7
embedding_dim = 500
lstm_units = 128
lstm_dropout = 0.1
recurrent_dropout = 0.1
filters=64
kernel_size=3
input_layer = Input(shape=(input_length,))
output_layer = Embedding(
input_dim=input_dim,
output_dim=embedding_dim,
input_shape=(input_length,)
)(input_layer)
output_layer = Bidirectional(
LSTM(lstm_units, return_sequences=True,
dropout=lstm_dropout, recurrent_dropout=recurrent_dropout)
)(output_layer)
output_layer = Conv1D(filters, kernel_size=kernel_size, padding='valid',
kernel_initializer='glorot_uniform')(output_layer)
avg_pool = GlobalAveragePooling1D()(output_layer)
max_pool = GlobalMaxPooling1D()(output_layer)
output_layer = concatenate([avg_pool, max_pool])
output_layer = Dense(num_classes, activation='softmax')(output_layer)
model = Model(input_layer, output_layer)
file = os.path.join(THIS_FOLDER, 'static/model.h5')
model.load_weights(file)
return model
class Emotion:
def __init__(self):
self.model = model()
self.tokenizer = token()
self.encoder = encoder()
def test(self,text):
labels = ['Happy','Sad','Anger','Disgust','Surprise','Fear','Bad']
tokenized = self.tokenizer.texts_to_sequences([text])
pad_data = pad_sequences(tokenized,428)
pred = self.model.predict(pad_data)
print(pred,flush=True)
emotion = labels[np.argmax(pred)]
confidence = pred[0][np.argmax(pred)] * 100
return (emotion,confidence)
Here we design the Model using Keras, then load the weights from the saved .h5 file for easy access, we create a class for model Emotion, which contains a test method that tests the model with input after initializing model, tokenizer, encoder as the result we return emotion and confidence from the model, which is rendered with HTML for the user to view.
Deploying to Pythonanywhere
Host the script pythonanywhere by following a few simple steps:
Step 1. Sign up for a new account.
For now, letβs stick with the free account. Sign up and log in to your account. Choose the flask and whichever python version you want. I will be using Python 3.7. After creating the web app, you will get a URL that points to your flask endpoint. By default, Your endpoint looks something like this: [username].pythonanywhere.com
Step 2. Upload the files
Inside the default folder β /mysite/ you need to upload your complete folder. You can do it either using the files page on the website or using the bash console by using wget command to download your files. and enable Force HTTPS File Structure looks like this
/mysite/
|
+-- flask_app.py
|
+-- decision.py
|
+-- static/
| |
| +-- encoder.pickle
| |
| +-- lstm_model.h5
| |
| +-- tokenizer.pickle
+-- templates/
| |
| +-- index.html
Step 3. Reload the web app
Web Application is ready Now and can be used for predicting emotions of text.