Create a Machine Learning Demo Using Gradio

Let’s build a Machine Learning Demo Using Gradio. We will be using the Titanic Dataset as an example.

Link to the Google Collab Notebook.

First, we will install and load the libraries.

!pip install -q gradio
!pip install -q catboost

import pandas as pd
import numpy as np
import seaborn as sns

sns.set_theme()

Then let’s load the data and analyse the data types.

df = sns.load_dataset("titanic")
df.dtypes

>>>
survived          int64
pclass            int64
sex              object
age             float64
sibsp             int64
parch             int64
fare            float64
embarked         object
class          category
who              object
adult_male         bool
deck           category
embark_town      object
alive            object
alone              bool
dtype: object

We can see that we have the target as survived, the same is also as alive. Also, we will not be using all the features to create the model as we want to show how you can demo a Machine Learning model as a live predictor, so we don’t want to overburden the user.

# Identifying features, we are keeping very few features as we want to simulate this using gradio
features = [ 'pclass', 'sex', 'age', 'fare',
       'embarked']
target = 'survived'

Then we replace the missing values. For age, we will replace it with the median and for the embarked we will replace it with the most common value, which is S.

# Filling missing values
df['age'].fillna(np.nanquantile(df['age'], 0.5), inplace = True)
df['embarked'].fillna("S", inplace = True)

Now let’s build the model. No tuning, as getting the highest accuracy or f1-score is not objective.

from catboost import CatBoostClassifier
clf = CatBoostClassifier()

# Creating features and target
X = df[features]
y = df[target]

clf.fit(X,y, cat_features=['pclass', 'sex', 'embarked'])

The we write the function which takes in the inputs and returns an output of whether the passenger would’ve survived or not.

def predict(pclass:int = 3, 
            sex:str = "male", 
            age:float = 30, 
            fare:float = 100, 
            embarked:str = "S"):
  prediction_array = np.array([pclass, sex, age, fare, embarked])
  survived = clf.predict(prediction_array)
  if survived == 1:
    return f"The passenger survived"
  else:
    return f"The passenger did not survive"

Now for the gradio demo, we want to take in these inputs with different gradio components, pass those as inputs to the prediction function and display the output. I go into much more detail on how this is done in the YouTube video, but the code snippet has comments which will help in case you don’t want to watch the explainer video.

with gr.Blocks() as demo:
  # Keeping the three categorical feature input in the same row
  with gr.Row() as row1:
    pclass = gr.Dropdown(choices=[1,2,3], label= "pclass")
    sex = gr.Dropdown(choices =["male", "female"], label = "sex")
    embarked = gr.Dropdown(choices =["C", "Q", "S"], label = "embarked")
  # Creating slider for the two numerical inputs and also defining the limits for both
  age = gr.Slider(1,100, label = "age", interactive = True
  )
  fare = gr.Slider(10,600, label = "fare", interactive = True
  )

  submit = gr.Button(value = 'Predict')

  # Showing the output 
  output = gr.Textbox(label = "Whether the passenger survived ?", interactive = False,)

  # Defining what happens when the user clicks the submit button
  submit.click(predict, inputs = [pclass,sex, age,fare,embarked], outputs = [output])

demo.launch(share = False, debug = False)

Then you’ll get an output like this, where you’re free to play around with the features and see what the ML model’s output will be.

Let me know in case you want to build something else with Gradio or want me to cover any ML topic in the comments below.

Comments

Leave a comment