BUILD APP MACHINE LEARNING WITH GRADIO
In machine learning we can make predictions with data using very specific techniques to train, evaluate and test our models.
What if we have the possibility of testing these models via a web interface?
Today we are going to use Gradio to deploy and test our models from a web interface.
Let’s get started
Exporting Machine Learning Models:
After training, evaluating and testing the models we will first export our model.
There are two possibilities for this, either with Pickle or Joblib.
We will use Joblib
After installing Joblib we will export our models by creating a dictionary where we include the models scaler numerical inputs and category type inputs.
Then we’re going to do a dump.
Now that our has just been exported let’s move on to installing Gradio now.
I think I’ll just reproduce the one from the documentation:
Gradio is the fastest way to demonstrate your machine learning model with a user-friendly web interface so anyone can use it, anywhere!
To install Gradio:
We I will use a virtual environment.
For this I create a requiements.txt file where I put this
For installation there is also the official gradio documentation
After creating requirements.txt file and creating a virtual environment.
Now let’s create a python file call it app.py
in this file first import the necessary libraries
Let’s load the model, the scaler and the imputers
Inside this function let’s create a Dataframe with the inputs from the dataset.
Still in the function let’s try to separate our numerical inputs and the categories inputs.
Let’s apply transformations to our input data with the use of our imputer loaded from the beginning, i.e. our variables cat_imputer and num_imputer.
Now let’s encode our input categories.To do this we will create a Dataframe.
Let’s do the same for our imputed numericals but this time with scaler
Let’s do a concatenation between our input_encoded_df and the input_scaled_df in order to have the encoded data set.
Prediction with our Model
Let’s create a prediction variable and apply our model to final_df
Now here we will see the result of our prediction and we return
We have just created a function that allows us to predict results from
Let’s move on from Gradio Interface
Remember you tell us that we are going to use Gradio to test our model from a web interface.
First let’s create an array
Let’s create a context gradio.block and inside let’s create two variables img and title for the image and the title.
Then inside this context for the inputs.
Let’s create a button:
Then at the end let’s define the output and the result of the prediction.
Close the context with app.launch()
In our terminal we execute the command
python app.py to visualize our web interface
Gradio is an interface that allows us to visualize our results in order to allow everyone, even novices, to see a machine learning model verified.