Streamlit is an open-source python framework used to deploy machine learning models, analyze and visualize datasets in an interactive manner. Streamlit makes it very easy to run webapps with a very few lines of code which makes the job very easy for data scientists.
Before we get started let’s install streamlit using pip
After that done let’s see whether streamlit is working or not by running the following in the terminal:
It will start a streamlit webapp and will provide the link to the localhost in the terminal.
Working with streamlit is simple. Everything is written in simple python and to run it we just need to call streamlit run ‘your filename’ in the terminal.
In this article, we will be building a Text summarizer webapp with streamlit. Which will take a paragraph as an input from the user and display a summary of that paragraph using NLP models.
And we will also be building another application in the same webapp. In the second one we will deploy a webapp that helps the user to process some input text, by giving functionalities like, converting text to lowercase, removing punctuation, and converting the sentences to a list of words.
We need to install Genism and Sumy libraries for text summarization. Gensim is a library that is used for summarizing texts and is based on the ranks of text sentences using a variation of the TextRank algorithm. Similarly, Sumy is also a text Summarization library that contains LexRankSummarizer. It is an unsupervised approach inspired by algorithms PageRank and HITs.
Install them using pip
So let’s create a python file and name it app.py and open it in any text editor.
Download full code:
Importing the libraries
Next, let’s initialize the main function:
st.title is used to display a title in our app which takes a string as parameter. Call the main function.
Now we will modify our main function as we move on. As we are doing 2 tasks Text Summarization and Text processing, we are going to give the user options to choose between them. This will be displayed in the left side of the screen as a sidebar which can be done using the sidebar function of the streamlit.
So in the main function we will be adding the following lines,
Now we will run the app using streamlit run app.py in the terminal
Once the user has choosen the option it will be saved in the variable ‘choice’.
If the user has chosen the Summarize then the user is asked to enter the text that is to be summarized and also with what algorithm using which the summarization will be done
The subheader will display the string as a sub heading to the app. To take the paragraph input we have used the text_area method. And we have used the selectbox method to select the summarizing technique that we want to use.
With that our app will look like the following:
Once the selection of summarizer is done we will use a button in streamlit to perform the summarization operation with the selected technique and display the result.
So let’s test our app using Genism as the summarizer.
Otherwise if the user has chosen Sumy Lex Rank as the option then we will use the following function to summarize the text.
First it will break the text into smaller parts, then we instantiate LexRankSummarizer class. To that object we pass the parser or the words and it will convert it to a list of strings. Lastly, we joined the list and returned it as the output.
Up until now our main function would look something like this,
With that, one section of our app is completed. Next we will build the Text processing section.
If the user selects Text processing from the sidebar then we will ask for the input text after that we will give the user a list of operations to perform on the text.
The list of operations are Convert to lowercase, Remove the punctuation, Convert the sentence into a list of words etc. The user can select single or multiple operations at a time for which we have used the multiselect method. It returns a list containing all the functionalities selected by the user.
The choice of the user will be saved in the variable ‘choiceOperations’.
Next we will create a button named Process using the button method, when clicked the operations selected by the user will be performed on the text.
If the user has selected the first option then it will convert the raw_text into lower case
We used a flag to check if it is the first operation on the text if it is a then it will do the functionality on the raw_text and save the output in the out string as well as update the flag variable as False so that next operation must be done on the out string.
So let’s test our app,
Now If the user has selected the second option then we have used the variable called punctuation and which includes all the punctuation in it. We will parse through the string if we find any punctuation specified in the variable we will replace that by an empty string.
If the user has chosen the last option for converting all the sentences into a list of words then:
Finally we will display the output using the write method.
So let’s check all the functionality at a time:
With this our app is completed. Here we have successfully deployed two usecases in a single webapp with streamlit. Similarly we can even deploy multiple machine learning models to a single app very easily and efficiently. And host those apps to platforms like AWS, Heroku and many more.
You can also add different text summarizers aside from Genism and Sumy and even other text processing techniques like removing all the numbers, converting paragraph to list of sentences, converting all letters to capital, and many more.