building machine learning apps with streamlit

Building Machine Learning Apps with Streamlit (How to Embed ML Models)

In this tutorial, we will be learning how to use an awesome framework called streamlit to productionize our machine learning models. We will learn how to embed our ML models into streamlit, we will build machine learning apps with streamlit. So what is Streamlit?

Streamlit is a machine learning framework for building production ready ML tools. To install streamlit you use pip as follows

pip install streamlit

Let us begin.

Our app will consist of only one part, which is going to be both the back-end and the front-end. Unlike flask or express that we will have to have a back-end where the logic is done and our front-end for the user, streamlit offers you these features all in one place.

We will be building a gender classifier app that classifies the gender using the first names of users.

This is what we will be building

For receiving our user input we will be using streamlit.text_input() and then send the data to our model for processing.

We will have to vectorize our data and then use our model to make the prediction.The work flow will be as follows;

  1. User Input
  2. Vectorization of User Input
  3. Classification by Pretrained Model
  4. Displaying our result to end user

Let us see the most important aspects of our app. First of all we will import streamlit in our app.py file as follows

import streamlit as st

Then we will create a main function within which all our logic will be. And then some other functions to load our vectorizers,models and our images.

def main():
    """Gender Classifier App"""
    name = st.text_input("Enter Name","Type Here")
    if st.button("Predict"):
	result = predict_gender([name])
	if result[0] == 0:
	   prediction = 'Female'
	   c_img = 'female.png'
	else:
	   result[0] == 1
	   prediction = 'Male'
 	   c_img = 'male.png'

        st.success('Name: {} was classified as {}'.format(name.title(),prediction))
        load_images(c_img)

    

if __name__=='__main__':
     main()


We will use joblib to load our vectorizer and our already trained models

 

import joblib
# load Vectorizer For Gender Prediction
gender_vectorizer = open("models/gender_vectorizer.pkl","rb")
gender_cv = joblib.load(gender_vectorizer)

# load Model For Gender Prediction
gender_nv_model = open("models/naivebayesgendermodel.pkl","rb")
gender_clf = joblib.load(gender_nv_model)


# Prediction
def predict_gender(data):
	vect = gender_cv.transform(data).toarray()
	result = gender_clf.predict(vect)
	return result

Then we will use these in our main function.

You get the entire code here

You can also check  the entire video tutorial here

Streamlit makes it easier to productionize our ML models with simplicity. Thanks for your time.

If you are interested in learning more you can join this community to get updated with new materials and tips.

Thanks again
Jesus Saves

By Jesse E. Agbe(JCharis)

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *