Paula Leonova commited on
Commit
633f6ea
1 Parent(s): 0945896

Nest loading spinners

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