Paula Leonova
commited on
Commit
·
dae3587
1
Parent(s):
5e78355
Move spinner to earlier in app
Browse files
app.py
CHANGED
|
@@ -31,67 +31,64 @@ with st.form(key='my_form'):
|
|
| 31 |
labels = list(set([x.strip() for x in labels.strip().split(',') if len(x.strip()) > 0]))
|
| 32 |
submit_button = st.form_submit_button(label='Submit')
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
summarizer = load_summary_model()
|
| 36 |
-
classifier = load_model()
|
| 37 |
|
| 38 |
if submit_button:
|
| 39 |
if len(labels) == 0:
|
| 40 |
st.write('Enter some text and at least one possible topic to see predictions.')
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
unsafe_allow_html = True
|
| 94 |
-
)
|
| 95 |
-
st.dataframe(data2)
|
| 96 |
-
|
| 97 |
-
st.success('All Done!')
|
|
|
|
| 31 |
labels = list(set([x.strip() for x in labels.strip().split(',') if len(x.strip()) > 0]))
|
| 32 |
submit_button = st.form_submit_button(label='Submit')
|
| 33 |
|
| 34 |
+
with st.spinner('Loading pretrained models...'):
|
| 35 |
+
summarizer = load_summary_model()
|
| 36 |
+
classifier = load_model()
|
| 37 |
+
st.success('All Done!')
|
| 38 |
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if submit_button:
|
| 41 |
if len(labels) == 0:
|
| 42 |
st.write('Enter some text and at least one possible topic to see predictions.')
|
| 43 |
|
| 44 |
+
# For each body of text, create text chunks of a certain token size required for the transformer
|
| 45 |
+
nested_sentences = create_nest_sentences(document = text_input, token_max_length = 1024)
|
| 46 |
+
|
| 47 |
+
summary = []
|
| 48 |
+
st.markdown("### Text Chunk & Summaries")
|
| 49 |
+
st.markdown("Breaks up the original text into sections with complete sentences totaling \
|
| 50 |
+
less than 1024 tokens, a requirement for the summarizer.")
|
| 51 |
+
|
| 52 |
+
# For each chunk of sentences (within the token max), generate a summary
|
| 53 |
+
for n in range(0, len(nested_sentences)):
|
| 54 |
+
text_chunk = " ".join(map(str, nested_sentences[n]))
|
| 55 |
+
st.markdown(f"###### Chunk {n+1}/{len(nested_sentences)}" )
|
| 56 |
+
st.markdown(text_chunk)
|
| 57 |
+
|
| 58 |
+
chunk_summary = summarizer_gen(summarizer, sequence=text_chunk, maximum_tokens = 300, minimum_tokens = 20)
|
| 59 |
+
summary.append(chunk_summary)
|
| 60 |
+
st.markdown("###### Partial Summary")
|
| 61 |
+
st.markdown(chunk_summary)
|
| 62 |
+
# Combine all the summaries into a list and compress into one document, again
|
| 63 |
+
final_summary = " \n".join(list(summary))
|
| 64 |
+
|
| 65 |
+
# final_summary = summarizer_gen(summarizer, sequence=text_input, maximum_tokens = 30, minimum_tokens = 100)
|
| 66 |
+
st.markdown("### Combined Summary")
|
| 67 |
+
st.markdown(final_summary)
|
| 68 |
+
|
| 69 |
+
topics, scores = classifier_zero(classifier, sequence=final_summary, labels=labels, multi_class=True)
|
| 70 |
+
# st.markdown("### Top Label Predictions: Combined Summary")
|
| 71 |
+
# plot_result(topics[::-1][:], scores[::-1][:])
|
| 72 |
+
# st.markdown("### Download Data")
|
| 73 |
+
data = pd.DataFrame({'label': topics, 'scores_from_summary': scores})
|
| 74 |
+
# st.dataframe(data)
|
| 75 |
+
# coded_data = base64.b64encode(data.to_csv(index = False). encode ()).decode()
|
| 76 |
+
# st.markdown(
|
| 77 |
+
# f'<a href="data:file/csv;base64, {coded_data}" download = "data.csv">Download Data</a>',
|
| 78 |
+
# unsafe_allow_html = True
|
| 79 |
+
# )
|
| 80 |
+
|
| 81 |
+
st.markdown("### Top Label Predictions: Summary & Full Text")
|
| 82 |
+
topics_ex_text, scores_ex_text = classifier_zero(classifier, sequence=example_text, labels=labels, multi_class=True)
|
| 83 |
+
plot_dual_bar_chart(topics, scores, topics_ex_text, scores_ex_text)
|
| 84 |
+
|
| 85 |
+
data_ex_text = pd.DataFrame({'label': topics_ex_text, 'scores_from_full_text': scores_ex_text})
|
| 86 |
+
data2 = pd.merge(data, data_ex_text, on = ['label'])
|
| 87 |
+
st.markdown("### Data Table")
|
| 88 |
+
|
| 89 |
+
coded_data = base64.b64encode(data2.to_csv(index = False). encode ()).decode()
|
| 90 |
+
st.markdown(
|
| 91 |
+
f'<a href="data:file/csv;base64, {coded_data}" download = "data.csv">Click here to download the data</a>',
|
| 92 |
+
unsafe_allow_html = True
|
| 93 |
+
)
|
| 94 |
+
st.dataframe(data2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|