Spaces:
Runtime error
Runtime error
inclusive-ml commited on
Commit ·
9dbdd10
1
Parent(s): 5d45578
ner only
Browse files
app.py
CHANGED
|
@@ -8,21 +8,9 @@ st.set_page_config(page_title="Named Entity Recognition")
|
|
| 8 |
st.title("Named Entity Recognition")
|
| 9 |
st.write("_This web application is intended for educational use, please do not upload any sensitive information._")
|
| 10 |
st.write("- __Named Entity Recognition:__ Identifying all geopolitical entities, organizations, people, locations, or dates in a body of text.")
|
| 11 |
-
|
| 12 |
-
@st.cache(allow_output_mutation=True, show_spinner=False)
|
| 13 |
-
def Loading_Model_1():
|
| 14 |
-
sum2 = pipeline("summarization",framework="pt")
|
| 15 |
-
return sum2
|
| 16 |
-
@st.cache(allow_output_mutation=True, show_spinner=False)
|
| 17 |
-
def Loading_Model_2():
|
| 18 |
-
class1 = pipeline("zero-shot-classification",framework="pt")
|
| 19 |
-
return class1
|
| 20 |
-
@st.cache(allow_output_mutation=True, show_spinner=False)
|
| 21 |
-
def Loading_Model_3():
|
| 22 |
-
sentiment = pipeline("sentiment-analysis", framework="pt")
|
| 23 |
-
return sentiment
|
| 24 |
@st.cache(allow_output_mutation=True, show_spinner=False)
|
| 25 |
-
def
|
| 26 |
nlp = spacy.load('en_core_web_sm')
|
| 27 |
return nlp
|
| 28 |
@st.cache(allow_output_mutation=True)
|
|
@@ -43,70 +31,35 @@ def plot_result(top_topics, scores):
|
|
| 43 |
fig.update(layout_coloraxis_showscale=False)
|
| 44 |
fig.update_traces(texttemplate='%{text:0.1f}%', textposition='outside')
|
| 45 |
st.plotly_chart(fig)
|
|
|
|
| 46 |
with st.spinner(text="Please wait for the models to load. This should take approximately 60 seconds."):
|
| 47 |
-
|
| 48 |
-
class1 = Loading_Model_2()
|
| 49 |
-
sentiment = Loading_Model_3()
|
| 50 |
-
nlp = Loading_Model_4()
|
| 51 |
-
if option == 'Text Classification':
|
| 52 |
-
cat1 = st.text_input('Enter each possible category name (separated by a comma). Maximum 5 categories.')
|
| 53 |
-
text = st.text_area('Enter Text Below:', height=200)
|
| 54 |
-
submit = st.button('Generate')
|
| 55 |
-
if submit:
|
| 56 |
-
st.subheader("Classification Results:")
|
| 57 |
-
labels1 = cat1.strip().split(',')
|
| 58 |
-
result = class1(text, candidate_labels=labels1)
|
| 59 |
-
cat1name = result['labels'][0]
|
| 60 |
-
cat1prob = result['scores'][0]
|
| 61 |
-
st.write('Category: {} | Probability: {:.1f}%'.format(cat1name,(cat1prob*100)))
|
| 62 |
-
plot_result(result['labels'][::-1][-10:], result['scores'][::-1][-10:])
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
sent = result[0]['label']
|
| 82 |
-
cert = result[0]['score']
|
| 83 |
-
st.write('Text Sentiment: {} | Probability: {:.1f}%'.format(sent,(cert*100)))
|
| 84 |
-
if option == 'Named Entity Recognition':
|
| 85 |
-
text = st.text_area('Enter Text Below:', height=300)
|
| 86 |
-
submit = st.button('Generate')
|
| 87 |
-
if submit:
|
| 88 |
-
entities = []
|
| 89 |
-
entityLabels = []
|
| 90 |
-
doc = nlp(text)
|
| 91 |
-
for ent in doc.ents:
|
| 92 |
-
entities.append(ent.text)
|
| 93 |
-
entityLabels.append(ent.label_)
|
| 94 |
-
entDict = dict(zip(entities, entityLabels))
|
| 95 |
-
entOrg = entRecognizer(entDict, "ORG")
|
| 96 |
-
entPerson = entRecognizer(entDict, "PERSON")
|
| 97 |
-
entDate = entRecognizer(entDict, "DATE")
|
| 98 |
-
entGPE = entRecognizer(entDict, "GPE")
|
| 99 |
-
entLoc = entRecognizer(entDict, "LOC")
|
| 100 |
-
options = {"ents": ["ORG", "GPE", "PERSON", "LOC", "DATE"]}
|
| 101 |
-
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 8 |
st.title("Named Entity Recognition")
|
| 9 |
st.write("_This web application is intended for educational use, please do not upload any sensitive information._")
|
| 10 |
st.write("- __Named Entity Recognition:__ Identifying all geopolitical entities, organizations, people, locations, or dates in a body of text.")
|
| 11 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@st.cache(allow_output_mutation=True, show_spinner=False)
|
| 13 |
+
def Loading_NLP():
|
| 14 |
nlp = spacy.load('en_core_web_sm')
|
| 15 |
return nlp
|
| 16 |
@st.cache(allow_output_mutation=True)
|
|
|
|
| 31 |
fig.update(layout_coloraxis_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)
|