czyoung commited on
Commit
31597a6
·
verified ·
1 Parent(s): 1ef22d4

Add multi-file demo option

Browse files

Demo button renamed. Added multi-file demo button. Adjusted "Analyze all" button to a toggle. Multi-file demo begins by artificially loading local rttms and then uses the "Analyze all" pathway to process them.

Files changed (1) hide show
  1. app.py +108 -68
app.py CHANGED
@@ -475,70 +475,6 @@ if uploaded_file_paths is not None and len(uploaded_file_paths) > 0:
475
  file_names = st.session_state.file_names
476
  file_paths_dict = st.session_state.file_paths # dict {fname: path}
477
 
478
- if len(file_names) == 0:
479
- st.text("Upload file(s) to enable analysis")
480
- else:
481
- if st.button("Analyze All New Audio",key=f"button_all"):
482
- if len(file_names) == 0:
483
- st.error('Upload file(s) first!')
484
- else:
485
- print("Start analyzing")
486
- start_time = time.time()
487
- totalFiles = len(file_names)
488
- for i, fname in enumerate(file_names):
489
- fpath = file_paths_dict.get(fname, "")
490
- if st.session_state.results.get(fname):
491
- continue
492
- if fpath.lower().endswith('.txt'):
493
- with st.spinner(text=f'Loading Demo File {i+1} of {totalFiles}'):
494
- speakerList, annotations = su.loadAudioTXT(fpath)
495
- printV(annotations,4)
496
- totalSeconds = 0
497
- for segment in annotations.itersegments():
498
- if segment.end > totalSeconds:
499
- totalSeconds = segment.end
500
- st.session_state.results[fname] = (annotations, totalSeconds)
501
- st.session_state.summaries[fname] = {}
502
- st.session_state.unusedSpeakers[fname] = list(annotations.labels())
503
- elif fpath.lower().endswith('.rttm'):
504
- with st.spinner(text=f'Loading File {i+1} of {totalFiles}'):
505
- speakerList, annotations = su.loadAudioRTTM(fpath)
506
- printV(annotations,4)
507
- totalSeconds = 0
508
- for segment in annotations.itersegments():
509
- if segment.end > totalSeconds:
510
- totalSeconds = segment.end
511
- st.session_state.results[fname] = (annotations, totalSeconds)
512
- st.session_state.summaries[fname] = {}
513
- st.session_state.unusedSpeakers[fname] = list(annotations.labels())
514
- elif fpath.lower().endswith('.csv'):
515
- with st.spinner(text=f'Loading File {i+1} of {totalFiles}'):
516
- speakerList, annotations = su.loadAudioCSV(fpath)
517
- printV(annotations,4)
518
- totalSeconds = 0
519
- for segment in annotations.itersegments():
520
- if segment.end > totalSeconds:
521
- totalSeconds = segment.end
522
- st.session_state.results[fname] = (annotations, totalSeconds)
523
- st.session_state.summaries[fname] = {}
524
- st.session_state.unusedSpeakers[fname] = list(annotations.labels())
525
- else:
526
- with st.spinner(text=f'Processing File {i+1} of {totalFiles}'):
527
- annotations, totalSeconds, waveform, sample_rate = processFile(fpath)
528
- print(f"Finished processing {fpath}")
529
- st.session_state.results[fname] = (annotations, totalSeconds)
530
- st.session_state.summaries[fname] = {}
531
- st.session_state.unusedSpeakers[fname] = list(annotations.labels())
532
- with st.spinner(text=f'Generating speaker clips for File {i+1} of {totalFiles}'):
533
- generate_speaker_clips(annotations, waveform, sample_rate, fname)
534
- del waveform
535
- print(f"Speaker clips generated for {fpath}")
536
- with st.spinner(text=f'Analyzing File {i+1} of {totalFiles}'):
537
- analyze(fname)
538
- print(f"Finished analyzing {fpath}")
539
- print(f"Took {time.time() - start_time} seconds to analyze {totalFiles} files!")
540
- st.success(f"Took {time.time() - start_time} seconds to analyze {totalFiles} files!")
541
-
542
  class FakeUpload:
543
  def __init__(self,filepath):
544
  self.path = filepath
@@ -546,7 +482,7 @@ class FakeUpload:
546
  ]
