Named Entity Recognition App using Spacy, Gradio, and Hugging face Spaces

UPPU RAJESH KUMAR 28 Jan, 2022 • 5 min read

This article was published as a part of the Data Science Blogathon.

Introduction

Named Entity Recognition(NER) is a subtask of information extraction that locates and classifies different entities like name, organization, person, etc., in a sentence. Usually, it is done to classify named entities mentioned in unstructured text into predefined categories.

Named Entity Recognition(NER) has many real-world use cases. It can be used in News categorization by identifying several entities in an article and segmenting them into corresponding categories. It can also be used to automate customer service operations. For example, if a customer writes a query to the help desk then we can identify entities in the customers’ query and send that query to the corresponding office that deals with that particular query of the customer. Named Entity Recognition(NER) can also be used in recommendation engines. We can take the entities of the current article that a customer reads and match it with similar articles that have the same entities and recommend those articles that have similar entities to the article that the customer reads. In this way Named Entity Recognition(NER) is useful in many ways and is being used in many industries.

In this article, we shall build a small demo web app of Named Entity Recognition(NER) using Spacy and Gradio.

Overview

1) Spacy

2) Gradio

3) Hugging face Spaces

4) Building Gradio App

5) Deployment on Huggingface Spaces

6) Conclusion

Spacy

Named Entity Recognition App Spacy
Image-1

Spacy is an industrial-strength natural language processing library for all kinds of Natural language processing tasks. It is a widely used library by natural language processing professionals to build scalable NLP apps. It is easy to use and offers many functionalities like sentiment analysis, Named Entity Recognition, and also to train custom models with our own dataset. It’s an open-source library and hence it has a lot of support from the online community. In this demo app that we are building using gradio, we use the spacy library to do the task of Named Entity Recognition.

Gradio

Gradio is an open-source python library and gives us the fastest way to demo our machine learning model with a friendly web interface so that we can see if our model is working as per the desired way or not. It is a very useful library as it takes low code to get our web interface for the machine learning model. This library enables us to quickly prototype our model and check its performance. Many companies started using this library. Once we create a Gradio app we can even deploy it on desired hosting platforms. Gradio is a low code interface creating a python library for developing quick user interfaces.

Named Entity Recognition App
image-2

 

Hugging face Spaces

Hugging face spaces is a great way of hosting our machine learning models that we built using Streamlit or Gradio. It offers unlimited hosting services for any number of apps. We can find many demo apps and prototype apps built by the community in these spaces. Also, we can directly write our code in the designated sections and give the requirements.txt file to tell the necessary libraries to be installed. Huggingface spaces will automatically use all these files and deploy our app. This is a quick and efficient way of checking our deployed machine learning in production for further analysis.

We shall deploy our gradio app on hugging face spaces.

   Hugging face Spaces
image-3

Building Gradio App

We use the following code to create a named entity recognition function and to finally give an interface using Gradio.

pip install spacy
pip install gradio
import spacy
import gradio as gr
nlp = spacy.load("en_core_web_sm")
def ner(sentence):
    doc = nlp(sentence)
    ents = [(e.text, e.label_) for e in doc.ents]
    return ents
iface = gr.interface(fn=ner, inputs="text", outputs="text")
iface.launch()

That’s it. the above few lines of code will produce a spacy app that does named entity recognition and also gives us an interface to give text input and named entities as output.

Let’s see the explanation of the above code –

Firstly we install the necessary libraries using the ‘pip’ command. We install spacy and gradio libraries using the pip install command.

Next, we import those libraries. We import spacy as it is and we import gradio with alias ‘gr’. Next, we load the small English pipeline trained on written web text that includes vocabulary, syntaxes, and entities. As a next step, we define a function ‘ner’ that returns the named entities. Inside this function first, we declare an instance of nlp object for the given sentence using nlp(). Next, we extract the text and corresponding label of the text from the created doc object in the previous step as a tuple into a list. Thus our function produces the named entities of the given text along with the words and returns a list.

As a final step, we use gradio to create an interface for our spacy model. We create the interface using ‘gradio.interface()‘. This command takes a function, input mode, output mode as inputs. In our app, we gave the ‘ner‘ function as input function. Our input is a text, hence ‘input=’text’ ‘. Our expected output is also text, hence we gave output=’text’. Finally, we launch our app using ‘.launch()‘ method.

Finally, if you run the above code in your jupyter notebook you should be able to see the output as below –

Named Entity Recognition App

You can type your sentence under the sentence text box and click submit. You can see the result in the adjacent text box. Thus we have created a simple named entity recognition app using spacy and gradio. Now it’s time to deploy our app on hugging face spaces.

Deployment on Huggingface Spaces

To do this, go to this website and create an account if you don’t have one already. After you create an account login and click the ‘create new space’ button on the right side. Then you can see a new page asking for a name for the repository you are going to create. Give a name, choose a license, choose ‘Gradio’ under SDK options and finally click the ‘create new space’ button. This will create a repository of your app. Then click the ‘add file’ button on the top right corner and give the name ‘requirements.txt’. In that file add ‘spacy’ in the first line and in the second line add this link – ‘https://huggingface.co/spacy/en_core_web_sm/resolve/main/en_core_web_sm-any-py3-none-any.whl’. Finally, click the ‘commit changes’ button at the bottom of the page. Your app will be automatically deployed on hugging face servers. You can check your app by clicking the ‘App’ button in the title bar you see.

Thus we built and deployed a Named Entity Recognition app using spacy and Gradio.

Conclusion

Named Entity Recognition is an important NLP task that helps us in various fields like News article recommendation, brand recognition, etc., In this article, we saw how we can leverage the NER abilities that Spacy has to offer, and also we saw how to build an interface and deploy it on hugging face Spaces. If you have any queries, please comment below so that I can answer them to the best of my knowledge.

Image sources –

image-1: spaCy · Industrial-strength Natural Language Processing in Python

image-2: Gradio

image-3: Spaces – Hugging Face

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.

UPPU RAJESH KUMAR 28 Jan 2022

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Natural Language Processing
Become a full stack data scientist