czyoung commited on
Commit
ff96b8f
·
verified ·
1 Parent(s): 6fc8451

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +230 -231
app.py CHANGED
@@ -390,246 +390,245 @@ else:
390
 
391
  if currFile is None and len(st.session_state.results) > 0 and len(st.session_state.results[0]) > 0:
392
  st.write("Select a file to view from the sidebar")
393
- else:
394
- try:
395
- st.session_state.resetResult = False
396
- currFileIndex = file_names.index(currFile)
397
-
398
- if len(st.session_state.results) > currFileIndex and len(st.session_state.summaries) > currFileIndex and len(st.session_state.results[currFileIndex]) > 0:
399
 
400
- # Handle
401
- currSpeakerList, currAnnotation, currTotalTime = st.session_state.results[currFileIndex]
402
- speakerNames = currAnnotation.labels()
403
-
404
- # Update other categories
405
- unusedSpeakers = st.session_state.unusedSpeakers[currFileIndex]
406
- categorySelections = st.session_state["categorySelect"][currFileIndex]
407
- for i,category in enumerate(st.session_state.categories):
408
- speakerSet = categorySelections[i]
409
- st.sidebar.multiselect(category,
410
- speakerSet+unusedSpeakers,
411
- default=speakerSet,
412
- key=f"multiselect_{category}",
413
- on_change=updateCategoryOptions,
414
- args=(currFileIndex,))
415
- st.sidebar.button(f"Remove {category}",key=f"remove_{category}",on_click=removeCategory,args=(i,))
416
-
417
-
418
-
419
- newCategory = st.sidebar.text_input('Add category', key='categoryInput',on_change=addCategory)
420
-
421
- df4_dict = {}
422
- nameList = st.session_state.categories
423
- extraNames = []
424
- valueList = [0 for i in range(len(nameList))]
425
- extraValues = []
426
-
427
- for i,speakerSet in enumerate(categorySelections):
428
- valueList[i] += su.sumTimes(currAnnotation.subset(speakerSet))
429
-
430
- for sp in unusedSpeakers:
431
- extraNames.append(sp)
432
- extraValues.append(su.sumTimes(currAnnotation.subset([sp])))
 
 
 
 
 
433
 
434
-
435
- df4_dict = {
436
- "names": nameList+extraNames,
437
- "values": valueList+extraValues,
438
- }
439
- df4 = pd.DataFrame(data=df4_dict)
440
- df4.name = "df4"
441
- st.session_state.summaries[currFileIndex]["df4"] = df4
442
- st.info(st.session_state.summaries)
443
- df2 = st.session_state.summaries[currFileIndex]["df2"]
444
- df3 = st.session_state.summaries[currFileIndex]["df3"]
445
- df4 = st.session_state.summaries[currFileIndex]["df4"]
446
- df5 = st.session_state.summaries[currFileIndex]["df5"]
447
- speakers_dataFrame = st.session_state.summaries[currFileIndex]["speakers_dataFrame"]
448
- currDF = speakers_dataFrame
449
- speakers_times = st.session_state.summaries[currFileIndex]["speakers_times"]
450
-
451
- # generate plotting window
452
- fig1 = go.Figure()
453
- fig1.add_trace(go.Pie(values=df3["values"],labels=df3["names"]))
454
- fig2 = go.Figure()
455
- fig2.add_trace(go.Pie(values=df4["values"],labels=df4["names"]))
456
- fig3_1 = px.sunburst(df5,
457
- branchvalues = 'total',
458
- names = "labels",
459
- ids = "ids",
460
- parents = "parents",
461
- values = "percentiles",
462
- custom_data=['labels','valueStrings','percentiles','parentNames','parentPercentiles'],
463
- color = 'labels',
464
- )
465
- fig3_1.update_traces(
466
- hovertemplate="<br>".join([
467
- '<b>%{customdata[0]}</b>',
468
- 'Duration: %{customdata[1]}s',
469
- 'Percentage of Total: %{customdata[2]:.2f}%',
470
- 'Parent: %{customdata[3]}',
471
- 'Percentage of Parent: %{customdata[4]:.2f}%'
472
- ])
473
- )
474
- fig3 = px.treemap(df5,
475
- branchvalues = "total",
476
  names = "labels",
 
477
  parents = "parents",
478
- ids="ids",
479
  values = "percentiles",
480
  custom_data=['labels','valueStrings','percentiles','parentNames','parentPercentiles'],
481
- color='labels',
482
  )
