Spaces:
Runtime error
Runtime error
inclusive-ml
commited on
Commit
·
10332be
1
Parent(s):
4c89d7a
classification only
Browse files
app.py
CHANGED
|
@@ -4,35 +4,16 @@ 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="
|
| 8 |
-
st.title("
|
| 9 |
st.write("_This web application is intended for educational use, please do not upload any sensitive information._")
|
| 10 |
-
st.
|
| 11 |
-
|
| 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
|
| 22 |
class1 = pipeline("zero-shot-classification",framework="pt")
|
| 23 |
return class1
|
| 24 |
-
|
| 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)
|
|
@@ -47,70 +28,18 @@ def plot_result(top_topics, scores):
|
|
| 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 |
-
|
| 69 |
-
|
| 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 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
| 4 |
from spacy import displacy
|
| 5 |
import plotly.express as px
|
| 6 |
import numpy as np
|
| 7 |
+
st.set_page_config(page_title="Text Classification'")
|
| 8 |
+
st.title("Text Classification'")
|
| 9 |
st.write("_This web application is intended for educational use, please do not upload any sensitive information._")
|
| 10 |
+
st.write("Placing a piece of text into one or more categories.")
|
| 11 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@st.cache(allow_output_mutation=True, show_spinner=False)
|
| 13 |
+
def Loading_Classifier():
|
| 14 |
class1 = pipeline("zero-shot-classification",framework="pt")
|
| 15 |
return class1
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def plot_result(top_topics, scores):
|
| 18 |
top_topics = np.array(top_topics)
|
| 19 |
scores = np.array(scores)
|
|
|
|
| 28 |
fig.update(layout_coloraxis_showscale=False)
|
| 29 |
fig.update_traces(texttemplate='%{text:0.1f}%', textposition='outside')
|
| 30 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
with st.spinner(text="Please wait for the models to load. This could take up to 60 seconds."):
|
| 33 |
+
class1 = Loading_Classifier()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
cat1 = st.text_input('Enter each possible category name (separated by a comma). Maximum 5 categories.')
|
| 36 |
+
text = st.text_area('Enter Text Below:', height=200)
|
| 37 |
+
submit = st.button('Generate')
|
| 38 |
+
if submit:
|
| 39 |
+
st.subheader("Classification Results:")
|
| 40 |
+
labels1 = cat1.strip().split(',')
|
| 41 |
+
result = class1(text, candidate_labels=labels1)
|
| 42 |
+
cat1name = result['labels'][0]
|
| 43 |
+
cat1prob = result['scores'][0]
|
| 44 |
+
st.write('Category: {} | Probability: {:.1f}%'.format(cat1name,(cat1prob*100)))
|
| 45 |
+
plot_result(result['labels'][::-1][-10:], result['scores'][::-1][-10:])
|