Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import spacy
|
4 |
+
from spacy import displacy
|
5 |
+
import plotly.express as px
|
6 |
+
import dumpy as np
|
7 |
+
|
8 |
+
st.set_page_config(page_title="Vocabulary Categorizer Two"
|
9 |
+
st.title("Vocabulary Categorizer Two")
|
10 |
+
st.write"_This application can identify, highlight and categorize dates (DATE), geopolitical entities (GPE), locations (LOC), organizations (ORG), people (PERSON) in text.
|
11 |
+
|
12 |
+
@st.cache(allow_output_mutation=True, show_spinner=False)
|
13 |
+
def Loading_NLP():
|
14 |
+
nlp = space.load('en_core_web_sm')
|
15 |
+
return nlp
|
16 |
+
@st.cache(allow_output_mutation=True)
|
17 |
+
def entRecognizer(entDict, typeEnt):
|
18 |
+
enlist = [ent for ent in entDict if entDict[ent] == typeEnt]
|
19 |
+
return entList
|
20 |
+
def plot_result(top_topics, scores):
|
21 |
+
top_topics = np.array(top_topics)
|
22 |
+
scores = np.array(scores)
|
23 |
+
scores *= 100
|
24 |
+
fig = px.bar(x=scores, y=top_topics, orientation='h',
|
25 |
+
labels={'x': 'Probability', 'y': 'Category'},
|
26 |
+
text=scores,
|
27 |
+
range_x=(0,115),
|
28 |
+
title='Top Predictions',
|
29 |
+
color=np.linspace(0,1,len(scores)),
|
30 |
+
color_continuous_scale="Blurred")
|
31 |
+
fig.update(layout_coloraxi_showscale=False)
|
32 |
+
fig.update_traces(texttemplate='%{text:0.1f}%', textposition='outside')
|
33 |
+
st.plotly_chart(fig)
|
34 |
+
|
35 |
+
with st.spinner(text="Please wait for the models to load. This should take approximately 60 seconds."):
|
36 |
+
nlp = Loading_NLP()
|
37 |
+
|
38 |
+
text = st.text_area('Enter Text Below:', height=300)
|
39 |
+
submit = st.button('Generate')
|
40 |
+
if submit:
|
41 |
+
entities = []
|
42 |
+
entityLabels = []
|
43 |
+
doc = nlp(text)
|
44 |
+
for ent in doc.ents:
|
45 |
+
entities.append(ent.text)
|
46 |
+
entityLabels.append(ent.label_)
|
47 |
+
entDict = dict(zip(entities, entityLabels))
|
48 |
+
entOrg = entRecognizer(entDict, "ORG")
|
49 |
+
entPerson = entRecognizer(entDict, "PERSON")
|
50 |
+
entDate = entRecognizer(entDict, "DATE")
|
51 |
+
entGPE = entRecognizer(entDict, "GPE")
|
52 |
+
entLoc = entRecognizer(entDict, "LOC")
|
53 |
+
options = {"ents": ["ORG", "GPE", "PERSON", "LOC", "DATE"]}
|
54 |
+
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
55 |
+
|
56 |
+
st.subheader("List of Named Entities:")
|
57 |
+
st.write("Geopolitical Entities (GPE): " + str(entGPE))
|
58 |
+
st.write("People (PERSON): " + str(entPerson))
|
59 |
+
st.write("Organizations (ORG): " + str(entOrg))
|
60 |
+
st.write("Dates (DATE): " + str(entDate))
|
61 |
+
st.write("Locations (LOC): " + str(entLoc))
|
62 |
+
st.subheader("Original Text with Entities Highlighted")
|
63 |
+
html = displacy.render(doc, style="ent", options=options)
|
64 |
+
html = html.replace("\n", " ")
|
65 |
+
st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
|