Named Entity Recognition App with Spacy, Gradio, & Hugging face Spaces
Named Entity Recognition Made Easy with Spacy, Gradio and Hugging Face
Named entity recognition, or NER for short, is a fundamental task in natural language processing (NLP) that involves identifying and categorizing key entities mentioned in text. For example, given the sentence "Microsoft acquired GitHub for $7.5 billion in 2018", a NER system would ideally identify "Microsoft" and "GitHub" as organizations and "$7.5 billion" and "2018" as a money amount and date respectively.
NER has numerous valuable real-world applications. It‘s often used as a first step in information extraction pipelines to automatically pull out structured data from unstructured text. This has applications such as:
- Categorizing news articles by identifying people, places and events mentioned
- Extracting product and price information from customer reviews
- Anonymizing personal information in documents for privacy and compliance
- Enhancing search by extracting key entities and topics from queries and results
- Linking entities to knowledge bases and databases for richer data aggregation
Given the immense value of NER across domains, let‘s dive into how you can easily build your own named entity recognition system using the popular spacy library. We‘ll also build an interactive web demo of our system using gradio, and deploy it live on Hugging Face Spaces for anyone to try. The full source code is available on GitHub to follow along.
Introducing the Spacy NLP Library
Spacy is the leading open-source library for industrial-strength NLP in Python. It provides a concise and intuitive interface for performing a wide range of NLP tasks, with a focus on developer productivity and scalable production usage. Some of the key features of spacy include:
- Fastest syntactic parser in the world
- Named entity recognition
- Support for 60+ languages
- Integrated word vectors and semantic similarity
- Easy model packaging, deployment and workflow management
- State-of-the-art speed and accuracy
For named entity recognition specifically, spacy provides a variety of pre-trained NER models that can identify a wide range of named and numeric entities, including persons, organizations, locations, dates, times, money amounts and more. These models have been trained on large, diverse datasets and are highly optimized for performance.
Using spacy‘s en_core_web_sm model, we can perform named entity recognition with just a few lines of Python code:
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Microsoft acquired GitHub for $7.5 billion in 2018.")
for ent in doc.ents:
print(ent.text, ent.label_)
This outputs:
Microsoft ORG
GitHub ORG
$7.5 billion MONEY
2018 DATE
Here we first load spacy‘s small English model optimized for the web. We then pass our example sentence to the nlp object to process it and obtain a Doc object. The doc.ents property contains all the named entities that were found, and we can iterate through them and print out their text and entity label.
It‘s that simple to get started with named entity recognition in spacy! The library provides many more advanced capabilities, such as fine-tuning models on your own datasets, detailed control over the NER algorithm, integrating with other pipeline components and much more. But even out-of-the-box, spacy‘s NER models are highly capable and can support a variety of downstream NLP and information extraction tasks.
Building a Web UI with Gradio
While extracting entities in a Python script is very useful, it‘s even better to make our NER system accessible to non-technical users through an interactive web interface. This is where gradio comes in. Gradio is an open-source Python library that lets you quickly build customizable UI components for your machine learning models, and easily share them with a link.
To build our NER web UI, we first define a Python function that will serve as the backend logic:
import spacy
nlp = spacy.load("en_core_web_sm")
def ner(text):
doc = nlp(text)
ents = [(ent.text, ent.label_) for ent in doc.ents]
return ents
This function takes a raw text string as input, processes it with spacy to extract the named entities, and returns a list of (entity_text, entity_label) tuples.
We then use gradio to create a web interface for this function:
import gradio as gr
iface = gr.Interface(fn=ner,
inputs=gr.Textbox(label="Text"),
outputs="html",
title="Named Entity Recognition",
description="Extract named entities from text with spacy.")
iface.launch()
Here we create a gradio Interface by passing our ner function, specifying the input component (a text box), output component (HTML), and adding a title and description. Launching the interface will automatically generate a web app that we can interact with locally.
With just a handful of lines of code, we now have a fully functioning web app for named entity recognition! Gradio supports many more input and output components, as well as event listeners, state, and flexible layouts and styling. It‘s a powerful and easy way to demo your ML models and quickly iterate on a web UI.
Deploying to Hugging Face Spaces
To make our app truly accessible to anyone on the web, we can deploy it for free on Hugging Face Spaces with just a few clicks. Hugging Face Spaces is a platform for easily deploying machine learning demos, with support for gradio, streamlit and other popular frameworks.
After creating a Hugging Face account, we can create a new Space and select Gradio as the framework. In the code editor, we paste our backend function code and gradio interface code. We also need to provide a requirements.txt file specifying the dependencies (spacy and gradio).
With our code and dependencies defined, the Space will automatically build and deploy our NER app, providing a public URL that we can share with anyone. We can also customize the appearance of the Space, view logs and monitor traffic.
And that‘s it! What started as a simple Python script is now a fully deployed web app that anyone can access and interact with. This ability to go from idea to accessible web UI in minutes is immensely powerful, and opens up many possibilities for prototyping, sharing and gathering feedback on ML models and NLP systems.
Next Steps
While our app is already quite functional and useful, there are many potential enhancements we could make, such as:
- Allowing the user to select different pre-trained spacy models for different languages or entity types
- Displaying entity information from a knowledge base
- Generating visualizations of entity relationships and co-occurrences
- Adding support for uploading and processing documents in addition to free text input
- Extracting other information like keywords and sentiment in addition to entities
- Fine-tuning the NER model on a custom dataset using spacy‘s training capabilities
With the flexibility and extensibility of spacy, gradio and Hugging Face Spaces, you can continue to iterate and adapt your app as your needs and use cases evolve. The modular nature of these tools also makes it easy to integrate your NLP pipeline with other data sources, storage systems and downstream applications.
Whether you‘re building a prototype for user research, demos for stakeholders, or production-grade systems, spacy and gradio provide a powerful toolkit for NLP and ML app development. By leveraging pre-trained models, flexible architectures and easy deployment options, you can rapidly go from idea to reality.
Conclusion
In this post, we saw how to build a complete named entity recognition application using the spacy NLP library, gradio for the web UI, and Hugging Face Spaces for deployment. With just a few dozen lines of code, we were able to create an app that can automatically extract useful entities from raw text, and make it available to anyone via a public URL.
The key takeaways are:
- Named entity recognition is a powerful NLP technique with many applications in information extraction, search, anonymization and more.
- Spacy provides fast, accurate and customizable NER capabilities out-of-the-box through pre-trained models and an intuitive Python interface.
- Gradio allows you to easily build web UI components for your ML models, and share them publicly with a link.
- Hugging Face Spaces enables one-click deployment of gradio and other ML web apps for free.
By leveraging these tools and following the workflow outlined in this post, you can rapidly prototype and productize your own NER systems and NLP applications. You can also adapt this approach for other NLP tasks like text classification, machine translation, summarization, and question answering.
I encourage you to try out the demo app and browse the source code on GitHub. Feel free to experiment, extend and adapt it to your own use cases. If you have any questions or feedback, let me know in the comments below!
We‘re truly living in an incredible time for NLP, with so many powerful open-source tools, models and datasets at our fingertips. It‘s never been easier to build amazing applications that can extract insights from the world‘s textual knowledge. The only limit is your imagination, so go out there and build something awesome!