Recently I built a deep learning model for my company predicting whether the user buys a car or not if yes then which car with good accuracy. Gave
demo to CEO & business team everyone got excited about it. We did testing also and it gave promising results.

Everyone pumped up and asked me to share API for the model so that we can spend our company focus on the probable buyers and also can plan to give a better consumer experience.

First, we used the Tornado framework to deploy our deep learning model. It used to load the model every time it gets API hit due to that response time is
very slow. We are new python so we did bit googling but didn’t find any solution. Then I saw Flask framework easy to use and we can declare a global variable and can load only once which can be reused whenever we want.

Let me share steps to you guys how to do it & how simple it is.

I am assuming you built your deep learning model already if not please refer my previous article to learn how to build deep learning model.

Step3: Extracting parameters which required for prediction(male, book1, book2, book3, book4, book5, isMarried, isNoble, numDeadRelations, boolDeadRelations, isPopular, popularity) of a character and appending them in the array.

Step4: Jsonifying the response object. Then adding header parameters for supporting cross-origin. Usually, we don’t deploy API in the same domain where we need to use so cross-origin support must at that time that’s why these header parameters required.

First, we are taking the name of the character then the rest of the parameters through getParameters method.

Then reshaped them to ndarray which we can use as input for our model.

Then we are finding the prediction through our model.

Then we are coming to conclusion whether the character is alive or dead.

Then we are preparing response object using sendResponse function and returning it.

# if this is the main thread of execution first load the model and then start the server
if __name__ == "__main__":
print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))
init()
app.run(threaded=True)

Step6: This code is for just to make sure If this is the main thread of execution first load the model and then start the server