Building GUI for your Machine Learning Models

Building GUI for your Machine Learning Models

How to build a cool GUI for your Machine Learning models which enable users to input data

Edem Gold's photo
Edem Gold
ยทSep 19, 2021ยท

7 min read

Subscribe to my newsletter and never miss my upcoming articles

We cannot solve our problems with the same thinking we used to create them -Albert Einstein

If you have ever built a Machine Learning model you've probably felt, well this was cool but how will other people be able to see how cool it is, model deployment is a part of Machine Learning which isn't paid as much attention as it should.

Well in this article I will introduce you to a new tool that will help you generate a web app for your Machine Learning model which you can of course share with other individuals to try out.

I will be building a simple neural network model using scikit-learn (not TF sorry๐Ÿ˜’) and create a GUI for the model using Gradio (this is the new cool tool I spoke about).

Let us Begin

What is Gradio

below is the logo of the gradio library

gradio cover.png image credits: gradio

According to the Gradio website, Gradio allows you to quickly create customizable UI components around your TensorFlow or PyTorch models or even arbitrary Python functions. Not terribly informative ei๐Ÿ˜….

If you have ever used a python GUI library like Tkinter, Gradio is like that.

Gradio is a GUI library that allows you to create customizable GUI components for your Machine Learning model.

now that we understand what Gradio is


For you to successfully work through this tutorial

  • You must have python installed

Let's Get Building

You can check out the GitHub repo for the project here

Let us begin

Install required packages Packages

Let us install the required packages

pip install sklearn
pip install pandas
pip install numpy
pip install gradio

Getting our data

Our data is going to be in the. CSV format.

You can get the data by clicking here

Importing Packages

We are going to import the required packages

import numpy as np

import pandas as pd

import gradio as gr

Next, we are going to filter the warnings so we don't see them.

import warnings


Import the data

Next, we are going to import our data

data = pd.read_csv('diabetes.csv')

Now let's see a little preview of our dataset


Let us see the feature columns in our dataset

print (data.columns)

Getting our Variables

Next, we get our X and Y variables

x = data.drop(['Outcome'], axis=1)

y = data['Outcome']

Splitting the data

Now we are going to split our data using the scikit-learn's inbuilt train_test_split function.

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x,y)

Scaling our data

Next, we are going to scale our data using scikit-learn's inbuilt StandardScaler object.

from sklearn.preprocessing import StandardScaler

#instantiate StandardScaler object
scaler = StandardScaler()

#scale data
x_train_scaled = scaler.fit_transform(x_train)

x_test_scaled = scaler.fit_transform(x_test)

In the code above we scaled our data using the StandardScaler object made available to us through scikit-learn. To know more about Scaling and why it is done click here

Instantiating and training the model

In this section, we are going to create and train our model. The model we are going to make us of will be a Multi-Layer Perceptron Classifier, a neural network built into scikit-learn.

#import model object
from sklearn.neural_network import MLPClassifier
model =  MLPClassifier(max_iter=1000,  alpha=1)

#train model on training data, y_train)

#getting model performance on test data
print("accuracy:", model.score(x_test_scaled, y_test))

Creating the function for Gradio

Now is the fun part. Here we are going to create a function that will take in the features of the data set which our model was trained on and pass it as an array to our model to predict. Then we are going to build our gradio web app based on that function.

To understand why we have to write a function, you must first understand that gradio builds GUI components for our Machine Learning model based on the function. The function provides a way for gradio to get input from users and pass it on to the ML model which will then process it and then pass it back to gradio which then passes the result out.

let's write...

First, we will get the feature columns which we will then pass onto our function.

#geting our columns


Now we will create our function.

def diabetes(Pregnancies, Glucose, Blood_Pressure, SkinThickness, Insulin, BMI, Diabetes_Pedigree, Age):
#turning the arguments into a numpy array  

 x = np.array([Pregnancies,Glucose,Blood_Pressure,SkinThickness,Insulin,BMI,Diabetes_Pedigree,Age])

  prediction = model.predict(x.reshape(1, -1))

  return prediction

In the code above we passed the feature columns from our data as arguments into a function, we named diabetes. Then we turned the arguments into a numpy array which we then passed onto our model for prediction and finally we returned the predicted result of our model.

Creating our Gradio Interface

Now we are going to create our Web App interface using Gradio

outputs = gr.outputs.Textbox()

app = gr.Interface(fn=diabetes, inputs=['number','number','number','number','number','number','number','number'], outputs=outputs,description="This is a diabetes model")

The first thing we did above was to create a variable named outputs which hold the GUI component for our model result. The result of our model's prediction will be outputted in a text box.

Then we instantiated the Gradio interface object and passed in our earlier diabetes function. Then we generated our Inputs GUI component and then we told the radio to expect 8 inputs in form of numbers.

The inputs represent the feature columns that are present in our dataset, the same 8 feature column names we passed into our diabetes function.

Then we passed our earlier output variable into the outputs parameter present in the object.

Finally, we passed in the description of our web app into the description parameter.

Launching the Gradio Web App

Now we're going to Launch our Gradio web app.


NOTE: If you are launching the Gradio app as a scrip from that e command line you will be given a local host link which you will then copy and paste in our browser to see your web app.

If you are launching the app from a jupyter notebook you will see a live preview of the app as you run the cell and you will also be provided with a link.

Hosting and sharing your Web App

If you want to share your web app all you have to do is put in share=True as a parameter in your launch object.

#To provide a shareable link

A link will then be provided for you with a .gradio extension. But this shareable link lasts for only 24 hours and will last if only your system is running. Because Gradio just hosts the web app on your system.

In simple words, for your link to work, your system has to be on because gradio uses your system to host the web app and once your system is off. The server connection is severed and you get a 500๐Ÿ˜….

Luckily for us, Gradio also provides a way for you to permanently host your model. But the service is subscription-based so you have to pay $7 monthly to access it. Permanent hosting is way out of the scope o this article (partly because the author is broke๐Ÿ˜…). But if you are interested in it, click here


The Gradio Library is really cool and it helps solve a huge problem plaguing the Machine Learning Community, Model Deployment.

90 per cent of Machine Learning models built are not deployed and Gradio is working to fix that.

It also serves as a way for beginners and experts to show off their models and also test the models in real life.

You can't go wrong with the Gradio Library.

Congrats ๐ŸŽ‰, you're part of the read to the end club. if you liked the article subscribe to my blog so you don't miss my upcoming articles, I talk about AI and cool tech.

See you on the next one๐Ÿš€๐Ÿš€

Share this