547
  demoPath = "sample.rttm"
548
  isDemo = False
549
- if st.sidebar.button("Load Demo Example"):
550
  demoName = demoPath.split('/')[-1]
551
  start_time = time.time()
552
  if demoName not in st.session_state.file_names:
@@ -569,12 +505,116 @@ if st.sidebar.button("Load Demo Example"):
569
  st.session_state.results[demoName] = (annotations, totalSeconds)
570
  st.session_state.summaries[demoName] = {}
571
  st.session_state.unusedSpeakers[demoName] = list(annotations.labels())
572
- with st.spinner(text=f'Analyzing Demo Data'):
573
- analyze(demoName)
574
  st.success(f"Took {time.time() - start_time} seconds to analyze the demo file!")
575
  st.session_state.select_currFile = demoName
576
  isDemo = True
577
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
  currFile = st.sidebar.selectbox('Current File', file_names, on_change=updateMultiSelect, key="select_currFile")
579
 
580
  if isDemo:
 
475
  file_names = st.session_state.file_names
476
  file_paths_dict = st.session_state.file_paths # dict {fname: path}
477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  class FakeUpload:
479
  def __init__(self,filepath):
480
  self.path = filepath
 
482
  ]
483
  demoPath = "sample.rttm"
484
  isDemo = False
485
+ if st.sidebar.button("Single File Demo"):
486
  demoName = demoPath.split('/')[-1]
487
  start_time = time.time()
488
  if demoName not in st.session_state.file_names:
 
505
  st.session_state.results[demoName] = (annotations, totalSeconds)
506
  st.session_state.summaries[demoName] = {}
507
  st.session_state.unusedSpeakers[demoName] = list(annotations.labels())
508
+ with st.spinner(text=f'Analyzing Demo Data'):
509
+ analyze(demoName)
510
  st.success(f"Took {time.time() - start_time} seconds to analyze the demo file!")
511
  st.session_state.select_currFile = demoName
512
  isDemo = True
