inclusive-ml commited on
Commit
d37d1de
1 Parent(s): 32bd940

initial commit

Browse files
Files changed (2) hide show
  1. app.py +116 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 numpy as np
7
+ st.set_page_config(page_title="NLP Prototype")
8
+ st.title("Natural Language Processing Prototype")
9
+ st.write("_This web application is intended for educational use, please do not upload any sensitive information._")
10
+ st.subheader("__Which natural language processing task would you like to try?__")
11
+ st.write("- __Sentiment Analysis:__ Identifying whether a piece of text has a positive or negative sentiment.")
12
+ st.write("- __Named Entity Recognition:__ Identifying all geopolitical entities, organizations, people, locations, or dates in a body of text.")
13
+ st.write("- __Text Classification:__ Placing a piece of text into one or more categories.")
14
+ st.write("- __Text Summarization:__ Condensing larger bodies of text into smaller bodies of text.")
15
+ option = st.selectbox('Please select from the list',('','Sentiment Analysis','Named Entity Recognition', 'Text Classification','Text Summarization'))
16
+ @st.cache(allow_output_mutation=True, show_spinner=False)
17
+ def Loading_Model_1():
18
+ sum2 = pipeline("summarization",framework="pt")
19
+ return sum2
20
+ @st.cache(allow_output_mutation=True, show_spinner=False)
21
+ def Loading_Model_2():
22
+ class1 = pipeline("zero-shot-classification",framework="pt")
23
+ return class1
24
+ @st.cache(allow_output_mutation=True, show_spinner=False)
25
+ def Loading_Model_3():
26
+ sentiment = pipeline("sentiment-analysis", framework="pt")
27
+ return sentiment
28
+ @st.cache(allow_output_mutation=True, show_spinner=False)
29
+ def Loading_Model_4():
30
+ nlp = spacy.load('en_core_web_sm')
31
+ return nlp
32
+ @st.cache(allow_output_mutation=True)
33
+ def entRecognizer(entDict, typeEnt):
34
+ entList = [ent for ent in entDict if entDict[ent] == typeEnt]
35
+ return entList
36
+ def plot_result(top_topics, scores):
37
+ top_topics = np.array(top_topics)
38
+ scores = np.array(scores)
39
+ scores *= 100
40
+ fig = px.bar(x=scores, y=top_topics, orientation='h',
41
+ labels={'x': 'Probability', 'y': 'Category'},
42
+ text=scores,
43
+ range_x=(0,115),
44
+ title='Top Predictions',
45
+ color=np.linspace(0,1,len(scores)),
46
+ color_continuous_scale="Bluered")
47
+ fig.update(layout_coloraxis_showscale=False)
48
+ fig.update_traces(texttemplate='%{text:0.1f}%', textposition='outside')
49
+ st.plotly_chart(fig)
50
+ with st.spinner(text="Please wait for the models to load. This should take approximately 60 seconds."):
51
+ sum2 = Loading_Model_1()
52
+ class1 = Loading_Model_2()
53
+ sentiment = Loading_Model_3()
54
+ nlp = Loading_Model_4()
55
+ if option == 'Text Classification':
56
+ cat1 = st.text_input('Enter each possible category name (separated by a comma). Maximum 5 categories.')
57
+ text = st.text_area('Enter Text Below:', height=200)
58
+ submit = st.button('Generate')
59
+ if submit:
60
+ st.subheader("Classification Results:")
61
+ labels1 = cat1.strip().split(',')
62
+ result = class1(text, candidate_labels=labels1)
63
+ cat1name = result['labels'][0]
64
+ cat1prob = result['scores'][0]
65
+ st.write('Category: {} | Probability: {:.1f}%'.format(cat1name,(cat1prob*100)))
66
+ plot_result(result['labels'][::-1][-10:], result['scores'][::-1][-10:])
67
+
68
+ if option == 'Text Summarization':
69
+ max_lengthy = st.slider('Maximum summary length (words)', min_value=30, max_value=150, value=60, step=10)
70
+ num_beamer = st.slider('Speed vs quality of summary (1 is fastest)', min_value=1, max_value=8, value=4, step=1)
71
+ text = st.text_area('Enter Text Below (maximum 800 words):', height=300)
72
+ submit = st.button('Generate')
73
+ if submit:
74
+ st.subheader("Summary:")
75
+ with st.spinner(text="This may take a moment..."):
76
+ 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)
77
+ text2 =summWords[0]["summary_text"]
78
+ st.write(text2)
79
+ if option == 'Sentiment Analysis':
80
+ text = st.text_area('Enter Text Below:', height=200)
81
+ submit = st.button('Generate')
82
+ if submit:
83
+ st.subheader("Sentiment:")
84
+ result = sentiment(text)
85
+ sent = result[0]['label']
86
+ cert = result[0]['score']
87
+ st.write('Text Sentiment: {} | Probability: {:.1f}%'.format(sent,(cert*100)))
88
+ if option == 'Named Entity Recognition':
89
+ text = st.text_area('Enter Text Below:', height=300)
90
+ submit = st.button('Generate')
91
+ if submit:
92
+ entities = []
93
+ entityLabels = []
94
+ doc = nlp(text)
95
+ for ent in doc.ents:
96
+ entities.append(ent.text)
97
+ entityLabels.append(ent.label_)
98
+ entDict = dict(zip(entities, entityLabels))
99
+ entOrg = entRecognizer(entDict, "ORG")
100
+ entPerson = entRecognizer(entDict, "PERSON")
101
+ entDate = entRecognizer(entDict, "DATE")
102
+ entGPE = entRecognizer(entDict, "GPE")
103
+ entLoc = entRecognizer(entDict, "LOC")
104
+ options = {"ents": ["ORG", "GPE", "PERSON", "LOC", "DATE"]}
105
+ HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
106
+
107
+ st.subheader("List of Named Entities:")
108
+ st.write("Geopolitical Entities (GPE): " + str(entGPE))
109
+ st.write("People (PERSON): " + str(entPerson))
110
+ st.write("Organizations (ORG): " + str(entOrg))
111
+ st.write("Dates (DATE): " + str(entDate))
112
+ st.write("Locations (LOC): " + str(entLoc))
113
+ st.subheader("Original Text with Entities Highlighted")
114
+ html = displacy.render(doc, style="ent", options=options)
115
+ html = html.replace("\n", " ")
116
+ st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch==1.7.1
2
+ numpy==1.19.1
3
+ plotly==4.14.3
4
+ streamlit>=0.76.0
5
+ spacy==3.0.0
6
+ transformers==4.3.2
7
+ en_core_web_sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.0.0/en_core_web_sm-3.0.0.tar.gz