483
- fig3.update_traces(
484
- hovertemplate="<br>".join([
485
- '<b>%{customdata[0]}</b>',
486
- 'Duration: %{customdata[1]}s',
487
- 'Percentage of Total: %{customdata[2]:.2f}%',
488
- 'Parent: %{customdata[3]}',
489
- 'Percentage of Parent: %{customdata[4]:.2f}%'
490
- ])
491
- )
492
- st.plotly_chart(fig1, use_container_width=True)
493
- st.plotly_chart(fig2, use_container_width=True)
494
- st.plotly_chart(fig3_1, use_container_width=True)
495
- st.plotly_chart(fig3, use_container_width=True)
496
-
497
-
498
- fig_la = px.timeline(speakers_dataFrame, x_start="Start", x_end="Finish", y="Resource", color="Resource")
499
- fig_la.update_yaxes(autorange="reversed")
500
-
501
- hMax = int(currTotalTime//3600)
502
- mMax = int(currTotalTime%3600//60)
503
- sMax = int(currTotalTime%60)
504
- msMax = int(currTotalTime*1000000%1000000)
505
- timeMax = dt.time(hMax,mMax,sMax,msMax)
506
-
507
- fig_la.update_layout(
508
- xaxis_tickformatstops = [
509
- dict(dtickrange=[None, 1000], value="%H:%M:%S.%L"),
510
- dict(dtickrange=[1000, None], value="%H:%M:%S")
511
- ],
512
- xaxis=dict(
513
- range=[dt.datetime.combine(dt.date.today(), dt.time.min),dt.datetime.combine(dt.date.today(), timeMax)]
514
- ),
515
- xaxis_title="Time",
516
- yaxis_title="Speaker",
517
- legend_title=None
518
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
 
520
- st.plotly_chart(fig_la, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
 
522
- fig2_la = px.bar(df2, x="values", y="names", color="names", orientation='h',
523
- custom_data=["names","values"])
524
- fig2_la.update_xaxes(ticksuffix="%")
525
- fig2_la.update_yaxes(autorange="reversed")
526
- fig2_la.update_layout(
527
- xaxis_title="Percentage Time Spoken",
528
- yaxis_title="Speaker",
529
- legend_title=None
530
-
531
- )
532
- fig2_la.update_traces(
533
- hovertemplate="<br>".join([
534
- '<b>%{customdata[0]}</b>',
535
- 'Percentage of Time: %{customdata[1]:.2f}%'
536
- ])
537
- )
538
- st.plotly_chart(fig2_la, use_container_width=True)
539
-
540
- except ValueError:
541
- pass
542
-
543
- if len(st.session_state.results) > 0:
544
- with st.spinner(text='Processing summary results...'):
545
- fileNames = st.session_state.file_names
546
- results = []
547
- indices = []
548
- for i, resultTuple in enumerate(st.session_state.results):
549
- if len(resultTuple) == 2:
550
- fileNames.append(resultTuple[0])
551
- results.append(resultTuple[1])
552
- indices.append(i)
553
- st.info(f'filenames : {fileNames}')
554
- st.info(f'indices : {indices}')
555
- st.info(f'results : {results}')
556
- if len(indices) > 1:
557
-
558
- df6_dict = {
559
- "files":fileNames,
560
- }
561
- allCategories = copy.deepcopy(st.session_state.categories)
562
- for i in indices:
563
- currSpeakerList, currAnnotation, currTotalTime = st.session_state.results[i]
564
- categorySelections = st.session_state["categorySelect"][i]
565
- catSummary,extraCats = su.calcCategories(currAnnotation,categorySelections)
566
- st.session_state.summaries[i]["categories"] = (catSummary,extraCats)
567
- for extra in extraCats:
568
- df6_dict[extra] = []
569
- if extra not in allCategories:
570
- allCategories.append(extra)
571
 
572
-
573
- for category in st.session_state.categories:
574
- df6_dict[category] = []
575
- for i in indices:
576
- summary, extras = st.session_state.summaries[i]["categories"]
577
- theseCategories = st.session_state.categories + extras
578
- for j, timeSlots in enumerate(summary):
579
- df6_dict[theseCategories[j]].append(sum([t.duration for _,t in timeSlots])/st.session_state.results[i][2])
580
- for category in allCategories:
581
- if category not in theseCategories:
582
- df6_dict[category].append(0)
583
- df6 = pd.DataFrame(df6_dict)
584
- summFig = px.bar(df6, x="files", y=allCategories)
585
- st.plotly_chart(summFig, use_container_width=True)
586
 
587
-
588
- voiceNames = ["No Voice","One Voice","Multi Voice"]
589
- df7_dict = {
590
- "files":fileNames,
591
- }
592
- for category in voiceNames:
593
- df7_dict[category] = []
594
- for resultID,summary in enumerate(st.session_state.summaries):
595
- partialDf = summary["df5"]
596
- for i in range(len(voiceNames)):
597
- df7_dict[voiceNames[i]].append(partialDf["percentiles"][i])
598
- df7 = pd.DataFrame(df7_dict)
599
- sorted_df7 = df7.sort_values(by=['One Voice', 'Multi Voice'])
600
- summFig2 = px.bar(sorted_df7, x="files", y=["One Voice","Multi Voice","No Voice",])
601
- st.plotly_chart(summFig2, use_container_width=True)
602
- sorted_df7_3 = df7.sort_values(by=['Multi Voice','One Voice'])
603
- summFig3 = px.bar(sorted_df7_3, x="files", y=["One Voice","Multi Voice","No Voice",])
604
- st.plotly_chart(summFig3, use_container_width=True)
605
- sorted_df7_4 = df7.sort_values(by=['No Voice', 'Multi Voice'],ascending=False)
606
- summFig4 = px.bar(sorted_df7_4, x="files", y=["One Voice","Multi Voice","No Voice",])
607
- st.plotly_chart(summFig4, use_container_width=True)
608
-
609
-
610
-
611
- old = '''userid = st.text_input("user id:", "Guest")
612
- colorPref = st.text_input("Favorite color?", "None")
613
- radio = st.radio('Pick one:', ['Left','Right'])
614
- selection = st.selectbox('Select', [1,2,3])
615
- if st.button("Upload Files to Dataset"):
616
- save_data({"color":colorPref,"direction":radio,"number":selection},
617
- file_paths,
618
- userid)
619
- st.success('I think it worked!')
620
- '''
621
- @st.cache_data
622
- def convert_df(df):
623
- return df.to_csv(index=False).encode('utf-8')
624
-
625
-
626
- if currDF is not None:
627
- csv = convert_df(currDF)
628
 
629
- st.download_button(
630
- "Press to Download analysis data",
631
- csv,
632
- "file.csv",
633
- "text/csv",
634
- key='download-csv'
635
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
 
391
  if currFile is None and len(st.session_state.results) > 0 and len(st.session_state.results[0]) > 0:
392
  st.write("Select a file to view from the sidebar")
 
 
 
 
 
 
393
 
394
+ st.session_state.resetResult = False
395
+ currFileIndex = file_names.index(currFile)
396
+
397
+ if len(st.session_state.results) > currFileIndex and len(st.session_state.summaries) > currFileIndex and len(st.session_state.results[currFileIndex]) > 0:
398
+
399
+ # Handle
400
+ currSpeakerList, currAnnotation, currTotalTime = st.session_state.results[currFileIndex]
401
+ speakerNames = currAnnotation.labels()
402
+
403
+ # Update other categories
404
+ unusedSpeakers = st.session_state.unusedSpeakers[currFileIndex]
405
+ categorySelections = st.session_state["categorySelect"][currFileIndex]
406
+ for i,category in enumerate(st.session_state.categories):
407
+ speakerSet = categorySelections[i]
408
+ st.sidebar.multiselect(category,
409
+ speakerSet+unusedSpeakers,
410
+ default=speakerSet,
411
+ key=f"multiselect_{category}",
412
+ on_change=updateCategoryOptions,
413
+ args=(currFileIndex,))
414
+ st.sidebar.button(f"Remove {category}",key=f"remove_{category}",on_click=removeCategory,args=(i,))
415
+
416
+
417
+
418
+ newCategory = st.sidebar.text_input('Add category', key='categoryInput',on_change=addCategory)
419
+
420
+ df4_dict = {}
421
+ nameList = st.session_state.categories
422
+ extraNames = []
423
+ valueList = [0 for i in range(len(nameList))]
424
+ extraValues = []
425
+
426
+ for i,speakerSet in enumerate(categorySelections):
427
+ valueList[i] += su.sumTimes(currAnnotation.subset(speakerSet))
428
+
429
+ for sp in unusedSpeakers:
430
+ extraNames.append(sp)
431
+ extraValues.append(su.sumTimes(currAnnotation.subset([sp])))
432
 
433
+
434
+ df4_dict = {
435
+ "names": nameList+extraNames,
436
+ "values": valueList+extraValues,
437
+ }
438
+ df4 = pd.DataFrame(data=df4_dict)
439
+ df4.name = "df4"
440
+ st.session_state.summaries[currFileIndex]["df4"] = df4
441
+ st.info(st.session_state.summaries)
442
+ df2 = st.session_state.summaries[currFileIndex]["df2"]
443
+ df3 = st.session_state.summaries[currFileIndex]["df3"]
444
+ df4 = st.session_state.summaries[currFileIndex]["df4"]
445
+ df5 = st.session_state.summaries[currFileIndex]["df5"]
446
+ speakers_dataFrame = st.session_state.summaries[currFileIndex]["speakers_dataFrame"]
447
+ currDF = speakers_dataFrame
448
+ speakers_times = st.session_state.summaries[currFileIndex]["speakers_times"]
449
+
450
+ # generate plotting window
451
+ fig1 = go.Figure()
452
+ fig1.add_trace(go.Pie(values=df3["values"],labels=df3["names"]))
453
+ fig2 = go.Figure()
454
+ fig2.add_trace(go.Pie(values=df4["values"],labels=df4["names"]))
455
+ fig3_1 = px.sunburst(df5,
456
+ branchvalues = 'total',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
  names = "labels",
458
+ ids = "ids",
459
  parents = "parents",
 
460
  values = "percentiles",
461
  custom_data=['labels','valueStrings','percentiles','parentNames','parentPercentiles'],
462
+ color = 'labels',
463
  )
464
+ fig3_1.update_traces(
465
+ hovertemplate="<br>".join([
466
+ '<b>%{customdata[0]}</b>',
467
+ 'Duration: %{customdata[1]}s',
468
+ 'Percentage of Total: %{customdata[2]:.2f}%',
469
+ 'Parent: %{customdata[3]}',
470
+ 'Percentage of Parent: %{customdata[4]:.2f}%'
471
+ ])
472
+ )
473
+ fig3 = px.treemap(df5,
474
+ branchvalues = "total",
475
+ names = "labels",
476
+ parents = "parents",
477
+ ids="ids",
478
+ values = "percentiles",
479
+ custom_data=['labels','valueStrings','percentiles','parentNames','parentPercentiles'],
480
+ color='labels',
481
+ )
482
+ fig3.update_traces(
483
+ hovertemplate="<br>".join([
484
+ '<b>%{customdata[0]}</b>',
485
+ 'Duration: %{customdata[1]}s',
486
+ 'Percentage of Total: %{customdata[2]:.2f}%',
487
+ 'Parent: %{customdata[3]}',
488
+ 'Percentage of Parent: %{customdata[4]:.2f}%'
489
+ ])
490
+ )
491
+ st.plotly_chart(fig1, use_container_width=True)
492
+ st.plotly_chart(fig2, use_container_width=True)
493
+ st.plotly_chart(fig3_1, use_container_width=True)
494
+ st.plotly_chart(fig3, use_container_width=True)
495
+
496
+
497
+ fig_la = px.timeline(speakers_dataFrame, x_start="Start", x_end="Finish", y="Resource", color="Resource")
498
+ fig_la.update_yaxes(autorange="reversed")
499
+
500
+ hMax = int(currTotalTime//3600)
501
+ mMax = int(currTotalTime%3600//60)
502
+ sMax = int(currTotalTime%60)
503
+ msMax = int(currTotalTime*1000000%1000000)
504
+ timeMax = dt.time(hMax,mMax,sMax,msMax)
505
+
506
+ fig_la.update_layout(
507
+ xaxis_tickformatstops = [
508
+ dict(dtickrange=[None, 1000], value="%H:%M:%S.%L"),
509
+ dict(dtickrange=[1000, None], value="%H:%M:%S")
510
+ ],
511
+ xaxis=dict(
512
+ range=[dt.datetime.combine(dt.date.today(), dt.time.min),dt.datetime.combine(dt.date.today(), timeMax)]
513
+ ),
514
+ xaxis_title="Time",
515
+ yaxis_title="Speaker",
516
+ legend_title=None
517
+ )
518
+
519
+ st.plotly_chart(fig_la, use_container_width=True)
520
+
521
+ fig2_la = px.bar(df2, x="values", y="names", color="names", orientation='h',
522
+ custom_data=["names","values"])
523
+ fig2_la.update_xaxes(ticksuffix="%")
524
+ fig2_la.update_yaxes(autorange="reversed")
525
+ fig2_la.update_layout(
526
+ xaxis_title="Percentage Time Spoken",
527
+ yaxis_title="Speaker",
528
+ legend_title=None
529
 
530
+ )
531
+ fig2_la.update_traces(
532
+ hovertemplate="<br>".join([
533
+ '<b>%{customdata[0]}</b>',
534
+ 'Percentage of Time: %{customdata[1]:.2f}%'
535
+ ])
536
+ )
537
+ st.plotly_chart(fig2_la, use_container_width=True)
538
+
539
+ except ValueError:
540
+ pass
541
+
542
+ if len(st.session_state.results) > 0:
543
+ with st.spinner(text='Processing summary results...'):
544
+ fileNames = st.session_state.file_names
545
+ results = []
546
+ indices = []
547
+ for i, resultTuple in enumerate(st.session_state.results):
548
+ if len(resultTuple) == 2:
549
+ fileNames.append(resultTuple[0])
550
+ results.append(resultTuple[1])
551
+ indices.append(i)
552
+ st.info(f'filenames : {fileNames}')
553
+ st.info(f'indices : {indices}')
554
+ st.info(f'results : {results}')
555
+ if len(indices) > 1:
556
 
557
+ df6_dict = {
558
+ "files":fileNames,
559
+ }
560
+ allCategories = copy.deepcopy(st.session_state.categories)
561
+ for i in indices:
562
+ currSpeakerList, currAnnotation, currTotalTime = st.session_state.results[i]
563
+ categorySelections = st.session_state["categorySelect"][i]
564
+ catSummary,extraCats = su.calcCategories(currAnnotation,categorySelections)
565
+ st.session_state.summaries[i]["categories"] = (catSummary,extraCats)
566
+ for extra in extraCats:
567
+ df6_dict[extra] = []
568
+ if extra not in allCategories:
569
+ allCategories.append(extra)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
 
571
+
572
+ for category in st.session_state.categories:
573
+ df6_dict[category] = []
574
+ for i in indices:
575
+ summary, extras = st.session_state.summaries[i]["categories"]
576
+ theseCategories = st.session_state.categories + extras
577
+ for j, timeSlots in enumerate(summary):
578
+ df6_dict[theseCategories[j]].append(sum([t.duration for _,t in timeSlots])/st.session_state.results[i][2])
579
+ for category in allCategories:
580
+ if category not in theseCategories:
581
+ df6_dict[category].append(0)
582
+ df6 = pd.DataFrame(df6_dict)
583
+ summFig = px.bar(df6, x="files", y=allCategories)
584
+ st.plotly_chart(summFig, use_container_width=True)
585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586
 
587
+ voiceNames = ["No Voice","One Voice","Multi Voice"]
588
+ df7_dict = {
589
+ "files":fileNames,
590
+ }
591
+ for category in voiceNames:
592
+ df7_dict[category] = []
593
+ for resultID,summary in enumerate(st.session_state.summaries):
594
+ partialDf = summary["df5"]
595
+ for i in range(len(voiceNames)):
596
+ df7_dict[voiceNames[i]].append(partialDf["percentiles"][i])
597
+ df7 = pd.DataFrame(df7_dict)
598
+ sorted_df7 = df7.sort_values(by=['One Voice', 'Multi Voice'])
599
+ summFig2 = px.bar(sorted_df7, x="files", y=["One Voice","Multi Voice","No Voice",])
600
+ st.plotly_chart(summFig2, use_container_width=True)
601
+ sorted_df7_3 = df7.sort_values(by=['Multi Voice','One Voice'])
602
+ summFig3 = px.bar(sorted_df7_3, x="files", y=["One Voice","Multi Voice","No Voice",])
603
+ st.plotly_chart(summFig3, use_container_width=True)
604
+ sorted_df7_4 = df7.sort_values(by=['No Voice', 'Multi Voice'],ascending=False)
605
+ summFig4 = px.bar(sorted_df7_4, x="files", y=["One Voice","Multi Voice","No Voice",])
606
+ st.plotly_chart(summFig4, use_container_width=True)
607
+
608
+
609
+
610
+ old = '''userid = st.text_input("user id:", "Guest")
611
+ colorPref = st.text_input("Favorite color?", "None")
612
+ radio = st.radio('Pick one:', ['Left','Right'])
613
+ selection = st.selectbox('Select', [1,2,3])
614
+ if st.button("Upload Files to Dataset"):
615
+ save_data({"color":colorPref,"direction":radio,"number":selection},
616
+ file_paths,
617
+ userid)
618
+ st.success('I think it worked!')
619
+ '''
620
+ @st.cache_data
621
+ def convert_df(df):
622
+ return df.to_csv(index=False).encode('utf-8')
623
+
624
+
625
+ if currDF is not None:
626
+ csv = convert_df(currDF)
627
+
628
+ st.download_button(
629
+ "Press to Download analysis data",
630
+ csv,
631
+ "file.csv",
632
+ "text/csv",
633
+ key='download-csv'
634
+ )