Building a Machine learning website under 5mins
Back to the days when we used to spend hours spending days under deployment of ML models. A lot of time spent on building a good UI as well. Things were a lot messed up, making things more dynamic required usage of asynchronous connections to make things more interactive for users.
Fixing every little thing in UI to making the user experience fit the goal stack.
There were many ways of deploying machine learning models and making it available to real-time users. I still remember the days when we used to spend hours just to reduce the computation time and making everything perform in real-time.
Users love when they see results in real-time with the least time delay.
So what's the solution for this?
Hell yeah !!! there is a solution to it.
Recently, I came across this repo on Github named Streamlit. That helps the developer to deploy the machine learning code with just a few lines of code.
Now, what in the world is Streamlit?
Simple !!! isn't it.
How does Streamlit work?
Streamlit is an open-sourced library. Rather than a paid web app. Caching in streamlit simplifies setting up computation pipelines. Chaining cached functions automatically creates efficient computation pipelines.
In short, the pipeline is load_metadata → create_summary. Every time we run our streamlit web app streamlit only considers whatever subset of the pipeline is required to get the right answer.
Now let's jump into code and see how our streamlit library works. Will then be building a webapp for real-time sentence classifications and in-depth analysis.
So let's get started with Building a Simple Hello World Web-app.
Installing streamlit via pip. Open up a terminal and paste the below command.
pip install streamlit
To see the demo version streamlit you can simply write the below command
So we will see something like this when we type “streamlit hello” on your terminal. The app will open up on port number 8501.
Starting with your own code. Copy-paste the below code on your editor.
The code is in the simplest readable version possible. Moreover, it's just simple English. After you paste this code to your editor, make sure that you save it…lol.
You can run the app by writing the following command.
streamlit run app.py
This is what you see on running the above lines of code.
Okay !! Now that was something quick that too with less than 16 lines of code.
Deploying a Machine Learning Model onto your Project
We will be creating an app where we pass a sentence and as an output, we get an in-depth analysis of the same sentence. We will be using a sentence classifier, as well as performing some actions like NER on it. All the codes with are in this repo.
Deploying our machine learning code. Its simple you just need to write the following into your code.
Well for my app I used an fasttext model that I had created about two years ago. The model classifies a sentence to various emotions like love, happiness, sadness, hate, surprise, etc. We can go further and add more advanced package like BERT for sentiment analysis. That completely depends on how you use it.
For our NER model, we will be using a package named spacy-streamlit. A package that was launched just few days ago to integrate basic features visualization on NLP into streamlit.
To install spacy-streamlit,
pip install spacy-streamlit
Let's dig into the code. Let's start off by importing some packages.
Step 2: start off by setting a path for our model and then passing the input from the user to the model directly.
This is just the demo code on how to load your model and pass input to your model. The major intention being on how you can pass the input from the UI directly without making the old-way POST requests and then wait for the server to get back with a response. After you type your sentence in the text box, just hit enter(better would be to provide a button for it). You can see something like this when you run the code.
Here We Go !!!! We did it. The deployment otherwise takes a lot of time. Then to handle the post requests, i.e, the old way, streamlit simplifies those things. And makes sure that you can write a hassle-free code. It simplifies the initial steps that help you focus on the other important features. This package was built for machine learning engineers so that they can spend most of there time building their models rather than spending most of there time on the web-app and thereby compromising on the quality.
Well, this was just a small example, you can also look into its official repo where they have demonstrated streamlit on self-driving cars.
You can find the link to the repo at- link. Follow me on medium and Github to learn more.
I have been working closely with startups for the past 4 years. Having worked with more than 4 startups. I truly believe that time plays a very important role. Selecting the priority list according to the feedback always stay at the top priority. Startups are also limited by a small team, who barely can afford to hire a UI developer. This seemed to be the most good to go solution to me. With time we will overcome these small problems, maybe this was a good start, that will help developers to increase there efficiency.
If you like the blog do clap, comment, and share. Follow me on Linkedin to get in touch.