Building a Powerful Text Analysis App with Spacy, Streamlit, and Hugging Face Spaces
Text analysis, also known as natural language processing (NLP), is a rapidly growing field that involves extracting insights and meaning from unstructured text data. By applying techniques like sentiment analysis, named entity recognition, topic modeling, and more, we can gain a deeper understanding of everything from customer feedback to news articles to social media posts.
Some common applications of text analysis include:
- Monitoring brand sentiment on social media
- Automatically categorizing and routing customer support tickets
- Identifying key people, places, and events mentioned in news articles
- Detecting spam and hate speech in online content
- Summarizing long documents and reports
In this post, we‘ll walk through how to build an interactive text analysis web app using the popular Spacy NLP library, Streamlit for creating the user interface, and Hugging Face Spaces for deployment. Specifically, we‘ll focus on three core NLP tasks:
- Sentiment Analysis – Is the text positive, negative, or neutral in emotion?
- Named Entity Recognition – What people, places, organizations, etc. are mentioned?
- Subjectivity Analysis – Is the text more objective (factual) or subjective (opinion-based)?
By the end, you‘ll have a fully functional app that you can adapt and extend for your own text analysis needs. Let‘s get started!
Setting Up the Environment
The first step is to set up our Python development environment and install the required libraries. If you don‘t have Python already, you can download it from python.org.
Next, create a new virtual environment to isolate our app‘s dependencies:
python -m venv myenv
source myenv/bin/activate # For Unix/MacOS
myenv\Scripts\activate.bat # For Windows
Now install the necessary packages:
pip install spacy streamlit
python -m spacy download en_core_web_sm
This will install spacy, streamlit, and spacy‘s small English language model which we‘ll use for processing text. We‘re ready to start building the app!
Coding the Streamlit App
Create a new Python file called app.py and add the following code:
import streamlit as st
import spacy
from spacy import displacy
nlp = spacy.load("en_core_web_sm")
st.title("Spacy Text Analyzer")
st.markdown("Enter some text below and choose an analysis option.")
text = st.text_area("Text to analyze", "Sam was an excellent employee and always went above and beyond what was asked. He was let go yesterday which came as a huge shock to the team.")
selected_analysis = st.sidebar.radio("Analysis", ["Sentiment", "NER", "Subjectivity"])
if selected_analysis == "Sentiment":
doc = nlp(text)
sentiment = doc._.blob.polarity
interpretation = "Positive" if sentiment > 0 else "Negative" if sentiment < 0 else "Neutral"
st.write(f"Sentiment: {sentiment:.3f} ({interpretation})")
elif selected_analysis == "NER":
doc = nlp(text)
ner_html = displacy.render(doc, style="ent")
st.markdown(ner_html, unsafe_allow_html=True)
elif selected_analysis == "Subjectivity":
doc = nlp(text)
subjectivity = doc._.blob.subjectivity
interpretation = "Subjective" if subjectivity > 0.5 else "Objective"
st.write(f"Subjectivity: {subjectivity:.3f} ({interpretation})")
This code does the following:
-
We import the required libraries and load spacy‘s English model.
-
We create a title and instructions using st.title() and st.markdown().
-
We add a text area input where the user can paste or type the text they want to analyze.
-
We use st.sidebar.radio() to let the user pick which type of analysis to run (sentiment, NER, or subjectivity).
-
Depending on which analysis is selected, we process the text with spacy and display the results.
- For sentiment, we use spacy_textblob to get the polarity score and map it to positive/negative/neutral.
- For NER, we use displacy.render() to create an HTML visualization of the named entities.
- For subjectivity, we get the subjectivity score and interpret it as subjective/objective.
Overall, this gives us an interface like:
[Screenshot of Streamlit app]The user can enter any text, select an analysis approach from the sidebar, and immediately see the results. Pretty neat!
Now let‘s take a closer look at how spacy handles these analysis tasks under the hood, focusing on sentiment analysis.
Sentiment Analysis with spaCy
Sentiment analysis is the process of determining whether a piece of text is positive, negative, or neutral. It‘s commonly used to gauge opinionns, emotions, and attitudes in things like product reviews, social media comments, and customer feedback.
spaCy offers sentiment analysis capabilities via the spacy_textblob extension package. TextBlob itself is a separate Python library for processing textual data.
When we call doc._.blob.polarity in our code, here‘s what happens:
- spacy_textblob tokenizes the text and performs part-of-speech tagging under the hood using spacy‘s pipeline
- It then looks up each word in a table of sentiment lexicons to retrieve a polarity score (positive or negative)
- The polarity scores for each word are combined to calculate an overall sentiment score between -1.0 and 1.0, where -1.0 is very negative, 0 is neutral, and 1.0 is very positive
This approach of scoring individual words and averaging them is called a "bag-of-words" model. It‘s relatively simple but quite effective for many use cases.
That said, this method isn‘t perfect. It doesn‘t handle negation well ("not good" would be scored positively), and it can miss sentiment that depends on word order or spanning multiple words.
Deep learning-based sentiment models can capture more nuance and context, but require a lot more training data and compute resources. Tools like flair and transformers provide state-of-the-art sentiment models that may be a better fit depending on your accuracy needs and the type of text being analyzed.
To assess spacy_textblob‘s accuracy, we could run it on a labeled test set of texts and compare the predicted sentiment to the true labels. A common metric is the F1 score which balances precision and recall.
In practice, spacy_textblob‘s sentiment model is convenient to use and provides a quick way to get a sentiment read on text data. But it‘s always a good idea to spot check a sample of the model‘s predictions to validate that it fits your use case.
Taking the App Further
We‘ve built a useful text analysis app, but there are tons of possibilities to extend it! Here are a few ideas:
Add more NLP capabilities. spacy offers a wide range of other features like tokenization, lemmatization, part-of-speech tagging, dependency parsing, and more. We could expose some of these in the app to let users explore the linguistic structure of text in depth.
Extract insights and visuals. Going beyond just displaying the raw NLP outputs, we could provide summarized insights like "Top 5 mentioned people" or "Distribution of sentiment scores". Interactive visualizations could help users understand patterns in the text more intuitively.
Enable document-level analysis. In addition to analyzing individual text snippets, we could let users upload full documents (PDFs, Word files, etc.) and apply the NLP pipeline to automatically extract structured insights across the entire document.
Implement fine-tuned models. While spacy‘s pre-trained pipelines are great to get started, we can swap in models fine-tuned on domain-specific data (e.g. scientific papers, financial news, legal contracts) to improve analysis quality for particular use cases.
There‘s really no limit to how far you can go in building an intelligent text analysis application by chaining together NLP building blocks. The best part is that libraries like spacy and tools like Streamlit make it easy to rapidly experiment with new capabilities!
Deployment with Hugging Face Spaces
To make our app accessible to the world, we can use the free deployment option from Hugging Face Spaces. Just create a Spaces account, make a new Space, and push your app.py file along with a requirements.txt containing the Python dependencies (streamlit,spacy, etc.)
Spaces will give you a public URL where anyone can access and use your app. It‘s an easy way to share your work and let others benefit from your text analysis tools!
Check out this live demo of the sentiment analyzer app we built: [link to Spaces app]
Conclusion
In this post, we saw how to combine the spacy library for natural language processing, Streamlit for building interactive web apps, and Hugging Face Spaces for deployment and sharing.
Specifically, we walked through building a text analysis app to perform sentiment analysis, named entity recognition, and subjectivity scoring on user-provided text. We took a closer look at how spacy‘s sentiment analysis works under the covers, and discussed some ways to expand the app‘s capabilities.
The complete source code for this project is available on GitHub: [link to repo]
I encourage you to clone the repo and experiment with adding your own features and analysis tools. The possibilities are endless, and the combination of spacy, Streamlit, and Hugging Face makes it fun and simple to build powerful NLP apps.
Feel free to reach out if you have any questions or want to share what you‘ve built. Happy text analyzing!