Content ITV PRO
This is Itvedant Content department
Build ML Web App Using Streamlit
Business Scenario
Welcome!
Today is your tenth day as a Junior Data Scientist at Telecom Customer Intelligence Solutions.
Over the previous labs, your team successfully developed multiple Machine Learning models to predict customer churn. These models help identify customers who are likely to discontinue telecom services, enabling the business to take proactive retention measures.
While the model performs well inside Jupyter Notebook, it is not easily accessible to customer retention managers and business stakeholders. Most users do not have Python knowledge and cannot interact directly with machine learning code.
The management team now wants a simple web-based application where users can enter customer information and instantly receive churn predictions.
To solve this challenge, your manager has asked you to deploy the Customer Churn Prediction Model using Streamlit.
As part of this project, you will:
Pre-Lab Preparation
Topic : Model Deployment Basics
1) Introduction to Streamlit for ML Model Deployment
2) Web Application for ML Predictions
git pull origin branchNameGit Pull
Task 1: Understanding Streamlit
Before deploying machine learning models, it is important to understand how models are typically used and why deployment is necessary.
Traditional Machine Learning Workflow
In a typical Machine Learning workflow:
Example:
prediction = model.predict(new_data)This workflow works well for developers but creates a limitation because only users familiar with Python can interact with the model.
To make machine learning models accessible to non-technical users, we need a user interface where users can provide inputs and receive predictions.
What is Streamlit?
Streamlit is an open-source Python framework used to build interactive data applications and machine learning dashboards quickly.
Instead of creating complex web applications, developers simply write Python scripts containing Streamlit commands. Streamlit automatically converts these scripts into a web interface.
Why Streamlit is Popular?
Built-In Widgets
Streamlit provides ready-to-use components such as:
These components make Streamlit an excellent tool for Machine Learning demonstrations and rapid prototyping.
Task 2: Build ML Web App Using Streamlit
Now that the Customer Churn Prediction model has been successfully trained, the customer retention team wants a simple interface where they can enter customer details and instantly determine whether a customer is likely to churn.
Click to download previous file : ML Lab 8.ipynb
Save Logistic Regression Model
1
import pickle
with open("churn_model.pkl","wb") as f:
pickle.dump(model,f)Create Streamlit Application
2
pickle.dump() saves the trained model into a file."wb" means Write Binary Mode.app.pyCreate a new Python file:
A Streamlit application is simply a Python script containing Streamlit functions.
Import Required Libraries
3
!pip install streamlit
import streamlit as st
import pickle
import numpy as npLoad Saved Model
4
model = pickle.load(
open("churn_model.pkl","rb")
)pickle.load() loads the trained model."rb" means Read Binary Mode.Create Dashboard Title
5
st.title(
"Telecom Customer Churn Prediction"
)
st.write(
"Predict whether a customer is likely to churn."
)st.title() displays the application title.st.write() displays supporting information.Create Sidebar Layout
6
st.sidebar.title(
"Customer Information"
)The sidebar helps organize input controls separately from the main dashboard.
Create Customer Input Components
7
tenure = st.sidebar.slider(
"Tenure",
0,
72
)
monthlycharges = st.sidebar.slider(
"Monthly Charges",
0,
150
)
totalcharges = st.sidebar.slider(
"Total Charges",
0,
10000
)Create Predict Button
8
st.slider() creates interactive sliders that allow users to select values within a specified range.
predict_button = st.button(
"Predict Churn"
)st.button() creates a clickable button that triggers prediction.
Generate Prediction
9
if predict_button:
features = np.array([
[ tenure, monthlycharges,totalcharges ])
prediction = model.predict(
features
)
st.success(
f"Prediction: {prediction[0]}"
)When the button is clicked:
Complete Streamlit Application
10
import streamlit as st
import pickle
import numpy as np
model = pickle.load(
open("churn_model.pkl","rb")
)
st.title(
"Telecom Customer Churn Prediction"
)
st.write(
"Predict whether a customer is likely to churn."
)
st.sidebar.title(
"Customer Information"
)
tenure = st.sidebar.slider(
"Tenure",
0,
72
)
monthlycharges = st.sidebar.slider(
"Monthly Charges",
0,
150
)
totalcharges = st.sidebar.slider(
"Total Charges",
0,
10000
)
if st.button(
"Predict Churn"
):
features = np.array([
[
tenure,
monthlycharges,
totalcharges
]
])
prediction = model.predict(
features
)
st.success(
f"Prediction: {prediction[0]}"
)Run the Application
11
streamlit run app.py
Great job!
You have successfully completed Lab 16: Build ML Web App Using Streamlit.
In this lab, you have: Understood Streamlit fundamentals, Learned the basics of ML model deployment, Saved and loaded models using Pickle, Built a Streamlit dashboard, Created interactive user input components, Generated churn predictions using a Logistic Regression model, Connected Machine Learning with a business-friendly web application.
You are now ready to move to the next stage of machine learning deployment. In the next lab, you will Deploy a Machine Learning Model for Real-World Use and learn how to make your ML applications accessible beyond the local environment.
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Topic : Model Deployment Basics
1) Web Application for ML Predictions
By Content ITV