-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstreamlit_web.py
More file actions
702 lines (617 loc) · 23 KB
/
streamlit_web.py
File metadata and controls
702 lines (617 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
"""Web interface"""
import re
import base64
import numpy as np
import os
import pandas as pd
from sklearn.manifold import TSNE
import spacy
import streamlit as st
from textblob import TextBlob
import src.analyzer as az
import src.constants as cts
import src.doc_similarity as ds
import src.get_handler as gh
import src.json_util as ju
import src.markdown as md
import src.summarizer as sz
import src.topic_modeling as tm
import src.visualization as vis
# resources/sample_reflections/lab1, resources/sample_reflections/lab2
# initialize main_df and preprocessed_Df
SPACY_MODEL_NAMES = ["en_core_web_sm", "en_core_web_md"]
preprocessed_df = pd.DataFrame()
main_df = pd.DataFrame()
assignments = None
assign_text = None
stu_id = None
success_msg = None
debug_mode = False
def main():
"""main streamlit function"""
# Title
st.sidebar.title("Welcome to GatorMiner!")
data_retreive_method = st.sidebar.selectbox(
"Choose the data retrieving method",
[
"Local file system",
"AWS",
],
)
if retreive_data(data_retreive_method):
analysis_mode = st.sidebar.selectbox(
"Choose the analysis mode",
[
"Home",
"Frequency Analysis",
"Sentiment Analysis",
"Document Similarity",
"Summary",
"Topic Modeling",
"Interactive",
],
)
if debug_mode:
st.write(main_df)
if analysis_mode == "Home":
landing_src()
else:
if analysis_mode == "Frequency Analysis":
st.title(analysis_mode)
frequency()
elif analysis_mode == "Sentiment Analysis":
st.title(analysis_mode)
sentiment()
elif analysis_mode == "Document Similarity":
st.title(analysis_mode)
doc_sim()
elif analysis_mode == "Summary":
st.title(analysis_mode)
summary()
elif analysis_mode == "Topic Modeling":
st.title(analysis_mode)
tpmodel()
elif analysis_mode == "Interactive":
st.title(analysis_mode)
interactive()
success_msg.empty()
def landing_src():
"""function to load and configurate readme source"""
with open("docs/LANDING_PAGE.md") as landing_file:
landing_src = landing_file.read()
for file in os.listdir("resources/images"):
if file.endswith(".png"):
img_path = f"resources/images/{file}"
with open(img_path, "rb") as f:
img_bin = base64.b64encode(f.read()).decode()
landing_src = landing_src.replace(img_path, f"data:image/png;base64,{img_bin}")
st.markdown(landing_src, unsafe_allow_html=True)
def landing_pg():
"""landing page"""
landing = st.sidebar.selectbox("Welcome", ["Home", "Interactive"])
if landing == "Home":
landing_src()
else:
interactive()
def retreive_data(data_retreive):
"""pipeline to retrieve data from user input to output"""
global preprocessed_df
global main_df
if data_retreive == "Local file system":
input_assignments = st.sidebar.text_input(
"Enter path(s) to markdown documents (seperate by comma)"
)
else:
input_assignments = st.sidebar.text_input(
"Enter assignment names of the markdown \
documents(seperate by comma)"
)
st.sidebar.info(
"You will need to store keys and endpoints in the \
environment variables")
if not input_assignments:
landing_pg()
else:
input_assignments = re.split(r"[;,\s]\s*", input_assignments)
try:
main_df, preprocessed_df = import_data(
data_retreive, input_assignments)
except TypeError:
st.sidebar.warning(
"No data imported. Please check the reflection document input")
landing_src()
else:
global success_msg
success_msg = None
if main_df.empty is not True:
success_msg = st.sidebar.success("Sucessfully Loaded!!")
global assign_id
assign_id = preprocessed_df.columns[0]
global assignments
assignments = st.sidebar.multiselect(
label="Select assignments below:",
options=main_df[assign_id].unique(),
)
global assign_text
assign_text = ", ".join(assignments)
global stu_id
stu_id = preprocessed_df.columns[1]
return True
@st.cache(allow_output_mutation=True)
def load_model(name):
"""load spacy model"""
return spacy.load(name)
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def import_data(data_retreive_method, paths):
"""pipeline to import data from local or aws"""
json_lst = []
if data_retreive_method == "Local file system":
try:
for path in paths:
json_lst.append(md.collect_md(path))
except FileNotFoundError as err:
st.sidebar.text(err)
landing_src()
else:
passbuild = st.sidebar.checkbox(
"Only retreive build success records", value=True)
try:
configs = gh.auth_config()
for path in paths:
response = gh.get_request(path, passbuild, **configs)
json_lst.append(ju.clean_report(response))
except (EnvironmentError, Exception) as err:
st.sidebar.error(err)
landing_src()
# when data is retreived
if json_lst:
raw_df = pd.DataFrame()
for item in json_lst:
single_df = pd.DataFrame(item)
raw_df = pd.concat([raw_df, single_df]).fillna("")
tidy_df = df_preprocess(raw_df)
return tidy_df, raw_df
def df_preprocess(df):
"""build and preprocess (combine, normalize, tokenize) text"""
# filter out first two columns -- non-report content
cols = df.columns[2:]
# combining text into combined column
df["combined"] = df[cols].apply(
lambda row: "\n".join(row.values.astype(str)), axis=1
)
# normalize
df[cts.NORMAL] = df["combined"].apply(lambda row: az.normalize(row))
# tokenize
df[cts.TOKEN] = df[cts.NORMAL].apply(lambda row: az.tokenize(row))
return df
def frequency():
"""main function for frequency analysis"""
# Create expandable container to show the description for frequency analyzer
freq_des = md.read_file('docs/frequency-analysis/frequency-analysis.md')
with st.beta_expander("Frequency Analysis Description"):
st.write(freq_des)
freq_type = st.sidebar.selectbox(
"Type of frequency analysis", ["Overall", "Student", "Question"]
)
if freq_type == "Overall":
freq_range = st.sidebar.slider(
"Select a range of Most frequent words", 1, 50, value=25
)
st.sidebar.success(
'To continue see individual frequency analysis select "Student"'
)
st.header(f"Overall most frequent words in **{assign_text}**")
overall_freq(freq_range)
elif freq_type == "Student":
freq_range = st.sidebar.slider(
"Select a range of Most frequent words", 1, 20, value=10
)
st.header(
f"Most frequent words by individual students in **{assign_text}**"
)
student_freq(freq_range)
elif freq_type == "Question":
freq_range = st.sidebar.slider(
"Select a range of Most frequent words", 1, 20, value=10
)
st.header(
f"Most frequent words in individual questions in **{assign_text}**"
)
question_freq(freq_range)
def overall_freq(freq_range):
"""page fore overall word frequency"""
plots_range = st.sidebar.slider(
"Select the number of plots per row", 1, 5, value=3
)
freq_df = pd.DataFrame(columns=["assignments", "word", "freq"])
# calculate word frequency of each assignments
for item in assignments:
# combined text of the whole assignment
combined_text = " ".join(
main_df[main_df[assign_id] == item][cts.NORMAL]
)
item_df = pd.DataFrame(
az.word_frequency(combined_text, freq_range),
columns=["word", "freq"],
)
item_df["assignments"] = item
freq_df = freq_df.append(item_df)
# plot all the subplots of different assignments
st.altair_chart(
vis.facet_freq_barplot(
freq_df, assignments, "assignments", plots_per_row=plots_range
)
)
freq_overall_des = md.read_file('docs/frequency-analysis/frequency-analysis-overall.md')
with st.beta_expander("Overall Frequency Analysis Description"):
st.write(freq_overall_des)
def student_freq(freq_range):
"""page for individual student's word frequency"""
students = st.multiselect(
label="Select specific students below:",
options=main_df[stu_id].unique(),
)
plots_range = st.sidebar.slider(
"Select the number of plots per row", 1, 5, value=3
)
freq_df = pd.DataFrame(columns=["student", "word", "freq"])
stu_assignment = main_df[
(main_df[stu_id].isin(students))
& main_df[assign_id].isin(assignments)
]
if len(students) != 0:
for student in students:
for item in assignments:
individual_freq = az.word_frequency(
stu_assignment[
(stu_assignment[assign_id] == item)
& (stu_assignment[stu_id] == student)
]
.loc[:, ["combined"]]
.to_string(),
freq_range,
)
ind_df = pd.DataFrame(individual_freq, columns=["word", "freq"])
ind_df["assignments"] = item
ind_df["student"] = student
freq_df = freq_df.append(ind_df)
st.altair_chart(
vis.facet_freq_barplot(
freq_df,
students,
"student",
color_column="assignments",
plots_per_row=plots_range,
)
)
freq_student_des = md.read_file('docs/frequency-analysis/frequency-analysis-student.md')
with st.beta_expander("Frequency Analysis for Student Description"):
st.write(freq_student_des)
def question_freq(freq_range):
"""page for individual question's word frequency"""
# drop columns with all na
select_preprocess = preprocessed_df[
preprocessed_df[assign_id].isin(assignments)
].dropna(axis=1, how="all")
questions = st.multiselect(
label="Select specific questions below:",
options=select_preprocess.columns[2:],
)
plots_range = st.sidebar.slider(
"Select the number of plots per row", 1, 5, value=1
)
freq_question_df = pd.DataFrame(columns=["question", "word", "freq"])
select_text = {}
for question in questions:
select_text[question] = main_df[question].to_string(
index=False, na_rep=""
)
question_df = pd.DataFrame(
select_text.items(), columns=["question", "text"]
)
if len(questions) != 0:
for question in questions:
quest_freq = az.word_frequency(
question_df[question_df["question"] == question]
.loc[:, ["text"]]
.to_string(),
freq_range,
)
ind_df = pd.DataFrame(quest_freq, columns=["word", "freq"])
ind_df["question"] = question
freq_question_df = freq_question_df.append(ind_df)
st.altair_chart(
vis.facet_freq_barplot(
freq_question_df,
questions,
"question",
plots_per_row=plots_range,
)
)
freq_question_des = md.read_file('docs/frequency-analysis/frequency-analysis-question.md')
with st.beta_expander("Frequency Analysis for Question Description"):
st.write(freq_question_des)
def sentiment():
"""main function for sentiment analysis"""
# Create expandable container to show description for sentiment analysis
sent_des = md.read_file('docs/sentiment-analysis.md')
with st.beta_expander("Sentiment Analysis Description"):
st.write(sent_des)
senti_df = main_df.copy(deep=True)
# Initializing the new columns with a numpy array, so the entire series is returned
senti_df[cts.POSITIVE], senti_df[cts.NEGATIVE] = az.top_polarized_word(senti_df[cts.TOKEN].values)
# calculate overall sentiment from the combined text
senti_df[cts.SENTI] = senti_df["combined"].apply(
lambda x: TextBlob(az.lemmatized_text(x)).sentiment.polarity
)
senti_df = senti_df[senti_df[assign_id].isin(assignments)]
senti_type = st.sidebar.selectbox(
"Type of sentiment analysis", ["Overall", "Student", "Question"]
)
if senti_type == "Overall":
st.sidebar.success(
'To continue see individual sentiment analysis select "Student"'
)
st.header(f"Overall sentiment polarity in **{assign_text}**")
overall_senti(senti_df)
elif senti_type == "Student":
st.header(f"View sentiment by individual students in **{assign_text}**")
student_senti(senti_df)
elif senti_type == "Question":
st.header(
f"View sentiment by individual questions in **{assign_text}**"
)
question_senti(senti_df)
def overall_senti(senti_df):
"""page for overall senti"""
# display line plot when there are multiple assingments
if len(assignments) > 1:
st.altair_chart(vis.stu_senti_lineplot(senti_df, stu_id))
st.altair_chart((vis.senti_combinedplot(senti_df, stu_id)))
def student_senti(input_df):
"""page for display individual student's sentiment"""
students = st.multiselect(
label="Select specific students below:",
options=input_df[stu_id].unique(),
)
plots_range = st.sidebar.slider(
"Select the number of plots per row", 1, 5, value=3
)
df_selected_stu = input_df.loc[input_df[stu_id].isin(students)]
senti_df = pd.DataFrame(
df_selected_stu, columns=[assign_id, stu_id, cts.SENTI]
)
if len(students) != 0:
st.altair_chart(
vis.facet_senti_barplot(
senti_df, students, stu_id, plots_per_row=plots_range
)
)
st.altair_chart(vis.stu_senti_barplot(senti_df, stu_id))
def question_senti(input_df):
"""page for individual question's sentiment"""
select_preprocess = preprocessed_df[
preprocessed_df[assign_id].isin(assignments)
].dropna(axis=1, how="all")
questions = st.multiselect(
label="Select specific questions below:",
options=select_preprocess.columns[2:],
)
select_text = []
for column in questions:
select_text.append(input_df[column].to_string(index=False, na_rep=""))
questions_senti_df = pd.DataFrame(
{"questions": questions, "text": select_text}
)
# calculate overall sentiment from the combined text
questions_senti_df[cts.SENTI] = questions_senti_df["text"].apply(
lambda x: TextBlob(x).sentiment.polarity
)
if len(select_text) != 0:
st.altair_chart(vis.question_senti_barplot(questions_senti_df))
def summary():
"""Display summarization"""
# Create expandable container to show description for summary feature
summ_des = md.read_file('docs/summary.md')
with st.beta_expander("Summary Description"):
st.write(summ_des)
sum_df = preprocessed_df[
preprocessed_df[assign_id].isin(assignments)
].dropna(axis=1, how="all")
for column in preprocessed_df.columns[2:]:
sum_df[column] = preprocessed_df[column].apply(
lambda x: sz.summarize_text(x)
)
st.write(sum_df)
def tpmodel():
"""Display topic modeling"""
# Create expandable container to show description for summary feature
topic_des = md.read_file('docs/topic-modelling.md')
with st.beta_expander("Topic Modelling Description"):
st.write(topic_des)
topic_df = main_df.copy(deep=True)
topic_df = topic_df[topic_df[assign_id].isin(assignments)]
# st.write(topic_df)
tp_type = st.sidebar.selectbox(
"Type of topic modeling analysis", ["Histogram", "Scatter"]
)
topic_range = st.sidebar.slider(
"Select the amount of topics", 1, 10, value=5
)
word_range = st.sidebar.slider(
"Select the amount of words per topic", 1, 10, value=5
)
# topic_df["topics"] = topic_df["tokens"].apply(
# lambda x: tm.topic_model(
# x, NUM_TOPICS=topic_range, NUM_WORDS=word_range)
# )
overall_topic_df, lda_model, corpus = tm.topic_model(
topic_df[cts.TOKEN].tolist(),
num_topics=topic_range,
num_words=word_range,
)
overall_topic_df["Student"] = topic_df[stu_id].tolist()
overall_topic_df[assign_id] = topic_df[assign_id].tolist()
# reorder the column
overall_topic_df = overall_topic_df[
[
assign_id,
"Student",
"Dominant_Topic",
"Topic_Keywords",
"Text",
"Perc_Contribution",
]
]
st.header(f"Overall topics in **{assign_text}**")
if tp_type == "Histogram":
hist_tm(overall_topic_df)
elif tp_type == "Scatter":
# topics = lda_model.show_topics(formatted=False)
scatter_tm(lda_model, corpus, overall_topic_df)
def hist_tm(topic_df):
"""Topic modeling in histogram"""
st.write(topic_df)
st.altair_chart(vis.tp_hist_plot(topic_df))
def scatter_tm(lda_model, corpus, overall_topic_df):
"""Topic modeling in scatter plot"""
topic_weights = []
for i, row_list in enumerate(lda_model[corpus]):
topic_weights.append([w for i, w in row_list[0]])
# Array of topic weights
arr = pd.DataFrame(topic_weights).fillna(0).values
# st.write(arr)
# Keep the well separated points (optional)
arr = arr[np.amax(arr, axis=1) > 0.35]
# st.write(arr)
# Dominant topic number in each doc
topic_num = np.argmax(arr, axis=1)
# st.write(topic_num)
random_state = st.sidebar.slider("Select random_state", 1, 1000, value=500)
angle = st.sidebar.slider("Select angle", 0, 100, value=50)
# tSNE Dimension Reduction
tsne_model = TSNE(
n_components=2,
verbose=1,
random_state=random_state,
angle=angle / 100,
init="pca",
)
tsne_lda = tsne_model.fit_transform(arr)
df_tsne = pd.DataFrame(
{
"x": tsne_lda[:, 0],
"y": tsne_lda[:, 1],
"topic": topic_num,
"topic_num": overall_topic_df["Dominant_Topic"],
}
)
# df_tsne["topic_num"] = overall_topic_df["Dominant_Topic"]
# st.write(df_tsne)
lda_scatter = vis.tp_scatter_plot(df_tsne)
st.altair_chart(lda_scatter)
def doc_sim():
"""Display document similarity"""
# Create expandable container to show description for document similarity analyzer
docs_des = md.read_file('docs/document-similarity.md')
with st.beta_expander("Document Similarity Description"):
st.write(docs_des)
doc_df = main_df.copy(deep=True)
doc_sim_type = st.sidebar.selectbox(
"Type of similarity analysis", ["TF-IDF", "Spacy"]
)
st.header(
f"Similarity between each student's document in **{assign_text}**"
)
if doc_sim_type == "TF-IDF":
tf_idf_sim(doc_df)
elif doc_sim_type == "Spacy":
spacy_sim(doc_df)
def tf_idf_sim(doc_df):
for assignment in assignments:
doc = doc_df[doc_df[assign_id] == assignment].dropna(
axis=1, how="all"
)
pairs = ds.create_pair(doc[stu_id])
# calculate similarity of the docs of the selected author pairs
similarity = [
ds.tfidf_cosine_similarity(
(
doc[doc[stu_id] == pair[0]][cts.NORMAL].values[0],
doc[doc[stu_id] == pair[1]][cts.NORMAL].values[0],
)
)
for pair in pairs
]
df_sim = pd.DataFrame({"pair": pairs, "similarity": similarity})
# Split the pair tuple into two columns for plotting
df_sim[["doc_1", "doc_2"]] = pd.DataFrame(
df_sim["pair"].tolist(), index=df_sim.index
)
st.altair_chart(
vis.doc_sim_heatmap(df_sim).properties(title=assignment)
)
def spacy_sim(doc_df):
spacy_model = st.sidebar.selectbox("Model name", SPACY_MODEL_NAMES)
nlp = load_model(spacy_model)
for assignment in assignments:
doc = doc_df[doc_df[assign_id] == assignment].dropna(
axis=1, how="all"
)
pairs = ds.create_pair(doc[stu_id])
# calculate similarity of the docs of the selected author pairs
similarity = [
ds.spacy_doc_similarity(
nlp,
(
doc[doc[stu_id] == pair[0]][cts.NORMAL].values[0],
doc[doc[stu_id] == pair[1]][cts.NORMAL].values[0],
),
)
for pair in pairs
]
df_sim = pd.DataFrame({"pair": pairs, "similarity": similarity})
# Split the pair tuple into two columns for plotting
df_sim[["doc_1", "doc_2"]] = pd.DataFrame(
df_sim["pair"].tolist(), index=df_sim.index
)
st.altair_chart(
vis.doc_sim_heatmap(df_sim).properties(title=assignment)
)
def interactive():
"""Page to allow nlp analysis from user input"""
# Create expandable container to show description for interactive feature
inter_des = md.read_file('docs/interactive.md')
with st.beta_expander("Interactive Description"):
st.write(inter_des)
input_text = st.text_area("Enter text", "Type here")
token_cb = st.checkbox("Show tokens")
ner_cb = st.checkbox("Show named entities")
sentiment_cb = st.checkbox("Show sentiment")
summary_cb = st.checkbox("Show Summary")
# st.success("Running Analysis")
# if st.button("Analysis"):
if token_cb:
tokens = az.tokenize(input_text)
st.write(tokens)
if ner_cb:
doc = az.get_nlp(input_text)
named_entities = az.named_entity_recognization(input_text)
if len(named_entities) > 0:
html = spacy.displacy.render(doc, style="ent")
# Newlines seem to mess with the rendering
html = html.replace("\n", " ")
HTML_WRAPPER = """<div style="overflow-x: auto; border: 1px solid \
#e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">\
{}</div>"""
st.write(HTML_WRAPPER.format(html), unsafe_allow_html=True)
else:
st.info("No named entity recognized")
if sentiment_cb:
sentiments = TextBlob(az.lemmatized_text(input_text))
st.write(sentiments.sentiment)
if summary_cb:
summaries = sz.summarize_text(input_text)
st.write(summaries)
if __name__ == "__main__":
main()