
About
In this article, I’ll attempt to demonstrate how you can set up a quick Flask API to serve your ML model predictions.
Once you have created and trained your model you’re going to want to be able to interact with it. While it’s possible to do this manually in a jupyter notebook, you will most likely want to automate your predictions.
I’ve created a dashboard using React.js which enables a user to configure the features of a game, which we can then send to an API in order to receive a prediction.
Setting Up
Go ahead and start up your editor of choice. I’m using VS Code, which is free and can be downloaded here. It’s pretty intuitive, and the first time you start it up and start working on a python file it will prompt you to install some modules that help to make creating python files a breeze.
Here is a great article about setting up VS Code for python development:
Creating Flask Server
Now we’re ready to write some code.
Go ahead and create a new folder to work in, and create a new file for the Flask server and one for the ‘PredictGame’ route:

Let’s go ahead and add the basic functionality for our Flask server.
First we load the required flask API’s for setting up a REST server.
Then import the provider class were about to implement that’s going to be in charge of handling the prediction request.
Finally, we tell the compiler to start the server.
Route Handler
Now we a way to interact with the requests we’re going to receive, and a way to send back the correct information.
Let’s go ahead and create a route handler that will contain all of our logic for receiving information about a game, and to send back the correct predictions.
First off we import the required libraries. We import ‘reqparse’ and Resource from Flask.
We will use ‘reqparse’ to make handling request parameters as easy as using a dictionary.
‘Resource’ will allow us to inherit a super class that will let us implement the different HTTP methods such as ‘get’, ‘delete’, ‘put’, ‘post’.
This makes it easy for us since all we need to do is implement the functionality inside a ‘get’ method.
Next, we import ‘xgboost’ because we will have to create a fresh model that we will load our pre-trained weights into.
We import numpy because the model expects the data to be in a certain shape and to be numpy array type.
Finally, we import json so we can convert the game argument from a json object into a python list.
In our constructor we set up the argument parser and initialize the model with the saved weights.
We then create our ‘get’ method which just parses the game object from the request, then we format the game object into a format the model expects, and finally, we make the predictions and return them in a dictionary.
This is a super basic implementation and doesn’t have any error handling or type checking.
Finally, at the bottom we have a private helper method we used to transform the data into the correct format.
Starting the Server
First, open up a terminal in the same directory as the server.py file and run the file using:
$ python server.py
This will launch the Flask server, and you should see some text announcing that the server is running and the address you can find it at:

Now our server is running and we can interact with the API.
Using the API
Now that we have a server up and running, we need to test our work.
There are several ways to make HTTP requests though the most popular tool for testing API endpoints is definitely Postman.
Postman is a free app which is used to create and run tests using HTTP, and lets you see exactly what is being sent and what is being received when you make requests.
Go ahead and download Postman, which we can use to make http requests and see the raw responses.
Once its downloaded go ahead and open it up, you should see a screen similar to this:

Thankfully the Postman UI is very intuitive and its easy to become comfortable with where everything is located.
Enter the URL we want to test, in this case:
http://127.0.0.1:5000/game
Note, your address may be different depending on which port. Also, ‘127.0.0.1’ is also sometimes called ‘localhost’, so sending a request to the following address would basically be the same.
http://localhost:5000/game
Notice the /game at the end of the address, this is the route we set up in the server.py file.
Back in Postman, under Query Params, you should see a list of ‘key’ ‘value’ fields.
Typing our params into these fields will automatically populate our URI with the correct values and URI encodings for special characters like spaces.
Go ahead and create a ‘game’ key with the following value (I know its ridiculously long, each game prediction is made based off 84 different features):
[5.39279682138009,6.683608045460701,0.15919970376939438,3.55590999603135,1.1554737031359337,0.7067666437650326,1.1426873175695553,1.2569168628035827,-0.7803980111346107,-0.2962583460820777,-0.24055440492626987,-1.7136287338568992,-0.1651305355827784,-0.2036357303066871,-0.4540528313626752,-0.48914117436953486,-0.22271712167442767,-0.5282740359736868,-0.031858861469518406,-0.24937339127109132,-0.32994634379035076,-0.512297869820368,-0.7031963621472596,-0.07105583014744833,-0.29576266052192457,0.1870374024689484,-0.14092851934520698,-0.33523660382633563,-0.14365069955967955,-0.12421426326285712,-0.06650450595001171,-0.10261069449730548,-0.1123532348785659,3.27774725901111,-0.45335715532058596,-0.16592152231605703,1.5775085110873435,1.5048682557955273,1.9597419248177352,-0.48621831608959837,1.642609246782289,0.5213592693458309,-0.48290073165693087,-0.3244873199973241,-0.301429739232314,2.4736135100111123,-0.22133313058352583,3.8765749060205286,-0.23604923873792524,-0.2401213205987764,-0.27373953556863745,-0.35059866422759306,4.098403285704536,4.559516332813075,-0.28035794137016523,-0.2973715918552079,-0.30799315147252165,3.713847136118922,3.139731271607883,-0.2625780433542825,2.3218106291644776,-0.30943922402842255,2.6890016668398484,-0.2316238571204376,3.884818704555654,-0.35758626244129893,-0.31778939411173085,-0.24428267916420615,-0.43130531370481745,-0.2341397130078093,-0.22087023503836106,-0.3216777806746954,-0.21651103657152967,-0.28636063937475126,-0.21776380127418174,-0.2768681688204387,-0.7287651618291123,-0.355929439908863,-0.24655521427098337,-0.22667679255666365,-0.2785523148360368,-0.2524454873949673]
Go ahead and press ‘Send’ and you should get a response back that looks like:

Perfect! Our server takes in a stringified list of floats representing the features of an individual game, and returns a number which is the predicted ranking of appearance on the Steam store.
Conclusion
In this article, we went through the entire process of creating a HTTP server that can make accept a request that contains a list of game features, and then responds with a number representing the predicted ranking on the Steam store page.
We made the server super basic and didn’t deal with any error handling or argument validation. There are a ton of resources for how to do that, and possibly could be a future article.
Thanks for reading, if you have any thoughts or questions please let me know, I would love the chance to chat about anything technology related.