Simple NLP app with Spacy-Streamlit

In this tutorial we will build a simple natural language processing(NLP) app with an awesome package called Spacy-Streamlit. Spacy-Streamlit is a python package that is useful for visualizing spaCy models and building interactive spaCy-powered apps with Streamlit.

It has several functions and utils for visualizing spacy’s essential NLP features such as

  • Tokenization using visualize_tokens()
  • NER(Named Entity Recognition) using visualize_ner()
  • Parser using visualize_parser()
  • Sentence Similarity using visualize_similarity()
  • Text Categorizer using visualize_textcat()
  • etc

With Spacy-streamlit making visualization for NLP task is very easy and requires little code. It contains several building block for doing some cools stuff.

Let us see how to install and how to build a simple NLP app with it.

Installation

pip install spacy-streamlit

NOTE: you will also need to install streamlit and spacy alongside their models.

In installing the various models you can use

python -m spacy download en

Our simple app will have two main sections:

  • Home
  • NER

Home: This is  where we will analyze and display our text as tokens

We will be using the basic streamlit  st.text_area() function for receiving input from the user and then we will use spacy_streamlit.visualize_tokens() to visualize and analyze our text like below.

spacy_streamlit.visualize_tokens(docx,attrs=['text','pos_','dep_','ent_type_'])

The spacy_streamlit.visualize_tokens() by default will list all the attributes, but you can specify which attributes you would like to analyze and display as a dataframe.

NER:In this section of the app  we will visualize any named entity in our text using the space_streamlit.visualize_ner( ) function

With this function you can even omit entity labels you don’t want to recognize which is quite cool considering the numerous ways we can apply this.

Below is the entire code for our NLP app.

# Core Pkgs
import streamlit as st 

# NLP Pkgs
import spacy_streamlit
import spacy
nlp = spacy.load('en')

import os
from PIL import Image


def main():
	"""A Simple NLP app with Spacy-Streamlit"""

	st.title("Spacy-Streamlit NLP App")
        our_image = Image.open(os.path.join('SpaCy_logo.svg.png'))
	st.image(our_image)

	menu = ["Home","NER"]
	choice = st.sidebar.selectbox("Menu",menu)

	if choice == "Home":
		st.subheader("Tokenization")
		raw_text = st.text_area("Your Text","Enter Text Here")
		docx = nlp(raw_text)
		if st.button("Tokenize"):
			spacy_streamlit.visualize_tokens(docx,attrs=['text','pos_','dep_','ent_type_'])

	elif choice == "NER":
		st.subheader("Named Entity Recognition")
		raw_text = st.text_area("Your Text","Enter Text Here")
		docx = nlp(raw_text)
		spacy_streamlit.visualize_ner(docx,labels=nlp.get_pipe('ner').labels)


if __name__ == '__main__':
	main()

We can now run our app from our terminal using

streamlit run app.py

Our app will automatically be opened on the expected localhost in our browser.

To conclude spacy_streamlit makes it quite easier to visualize interesting NLP takes with just simple code. Awesome work by @ines from Explosion AI.

You can also check out the video tutorial below

Thanks for your time.

Jesus Saves

By Jesse E.Agbe(JCharis)

 

2 thoughts on “Simple NLP app with Spacy-Streamlit”

Leave a Comment

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