513
+
514
+ multiFileDemoPaths = ["audioSamples/media-afc-cal-afc1986022_sr01a05.rttm","audioSamples/media-afc-cal-afc1986022_sr34a01.rttm","audioSamples/media-afc-cal-afc1986022_sr14b02.rttm",
515
+ "audioSamples/media-afc-cal-afc1986022_sr52a02.rttm","audioSamples/media-afc-cal-afc1986022_sr14b01.rttm"]
516
+ # TODO: prepare audio for playback of audio
517
+ multiFileAudioPaths = ["audioSamples/media-afc-cal-afc1986022_sr01a05.mp3","audioSamples/media-afc-cal-afc1986022_sr34a01.mp3","audioSamples/media-afc-cal-afc1986022_sr14b02.mp3",
518
+ "audioSamples/media-afc-cal-afc1986022_sr52a02.mp3","audioSamples/media-afc-cal-afc1986022_sr14b01.mp3"]
519
+
520
+ if st.sidebar.button("Multiple Files Demo"):
521
+ for demoPath in multiFileDemoPaths:
522
+ demoName = demoPath.split('/')[-1]
523
+ start_time = time.time()
524
+ if demoName not in st.session_state.file_names:
525
+ st.session_state.file_names.append(demoName)
526
+ st.session_state.file_paths[demoName] = demoPath
527
+ st.session_state.results.setdefault(demoName, [])
528
+ st.session_state.summaries.setdefault(demoName, {})
529
+ st.session_state.unusedSpeakers.setdefault(demoName, [])
530
+ st.session_state.categorySelect.setdefault(demoName, [[] for _ in st.session_state.categories])
531
+ st.session_state.speakerRenames.setdefault(demoName, {})
532
+ st.session_state.speakerClips.setdefault(demoName, {})
533
+ file_names = st.session_state.file_names
534
+
535
+ with st.spinner(text=f'Loading Demo Sample'):
536
+ speakerList, annotations = su.loadAudioRTTM(demoPath)
537
+ totalSeconds = 0
538
+ for segment in annotations.itersegments():
539
+ if segment.end > totalSeconds:
540
+ totalSeconds = segment.end
541
+ st.session_state.results[demoName] = (annotations, totalSeconds)
542
+ st.session_state.summaries[demoName] = {}
543
+ st.session_state.unusedSpeakers[demoName] = list(annotations.labels())
544
+ # TODO: Remove if not necessary
545
+ #st.session_state.select_currFile = demoName
546
+ isDemo = True
547
+ analyzeAllToggle = True
548
+
549
+ analyzeAllToggle = False
550
+ if len(file_names) == 0:
551
+ st.text("Upload file(s) to enable analysis")
552
+ else:
553
+ if st.button("Analyze All New Audio",key=f"button_all"):
554
+ if len(file_names) == 0:
555
+ st.error('Upload file(s) first!')
556
+ else:
557
+ analyzeAllToggle = True
558
+
559
+ if analyzeAllToggle == True:
560
+ print("Start analyzing")
561
+ start_time = time.time()
562
+ totalFiles = len(file_names)
563
+ for i, fname in enumerate(file_names):
564
+ fpath = file_paths_dict.get(fname, "")
565
+ if st.session_state.results.get(fname):
566
+ continue
567
+ if fpath.lower().endswith('.txt'):
568
+ with st.spinner(text=f'Loading Demo File {i+1} of {totalFiles}'):
569
+ speakerList, annotations = su.loadAudioTXT(fpath)
570
+ printV(annotations,4)
571
+ totalSeconds = 0
572
+ for segment in annotations.itersegments():
573
+ if segment.end > totalSeconds:
574
+ totalSeconds = segment.end
575
+ st.session_state.results[fname] = (annotations, totalSeconds)
576
+ st.session_state.summaries[fname] = {}
577
+ st.session_state.unusedSpeakers[fname] = list(annotations.labels())
578
+ elif fpath.lower().endswith('.rttm'):
579
+ with st.spinner(text=f'Loading File {i+1} of {totalFiles}'):
580
+ speakerList, annotations = su.loadAudioRTTM(fpath)
581
+ printV(annotations,4)
582
+ totalSeconds = 0
583
+ for segment in annotations.itersegments():
584
+ if segment.end > totalSeconds:
585
+ totalSeconds = segment.end
586
+ st.session_state.results[fname] = (annotations, totalSeconds)
587
+ st.session_state.summaries[fname] = {}
588
+ st.session_state.unusedSpeakers[fname] = list(annotations.labels())
589
+ elif fpath.lower().endswith('.csv'):
590
+ with st.spinner(text=f'Loading File {i+1} of {totalFiles}'):
591
+ speakerList, annotations = su.loadAudioCSV(fpath)
592
+ printV(annotations,4)
593
+ totalSeconds = 0
594
+ for segment in annotations.itersegments():
595
+ if segment.end > totalSeconds:
596
+ totalSeconds = segment.end
597
+ st.session_state.results[fname] = (annotations, totalSeconds)
598
+ st.session_state.summaries[fname] = {}
599
+ st.session_state.unusedSpeakers[fname] = list(annotations.labels())
600
+ else:
601
+ with st.spinner(text=f'Processing File {i+1} of {totalFiles}'):
602
+ annotations, totalSeconds, waveform, sample_rate = processFile(fpath)
603
+ print(f"Finished processing {fpath}")
604
+ st.session_state.results[fname] = (annotations, totalSeconds)
605
+ st.session_state.summaries[fname] = {}
606
+ st.session_state.unusedSpeakers[fname] = list(annotations.labels())
607
+ with st.spinner(text=f'Generating speaker clips for File {i+1} of {totalFiles}'):
608
+ generate_speaker_clips(annotations, waveform, sample_rate, fname)
609
+ del waveform
610
+ print(f"Speaker clips generated for {fpath}")
611
+ with st.spinner(text=f'Analyzing File {i+1} of {totalFiles}'):
612
+ analyze(fname)
613
+ print(f"Finished analyzing {fpath}")
614
+ print(f"Took {time.time() - start_time} seconds to analyze {totalFiles} files!")
615
+ st.success(f"Took {time.time() - start_time} seconds to analyze {totalFiles} files!")
616
+ analyzeAllToggle = False
617
+
618
  currFile = st.sidebar.selectbox('Current File', file_names, on_change=updateMultiSelect, key="select_currFile")
619
 
620
  if isDemo: