Streamlit; The Awesome new way to deploy Data Science & Machine Learning Apps.

Boadzie Daniel
Nov 6 · 5 min read

It is often a challenge for Data Scientist and Machine Learning Engineers to deploy their projects and models that they’ve worked so hard to tune(with an accuracy of 99.89%). For some people, it means learning to use Flask for their model deployment. This is usually not easy since most of them are not Web developers.

My situation was similar until I met Streamlit; an awesome new was to easily deploy DS/ML apps without being a pro at web development. I was shocked at how easy it is to deploy my model and Data Science projects with Streamlit. For me, Streamlit is the awesome framework I was waiting for that can be compared to Shiny for R users. But what makes Streamlit so awesome?

The following are some of the reasons why Streamlit is dope;

  1. It is FREE and Open Source; Cool right? I know. It is free and open-source so you can use it, commercialize it and share it.
  2. It is EASY TO LEARN; The learning curve for Streamlit is zero because it is pure python with no callbacks or hidden state. So if you know Python, you know Streamlit. I built 3 awesome Machine Learning apps in a Day. All I had to do was refer to their awesome official documentation. NO HASSLE!!
  3. It Works with EVERYTHING; Streamlit works with most if not all the ML frameworks; from Tensorflow and Pytorch to Scikit-learn to Seaborn, Altair, Plotly and many many more.
  4. Easy to Install; installing Streamlit is like installing every other python package PERIOD! You can do it with pip.
  5. There is more coming; They have plans to add one-click deployment as well. So watch out for more.

So now you see why I’m obsessed with this framework?

In this article and a series of others to come, I will walk you through the entire step involved in building and deploying 3 awesome ml apps with Streamlit. We will start with a multilingual sentimental analysis. Sentimental Analysis which is also know as Opinion Mining; is a branch of Natural Language Processing which involves the extraction of sentiments in text. The VADER package makes it easy to do Sentimental Analysis in Python.

We will use the following packages;

pip install streamlit # the awesome framework
pip install vaderSentiment # for sentimental analysis
pip install googletrans # for language translation

Now that we have installed our packages, let’s test our Streamlit installation by running the hello world app;

streamlit hello # run the hello app

you should see the app launch and run in your default browser at http://localhost:8501. Hurray! your first Streamlit app. But now let’s build our own.

First, let’s create a folder and a file you can call app.py in it. Then import the libraries and initialize them with the following code within your app.py file;

import streamlit as st
from googletrans import Translator
from vaderSentiment.vaderSentiment import \ SentimentIntensityAnalyzer
analyser = SentimentIntensityAnalyzer() # initialize it
translator = Translator() # initialize

So we imported Streamlit and gave it the alias st and also imported Translator from googletrans and SentimentIntensityAnalyzer from vaderSentiment and then initialized them.

Next, we will use the Streamlit .markdown method to write some Markdown text that will serve as headers and sub-headers to our visitors. By the way, Markdown is a Markup language like HTML but cooler without all the numerous tags that you have to declare when using HTML.

st.markdown(‘# Machine Learning App Registry’)
st.markdown(
‘#### These are projects from Artificial Intelligence Movement(AIM) Led by [Boadzie Daniel](http://boadzie.surge.sh/)')
st.markdown(‘## App 1: Multilingual Sentimental Analysis’)st.write(‘Sentimental Analysis is a branch of Natural Language Processing \
which involves the extraction of sentiments in text. The VADER package makes it easy to do Sentimental Analysis’)

The brain of our app will now look like the following;

# The sentiment analyzer function
def sentiment_analyzer_scores(sentence):
trans = translator.translate(sentence).text # extracting translation text
score = analyser.polarity_scores(trans) # analyzing the text
score = score[‘compound’]
if score >= 0.05:
return ‘The sentiment of your text is Positive’
elif score > -0.5 and score < 0.05:
return ‘The sentiment of your text is Neutral’
else:
return ‘The sentiment of your text is Negative’
return score

That is the awesome brain of our app. We created a function that takes a sentence as an argument, translates the sentence, analyzes the sentiment and returns the score with the appropriate human-readable message. Cool right? I know!

Now, how do we render this function to the front-end using inputs from a user? Surprisingly, Streamlit makes this ridiculously easy.

sentence = st.text_area(‘Write your sentence’) # we take user inputif st.button(‘Submit’): # a button for submitting the form
result = sentiment_analyzer_scores(sentence) # run our function on it
st.balloons() # show some cool animation
st.success(result) # show result in a Bootstrap panel

In the code above, we are saving the user’s input in the variable called sentence, then we use a condition to render the sentimental value in a Bootstrap panel sprinkled with some beautiful balloon animation when the submit button is hit.

We can now run our app like this

streamlit run app.py # if app is the name of your file

and the app should launch in our default browser at http://localhost:8501. ( The entire code for this article and many others to come can be found at my GitHub page)

hurray!!

And just like that, we have our completed multilingual sentiment analyzer without a sweat! How awesome is that? ( The app is live here)

This is just one of the many awesome DS/ML apps you can create with Streamlit. The subsequent articles in this series will handle other apps like the Salary prediction and Iris flower classifier and any more. I strongly believe Streamlit is the future of Data Science and Machine Learning apps for Python developers. As such, it is highly recommended that you hop on board now (if you haven’t already) to be a part of this awesome journey.

Boadzie Daniel

Written by

AI Engineer, Data Scientist, Full-stack Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade