fadzwan's picture
Create .app.py
654b7ee verified
import streamlit as st
import spacy
from spacy import displacy
from collections import Counter
# Load the English language model
nlp = spacy.load("en_core_web_sm")
def process_text(text):
# Process the text using the spaCy pipeline
doc = nlp(text)
# Extract the named entities
entities = [(ent.text, ent.label_) for ent in doc.ents]
# Count the occurrences of each entity type
entity_counts = Counter([ent[1] for ent in entities])
# Visualize the named entities using displacy
ent_html = displacy.render(doc, style="ent")
return ent_html, dict(entity_counts)
def main():
st.set_page_config(page_title="Named Entity Extraction")
st.title("Named Entity Extraction")
st.write("This app uses spaCy to extract named entities from the given text and visualize them.")
text = st.text_area("Text to Process", height=200, placeholder="Enter the text you want to analyze")
if st.button("Process Text"):
ent_html, entity_counts = process_text(text)
st.markdown(ent_html, unsafe_allow_html=True)
st.subheader("Entity Type Counts")
st.dataframe(entity_counts, use_container_width=True)
if __name__ == "__main__":
main()