Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
|
5 |
+
# Load spaCy model
|
6 |
+
nlp = spacy.load("en_core_web_sm")
|
7 |
+
|
8 |
+
def visualize_entities(doc):
|
9 |
+
# Create a list of (start, end, label) tuples for the named entities
|
10 |
+
entities = [(ent.start_char, ent.end_char, ent.label_) for ent in doc.ents]
|
11 |
+
|
12 |
+
# Highlight named entities in the input text
|
13 |
+
html = displacy.render(doc, style="ent", options={"ents": entities})
|
14 |
+
st.markdown(html, unsafe_allow_html=True)
|
15 |
+
|
16 |
+
def filter_entities(entities, selected_labels):
|
17 |
+
if not selected_labels:
|
18 |
+
return entities
|
19 |
+
|
20 |
+
filtered_entities = []
|
21 |
+
for entity, label in entities:
|
22 |
+
if label in selected_labels:
|
23 |
+
filtered_entities.append((entity, label))
|
24 |
+
return filtered_entities
|
25 |
+
|
26 |
+
def main():
|
27 |
+
st.title("Named Entity Recognition App")
|
28 |
+
st.write("Enter a text and get named entities!")
|
29 |
+
|
30 |
+
user_input = st.text_area("Enter text:", height=200)
|
31 |
+
visualize = st.checkbox("Visualize Entities")
|
32 |
+
filter_labels = st.multiselect("Filter Entities by Label:", options=["PERSON", "ORG", "GPE", "DATE"])
|
33 |
+
|
34 |
+
if st.button("Analyze"):
|
35 |
+
doc = nlp(user_input)
|
36 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
37 |
+
|
38 |
+
if visualize:
|
39 |
+
visualize_entities(doc)
|
40 |
+
|
41 |
+
filtered_entities = filter_entities(entities, filter_labels)
|
42 |
+
|
43 |
+
st.write("Named Entities:")
|
44 |
+
for entity, label in filtered_entities:
|
45 |
+
st.write(f"Text: {entity}, Entity: {label}")
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
main()
|