Deploying a Machine Learning Model as a Web App Using Gradio

Newton Kimathi
6 min readAug 17, 2023

--

Imagine you’ve developed a sophisticated machine Learning model, in this case,, one that predicts customer churn for a telecommunication company. The model can potentially save businesses from losing valuable customers, and strengthening their customer retention. Now, how do you make this model accessible and user-friendly? The answer lies in deploying it as a web app. In this article, we’ll explore how to deploy a machine learning model into a web app using Gradio .

What is Gradio

Gradio is an open-source Python library that allows you to create customizable interfaces for your machine learning models with minimal code (Mukherjee & Dey, 2019). It is designed to help you quickly build and deploy interactive web-based applications that allow users to input data, interact with your model, and view the results (Nokeri, 2021). Simply put, it a that allows the common people interact with your complex machine learning model.

Prerequisites

To embark on this journey, you need the following tools:

  • Trained Machine Learning Model: You should have a well-trained machine learning model that is able to predict based on relevant features. In this case, we’ll consider a model that predicts whether a telecom customer is likely to churn or not.
  • Python and Virtual Environment: Ensure that Python is installed on your system and a virtual environment to manage project dependencies. You can create a virtual environment using the following commands:
* Windows
```python
python -m venv venv;# To create venv environment
venv\Scripts\activate # To activate the environment
python -m pip install -q --upgrade pip # To upgrade pip version
* Linux & MacOs
```python
python3 -m venv venv # To create venv environment
source venv/bin/activate # To activate the environment
python -m pip install -q --upgrade pip # To upgrade pip version

``````
  • Designated Requirements file : Create a requirement file with the essential libraries such as pandas, gradio, pickle and any other libraries specific to your model’s requirements. Run the code below to install the the requirements in your virtual environment.
python -m pip install -qr requirements.txt 

Step-by-Step Deployment

We will break down the deployment into simple steps, as follows:

Step 1. Importing Necessary Libraries

We start by importing the required libraries as below:

# Importations
import pandas as pd
import gradio as gr
import os
import pickle

Step 2. Loading Machine Learning Components

We load the machine learning components saved during the model training. These components include preprocessing transformers and the trained model itself. This is crucial for ensuring consistent processing of input data within the web app.

# Function to load machine learning components
def load_components_func(fp):
# To load the machine learning components saved to re-use in the app
with open(fp, "rb") as f:
object = pickle.load(f)
return object


# Using the created function to Load the machine learning components
DIRPATH = os.path.dirname(os.path.realpath(__file__))
ml_core_fp = os.path.join(DIRPATH,"ML_Model.pkl")
ml_components_dict = load_components_func(fp=ml_core_fp)

# Defining the variables for each component
label_encoder = ml_components_dict['label_encoder']
encoder = ml_components_dict['encoder']
imputer = ml_components_dict['imputer']
scaler = ml_components_dict['scaler']
balance = ml_components_dict['imbalance']
model = ml_components_dict['model']

Here, we define a function called load_components_func(fp) which takes a single argument fp(short for file path). This function is responsible for loading machine learning components that were previously saved to a file (in this case, “ML_Model.pkl”) so that they can be reused within the web app.
Then, the DIRPATH variable is assigned the directory path of the current script file using the os.path.realpath(__file__) method. This ensures that we get the absolute path of the directory containing the script.

Next, the ml_core_fp variable is created by joining the DIRPATH with the filename “ML_Model.pkl”, forming the complete file path to the saved machine learning components.

The load_components_func() function is then called with ml_core_fp as an argument to load the machine learning components from the specified file. The loaded components are stored in the ml_components_dict dictionary, which can then be used to access various machine learning objects, such as a trained model, preprocessing transformers, etc., for use within the web app.

Step 3. Implementing the Preprocessing Components

