Spaces:
Sleeping
Sleeping
Create .app.py
Browse files
.app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
from collections import Counter
|
5 |
+
|
6 |
+
# Load the English language model
|
7 |
+
nlp = spacy.load("en_core_web_sm")
|
8 |
+
|
9 |
+
def process_text(text):
|
10 |
+
# Process the text using the spaCy pipeline
|
11 |
+
doc = nlp(text)
|
12 |
+
|
13 |
+
# Extract the named entities
|
14 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
15 |
+
|
16 |
+
# Count the occurrences of each entity type
|
17 |
+
entity_counts = Counter([ent[1] for ent in entities])
|
18 |
+
|
19 |
+
# Visualize the named entities using displacy
|
20 |
+
ent_html = displacy.render(doc, style="ent")
|
21 |
+
|
22 |
+
return ent_html, dict(entity_counts)
|
23 |
+
|
24 |
+
def main():
|
25 |
+
st.set_page_config(page_title="Named Entity Extraction")
|
26 |
+
st.title("Named Entity Extraction")
|
27 |
+
st.write("This app uses spaCy to extract named entities from the given text and visualize them.")
|
28 |
+
|
29 |
+
text = st.text_area("Text to Process", height=200, placeholder="Enter the text you want to analyze")
|
30 |
+
|
31 |
+
if st.button("Process Text"):
|
32 |
+
ent_html, entity_counts = process_text(text)
|
33 |
+
st.markdown(ent_html, unsafe_allow_html=True)
|
34 |
+
st.subheader("Entity Type Counts")
|
35 |
+
st.dataframe(entity_counts, use_container_width=True)
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
main()
|