You have finally trained your first Machine Learning model. Congratulations! What’s next though? How would you unleash the power of your model outside of your laptop? In this tutorial, we help you take the first step towards deploying models. We will use flask to create apps and flasgger to create a beautiful UI.
Pre-requisite:
- Anaconda distribution of Python
Libraries required:
- If you do not have Anaconda installed, you can download it from here:
https://www.anaconda.com/distribution/ - Libraries to install:
-numpy
-pandas
-sklearn
-flask
-flasgger (version = 0.8.1)
Github repository for this tutorial – https://github.com/DhruvilKarani/MLapp_using_flask_and_flasgger
You can install libraries using Anaconda Prompt (use the search option on windows) by typing –pip install <name of the library>. For flasgger, use pip install flasgger==0.8.1
Of course, don’t include the <>
Building an ML model
Before we deploy any model, let’s first build one. For now, let’s build a simple model on a simple dataset so that we can spend more time on the deployment part. We use the Iris dataset from sklearn’s datasets. The required imports are given below.
The Iris dataset looks something like this –
The variable to be predicted, i.e., Species, has three categories – Sentosa, Virginica, Versicolour. Now, we build our model in just 6 lines of code
The model we build is saved as a pickle file. A pickle file saves any file into its binary form. Next time we want to use this model, we don’t have to train it again. We can merely load this pickle file.
The above command saves the model as a pickle file under the name model_pkl in the path specified (in this case – C:/Users….model.pkl). Also, make sure you have / and not \. You might also want to check once if the file is present in the folder. Once you have made sure the file exists, the next step is to use flask and flasgger to make a fantastic UI. Make a new Python script and import the following modules and read the pickle file.
Next, we create a Flask object and name it app. The argument to this Flask object is the special keyword __name__. To create an easy UI for this app, we use the Swagger module in the Flasgger library.
Now, we create two apps – One which accepts individual values for all 4 input values and the other which accepts a CSV file as inputs. Let’s create the first app
The first line @app.route(‘/predict’) specifies the part of the URL which runs this particular app. If you do not understand this as of now, don’t worry. Things get more evident as we use the app. The next thing we do is create a function, named predict_iris. Under this function, we have a long docstring. Swagger uses this string for creating a UI. It says that the app requires 4 parameters namely S_length, S_width, P_length, P_width. All of these input values are of the query type. Next, the app uses the GET method to accept these values which means that we need to enter the numbers by ourselves. Then we pass these values to our model in a 2 D numpy array and return the predictions. Two things here –
- Predictions[0] returns the element in the prediction numpy array
- We always output a string, never a numeric value to avoid errors.
Now we build the second app, the one that accepts a file. However, before the app, we create a file that has all four variable values for which we predict the output. In the Python console, type the following
Select any 2-4 rows at random, copy them and save them in a CSV file. We use this file to test our second app. The file would look something like this
Notice the changes here. The @app.route decorator has ‘/predict_file’ as one of its argument. The docstring under our new function predict_iris_file tells Swagger to set the file as an input file. Next, we read the CSV using read_csv and make sure the header is set to None if you haven’t set the column names while making the CSV. Next, we use the model to make the predictions and return them as a string.
Finally, we run the app using
In the console, the output generates a local URL, something like this –
Copy the URL (the one highlighted) and paste it in your browser. Add /apidocs to it and hit Enter. For example http://127.0.0.1:5000/apidocs. Something like this opens up –
Click on default and then on /predict. You’ll find something like this –
Above is a UI for your first app. Go ahead, insert the four values and click on ‘Try it out!’. Under the response body, you find the predicted class. For our second app, upload the file by clicking on choose file.
When you try it out, you get a string of predicted classes.
Congratulations! You have just created a nice UI for your ML model. Feel free to play around and try out new things.
Yes! Finally something about solver for x.