Once the machine learning components are loaded, the next step is to implement the prediction logic. In our customer churn prediction web app, we want to take user inputs, process them using the loaded transformers, and make predictions using the trained model. We will create a function, predict_churn function, that takes user inputs (*args) and uses the loaded machine learning components to preprocess the inputs, scale the features, and make predictions. Here’s how this can be achieved:

# We first define the function
def predict_churn(*args, scaler=scaler, model=model, imputer=imputer, encoder=encoder):

# The user inputs are combined into a DataFrame with appropriate column names.
input_data = pd.DataFrame([args], columns=expected_inputs)

# Categorical columns are encoded using the previously loaded categorical encoder (encoder)
# to transform them into a format suitable for the model.
num_col = input_data[['SeniorCitizen', 'tenure', 'MonthlyCharges', 'TotalCharges']]
cat_col = input_data[['gender', 'Partner', 'Dependents', 'PhoneService', 'MultipleLines',
'InternetService', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection',
'TechSupport', 'StreamingTV', 'StreamingMovies', 'Contract',
'PaperlessBilling', 'PaymentMethod']]
cat_col = cat_col.astype(str)
encoded_data = encoder.transform(cat_col)
encoded_df = pd.concat([num_col, encoded_data], axis=1)

# If there are any missing values in the input data, they are imputed using the loaded imputer (imputer).

imputed_df = imputer.transform(encoded_df)

# The preprocessed data is then scaled using the loaded scaler (scaler)
# to ensure consistency with the scaling used during model training.

scaled_df = scaler.transform(encoded_df)

# The scaled data is fed into the loaded machine learning model (model)
# to obtain predictions. The predicted probabilities of churn and staying
# are calculated based on the model's output.
model_output = model.predict_proba(scaled_df)
#Probability of Churn(Positive class)
prob_Churn = float(model_output[0][1])
#Probability of staying(Negative Class)
prob_Stay = 1 - prob_Churn
return {"Prediction Churn": prob_Churn,
"Prediction Not Churn": prob_Stay}

3. Defining Input and Output Components

We define input and output components using Gradio. These components shape how users interact with the model. Each input component corresponds to a feature, and the output component displays the prediction.

Gender = gr.Radio(choices=['Male', 'Female'], label="Gender : Gender of the customer")
Partner = gr.Radio(choices=['Yes', 'No'], label="Partner : Whether the customer has a partner.")
Dependents = gr.Radio(choices=['Yes', 'No'], label="Dependents : Whether the customer has dependents.")
Tenure = gr.Number(label="Tenure : The Number of months the customer has been with the com
# Other inputs are defined in the repository
churn_prediction_label = gr.Label("Awaiting Submission....")

5. Creating the Web App Interface

We create the web app interface using the gr.Interface() function. This function takes input and output components, the prediction function, and other optional settings.

gr.Interface(inputs= Expected_Inputs,
outputs=churn_prediction_label,
fn=predict_churn,
title="Teleco Services Customer Churn Prediction",
description="Predict whether a customer will churn or stay based on input features."
).launch(inbrowser=True, show_error=True)

Run the App:

In the terminal, navigate to the project directory and run the following command :

python app.py

This will start the app and provide you with a local URL where you can access it.

Additional Resources

  1. GitHub Repository: https://github.com/Newton23-nk/Telco_Churn_Gradio_App
  2. Telco Churn Analysis and Modelling Repository: https://github.com/Newton23-nk/Telco-Customer-Churn

References

Mukherjee, A., & Dey, N. (2019). Python API modules for machine learning and Arduino. Smart Computing with Open Source Platforms, 191–205. https://doi.org/10.1201/9781351120340-20

Nokeri, T. C. (2021). Integrating a machine learning algorithm into a web app. Web App Development and Real-Time Web Analytics with Python, 189–213. https://doi.org/10.1007/978-1-4842-7783-6_11

--

--

Newton Kimathi

Data Scientist || Data Analyst || Python, R, SQL, PowerBI , Machine Learning ||