Create an interactive app for business users to consume an ML model in a simple and intuitive way, for example requesting a Chun prediction for one given customer.
This template is for developing a UI to easily consume ML models. By following this template, you can create ML-powered interactive applications that can be easily used by a wide range of business users, and that can deliver real value and insights to end-users.
This tutorial is tailored for the Churn Prediction model, but can be modified to work with other models as well.
Import required modules & load models
from sklearn.feature_extraction.text import TfidfVectorizer
from joblib import load
import pandas as pd
# Loading models to make predictions
model = load('/data_app/model_churn_prediction')
encoder = load('/data_app/encoder_churn_prediction')
Building a UI
The app is designed to allow users to Search records by customer name and obtain churn predictions for a customer. The UI includes a Submit button to get the records and initiate the analysis process.
st.title('Churn Prediction')
st.write("Welcome to Peliqan's Churn Prediction app!")
form = st.form(key='churn-form')
user_input = form.text_input('Search by customer name')
submit = form.form_submit_button('Submit')
Learn more about Streamlit functionality here.
Create a function to get records on search
We are applying the user search to find records in an SQL query:
def getRecords(searchTerm):
my_query = f"select * from customers where lower(CustomerName) like lower('{searchTerm}%') limit 5"
dbconn = pq.dbconnect(pq.DW_NAME)
data = dbconn.fetch(pq.DW_NAME, query = my_query)
st.dataframe(data) # show searched results
return data
Write a function to make predictions
Now we will define the function makePrediction that takes a user_input parameter as input. It uses a previously trained encoder and model object to predict the Churn probability of customer:
def makePrediction(user_input):
df = getRecords(user_input)
if df.empty:
return st.error(f"Customer with name {user_input} does not exist.")
X = df.drop(['Prediction', 'Churn', 'CustomerName'], axis=1)
# converting categorical columns to numeric
cat_cols = X.select_dtypes('object').columns
X[cat_cols] = X[cat_cols].apply(lambda x: encoder[x.name].transform(x))
# Making prediction
prediction = model.predict(X)
proba = model.predict_proba(X)[0][prediction]
result = pd.DataFrame({'CustomerID':X['CustomerID'], 'Customer Name':df['CustomerName'], 'Prediction': prediction, 'Probability': proba})
return result
Logic for Submit button
When the submit button is pressed, the makePrediction()
function is called with the user input as the argument. The predicted churn score for each found record is extracted from the function's returned result. Depending on the predicted score, the UI displays a message. We also plot a bar chart for Prediction vs Customer name.
if submit:
result = makePrediction(user_input)
if type(result) == pd.DataFrame:
st.header('Predictions')
st.dataframe(result)
st.header('Prediction of churn vs Customers')
st.bar_chart(result, y='Prediction', x='Customer Name')
What’s Next
- You can make real-time predictions on new incoming data and send alerts to Slack if the model makes a prediction above a certain threshold.
- You can make predictions on real-time incoming data using the saved model. Learn more about making real-time predictions on new incoming data.