inclusive-ml commited on
Commit
9dbdd10
·
1 Parent(s): 5d45578
Files changed (1) hide show
  1. app.py +31 -78
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
- option = st.selectbox('Please select from the list',('','Named Entity Recognition'))
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 Loading_Model_4():
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
- sum2 = Loading_Model_1()
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
- if option == 'Text Summarization':
65
- max_lengthy = st.slider('Maximum summary length (words)', min_value=30, max_value=150, value=60, step=10)
66
- num_beamer = st.slider('Speed vs quality of summary (1 is fastest)', min_value=1, max_value=8, value=4, step=1)
67
- text = st.text_area('Enter Text Below (maximum 800 words):', height=300)
68
- submit = st.button('Generate')
69
- if submit:
70
- st.subheader("Summary:")
71
- with st.spinner(text="This may take a moment..."):
72
- summWords = sum2(text, max_length=max_lengthy, min_length=15, num_beams=num_beamer, do_sample=True, early_stopping=True, repetition_penalty=1.5, length_penalty=1.5)
73
- text2 =summWords[0]["summary_text"]
74
- st.write(text2)
75
- if option == 'Sentiment Analysis':
76
- text = st.text_area('Enter Text Below:', height=200)
77
- submit = st.button('Generate')
78
- if submit:
79
- st.subheader("Sentiment:")
80
- result = sentiment(text)
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
- st.subheader("List of Named Entities:")
104
- st.write("Geopolitical Entities (GPE): " + str(entGPE))
105
- st.write("People (PERSON): " + str(entPerson))
106
- st.write("Organizations (ORG): " + str(entOrg))
107
- st.write("Dates (DATE): " + str(entDate))
108
- st.write("Locations (LOC): " + str(entLoc))
109
- st.subheader("Original Text with Entities Highlighted")
110
- html = displacy.render(doc, style="ent", options=options)
111
- html = html.replace("\n", " ")
112
- st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
 
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)