czyoung commited on
Commit
6a8a763
·
verified ·
1 Parent(s): e895a63

Updated to support multiple files and saving to dataset

Browse files
Files changed (1) hide show
  1. app.py +189 -135
app.py CHANGED
@@ -4,145 +4,199 @@ import numpy as np
4
  import torchaudio
5
  import sonogram_utility as su
6
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  st.title("Lecturer Support Tool")
9
 
10
- uploaded_file = st.file_uploader("Upload an audio of classroom activity to analyze")
11
-
12
- supported_file_types = ('.wav','.mp3','.mp4')
13
-
14
- if uploaded_file is not None:
15
- if not uploaded_file.name.endswith(supported_file_types):
16
- st.error('File must be of type: {}'.format(supported_file_types))
17
- uploaded_file = st.empty()
18
- else:
19
- st.audio(uploaded_file)
20
- if st.button("Analyze Audio"):
21
- if uploaded_file is None:
22
- st.error('Upload a file first!')
23
- else:
24
- # Process
25
- # Pretend to take time as an example
26
- with st.spinner(text='NOT ACTUALLY ANALYZING, JUST A FILLER ANIMATION'):
27
- time.sleep(5)
28
- st.success('Done')
29
-
30
- # RTTM load as filler
31
- speakerList, annotations = su.loadAudioRTTM("24F CHEM1402 Night Class Week 4.rttm")
32
-
33
- # Display breakdowns
34
- #--------------------------------------------------------------------------
35
-
36
- # Prepare data
37
- sortedSpeakerList = sorted([[row for row in speaker if row[1] > 0.25] for speaker in speakerList if len([row for row in speaker if row[1] > 0.25]) > 0],
38
- key=lambda e: min(e)[0])
39
- pred_count = len(sortedSpeakerList)
40
- lecturer_speaker_list,_ = su.twoClassExtendAnnotation(annotations)
41
- lecturer_pred_count = 2
42
- totalSeconds = 9049
43
- lecturer_speaker_times = []
44
- for i,speaker in enumerate(lecturer_speaker_list):
45
- lecturer_speaker_times.append(0)
46
- for timeSection in speaker:
47
- lecturer_speaker_times[i] += timeSection[1]
48
-
49
- all_speaker_times = []
50
- for i,speaker in enumerate(sortedSpeakerList):
51
- all_speaker_times.append(0)
52
- for timeSection in speaker:
53
- all_speaker_times[i] += timeSection[1]
54
-
55
- # Lecturer vs. Audience
56
- #---------------------------------------------------------------------------
57
-
58
- f, ax1 =plt.subplots()
59
-
60
- # Setting Y-axis limits
61
- ax1.set_ylim(0, lecturer_pred_count*5 + 5)
62
-
63
- # Setting X-axis limits
64
- #gnt.set_xlim(0, 160)
65
-
66
- # Setting labels for x-axis and y-axis
67
- ax1.set_title('Recording Results')
68
- ax1.set_xlabel('Minutes since start')
69
- ax1.set_ylabel('Speaker ID')
70
- ax1.spines.top.set_visible(False)
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- # Setting ticks on y-axis (5,10,15,...)
74
- step = 5
75
- ax1.set_yticks(list(range(step,(lecturer_pred_count+1)*step,step)))
76
- # Labelling tickes of y-axis ('1','2','3',...)
77
- pred_tick_list = [1,2]
78
- ax1.set_yticklabels(["Lectuerer","Audience"])
79
- #x_tick_list = range(0,6000,60)
80
- #ax1.set_xticks(x_tick_list)
81
- #ax1.set_xticklabels([str(int(element/60)) for element in x_tick_list])
82
- ax1.tick_params(axis='x', labelrotation=90)
83
- # Setting graph attribute
84
- ax1.grid(True)
85
-
86
- pred_colors = su.colors(lecturer_pred_count)
87
- for j, row in enumerate(lecturer_speaker_list):
88
- ax1.broken_barh(row, ((j+1)*5-1, 3), facecolors =(pred_colors[j]))
89
-
90
- f.set_figheight(5)
91
- f.set_figwidth(15)
92
-
93
- st.pyplot(f)
94
-
95
- st.write("Total length of audio: {}h:{:02d}m:{:02d}s".format(int(totalSeconds/3600),int((totalSeconds%3600)/60),int(totalSeconds%60)))
96
- st.write("Lecturer spoke: {}h:{:02d}m:{:02d}s -> {:.2f}% of time".format(int(lecturer_speaker_times[0]/3600),
97
- int((lecturer_speaker_times[0]%3600)/60),int(lecturer_speaker_times[0]%60),
98
- 100*lecturer_speaker_times[0]/totalSeconds))
99
- st.write("Audience spoke: {}h:{:02d}m:{:02d}s -> {:.2f}% of time".format(int(lecturer_speaker_times[1]/3600),
100
- int((lecturer_speaker_times[1]%3600)/60),int(lecturer_speaker_times[1]%60),
101
- 100*lecturer_speaker_times[1]/totalSeconds))
102
-
103
- # Experimental Speaker Breakdown
104
- #------------------------------------------------------------------------------
105
-
106
- f, ax1 =plt.subplots()
107
-
108
- # Setting Y-axis limits
109
- ax1.set_ylim(0, pred_count*5 + 5)
110
-
111
- # Setting X-axis limits
112
- #gnt.set_xlim(0, 160)
113
-
114
- # Setting labels for x-axis and y-axis
115
- ax1.set_title('Recording Results')
116
- ax1.set_xlabel('Minutes since start')
117
- ax1.set_ylabel('Speaker ID')
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
- # Setting ticks on y-axis (5,10,15,...)
121
- step = 5
122
- ax1.set_yticks(list(range(step,(pred_count+1)*step,step)))
123
- # Labelling tickes of y-axis ('1','2','3',...)
124
- pred_tick_list = range(1,pred_count+1)
125
- ax1.set_yticklabels([str(element) for element in pred_tick_list])
126
- x_tick_list = range(0,6000,60)
127
- ax1.set_xticks(x_tick_list)
128
- ax1.set_xticklabels([str(int(element/60)) for element in x_tick_list])
129
- ax1.tick_params(axis='x', labelrotation=90)
130
- # Setting graph attribute
131
- ax1.grid(True)
132
-
133
- pred_colors = su.colors(pred_count)
134
- for j, row in enumerate(sortedSpeakerList):
135
- ax1.broken_barh(row, ((j+1)*5-1, 3), facecolors =(pred_colors[j]))
136
-
137
- f.set_figheight(5)
138
- f.set_figwidth(15)
139
-
140
- st.pyplot(f)
141
-
142
- st.write("Total length of audio: {}h:{:02d}m:{:02d}s".format(int(totalSeconds/3600),int((totalSeconds%3600)/60),int(totalSeconds%60)))
143
- for i,speaker in enumerate(all_speaker_times):
144
- st.write("Speaker {} spoke: {}h:{:02d}m:{:02d}s -> {:.2f}% of time".format(i,
145
- int(speaker/3600),
146
- int((speaker%3600)/60),
147
- int(speaker%60),
148
- 100*speaker/totalSeconds))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import torchaudio
5
  import sonogram_utility as su
6
  import time
7
+ import ParquetScheduler as ps
8
+ from pathlib import Path
9
+ from typing import Any, Dict, List, Optional, Union
10
+ import copy
11
+
12
+ PARQUET_DATASET_DIR = Path("parquet_dataset")
13
+ PARQUET_DATASET_DIR.mkdir(parents=True,exist_ok=True)
14
+
15
+
16
+ scheduler = ps.ParquetScheduler(repo_id="Sonogram/SampleDataset")
17
+
18
+ def save_data(
19
+ config_dict: Dict[str,str], audio_path: List[str], userid: str,
20
+ ) -> None:
21
+ """Save data, i.e. move audio to a new folder and send paths+config to scheduler."""
22
+
23
+ save_dir = PARQUET_DATASET_DIR / f"{userid}"
24
+ save_dir.mkdir(parents=True, exist_ok=True)
25
+
26
+ data = copy.deepcopy(config_dict)
27
+
28
+ # Add timestamp
29
+ data["timestamp"] = datetime.datetime.utcnow().isoformat()
30
+
31
+ # Copy and add audio
32
+ for i,p in enumerate(audio_paths):
33
+ name = f"{i:03d}"
34
+ dst_path = save_dir / f"{name}{Path(p).suffix}"
35
+ shutil.copyfile(p, dst_path)
36
+ data[f"audio_{name}"] = dst_path
37
+
38
+ # Send to scheduler
39
+ scheduler.append(data)
40
 
41
  st.title("Lecturer Support Tool")
42
 
43
+ uploaded_file_paths = st.file_uploader("Upload an audio of classroom activity to analyze", accept_multiple_files=True)
44
+
45
+ supported_file_types = ('.wav','.mp3','.mp4','.txt')
46
+
47
+ valid_files = []
48
+ audio_tabs = []
49
+
50
+ if uploaded_file_paths is not None:
51
+ # Reset valid_files?
52
+ for uploaded_file in uploaded_file_paths:
53
+ if not uploaded_file.name.endswith(supported_file_types):
54
+ st.error('File must be of type: {}'.format(supported_file_types))
55
+ uploaded_file = None
56
+ else:
57
+ if uploaded_file not in valid_files:
58
+ valid_files.append(uploaded_file)
59
+ audio_tabs = st.tabs([f.name for f in valid_files])
60
+
61
+ for tab in audio_tabs:
62
+ if tab.button("Analyze Audio"):
63
+ if uploaded_file is None:
64
+ tab.error('Upload a file first!')
65
+ else:
66
+ # Process
67
+ # Pretend to take time as an example
68
+ with tab.spinner(text='NOT ACTUALLY ANALYZING, JUST A FILLER ANIMATION'):
69
+ time.sleep(5)
70
+ tab.success('Done')
71
+
72
+ # RTTM load as filler
73
+ speakerList, annotations = su.loadAudioRTTM("24F CHEM1402 Night Class Week 4.rttm")
74
+
75
+ # Display breakdowns
76
+ #--------------------------------------------------------------------------
77
+
78
+ # Prepare data
79
+ sortedSpeakerList = sorted([[row for row in speaker if row[1] > 0.25] for speaker in speakerList if len([row for row in speaker if row[1] > 0.25]) > 0],
80
+ key=lambda e: min(e)[0])
81
+ pred_count = len(sortedSpeakerList)
82
+ lecturer_speaker_list,_ = su.twoClassExtendAnnotation(annotations)
83
+ lecturer_pred_count = 2
84
+ totalSeconds = 9049
85
+ lecturer_speaker_times = []
86
+ for i,speaker in enumerate(lecturer_speaker_list):
87
+ lecturer_speaker_times.append(0)
88
+ for timeSection in speaker:
89
+ lecturer_speaker_times[i] += timeSection[1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ all_speaker_times = []
92
+ for i,speaker in enumerate(sortedSpeakerList):
93
+ all_speaker_times.append(0)
94
+ for timeSection in speaker:
95
+ all_speaker_times[i] += timeSection[1]
96
+
97
+ # Lecturer vs. Audience
98
+ #---------------------------------------------------------------------------
99
+
100
+ f, ax1 =plt.subplots()
101
+
102
+ # Setting Y-axis limits
103
+ ax1.set_ylim(0, lecturer_pred_count*5 + 5)
104
+
105
+ # Setting X-axis limits
106
+ #gnt.set_xlim(0, 160)
107
+
108
+ # Setting labels for x-axis and y-axis
109
+ ax1.set_title('Recording Results')
110
+ ax1.set_xlabel('Minutes since start')
111
+ ax1.set_ylabel('Speaker ID')
112
+ ax1.spines.top.set_visible(False)
113
 
114
+
115
+ # Setting ticks on y-axis (5,10,15,...)
116
+ step = 5
117
+ ax1.set_yticks(list(range(step,(lecturer_pred_count+1)*step,step)))
118
+ # Labelling tickes of y-axis ('1','2','3',...)
119
+ pred_tick_list = [1,2]
120
+ ax1.set_yticklabels(["Lectuerer","Audience"])
121
+ #x_tick_list = range(0,6000,60)
122
+ #ax1.set_xticks(x_tick_list)
123
+ #ax1.set_xticklabels([str(int(element/60)) for element in x_tick_list])
124
+ ax1.tick_params(axis='x', labelrotation=90)
125
+ # Setting graph attribute
126
+ ax1.grid(True)
127
+
128
+ pred_colors = su.colors(lecturer_pred_count)
129
+ for j, row in enumerate(lecturer_speaker_list):
130
+ ax1.broken_barh(row, ((j+1)*5-1, 3), facecolors =(pred_colors[j]))
131
+
132
+ f.set_figheight(5)
133
+ f.set_figwidth(15)
134
+
135
+ tab.pyplot(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
+ tab.write("Total length of audio: {}h:{:02d}m:{:02d}s".format(int(totalSeconds/3600),int((totalSeconds%3600)/60),int(totalSeconds%60)))
138
+ tab.write("Lecturer spoke: {}h:{:02d}m:{:02d}s -> {:.2f}% of time".format(int(lecturer_speaker_times[0]/3600),
139
+ int((lecturer_speaker_times[0]%3600)/60),int(lecturer_speaker_times[0]%60),
140
+ 100*lecturer_speaker_times[0]/totalSeconds))
141
+ tab.write("Audience spoke: {}h:{:02d}m:{:02d}s -> {:.2f}% of time".format(int(lecturer_speaker_times[1]/3600),
142
+ int((lecturer_speaker_times[1]%3600)/60),int(lecturer_speaker_times[1]%60),
143
+ 100*lecturer_speaker_times[1]/totalSeconds))
144
+
145
+ # Experimental Speaker Breakdown
146
+ #------------------------------------------------------------------------------
147
+
148
+ f, ax1 =plt.subplots()
149
+
150
+ # Setting Y-axis limits
151
+ ax1.set_ylim(0, pred_count*5 + 5)
152
+
153
+ # Setting X-axis limits
154
+ #gnt.set_xlim(0, 160)
155
+
156
+ # Setting labels for x-axis and y-axis
157
+ ax1.set_title('Recording Results')
158
+ ax1.set_xlabel('Minutes since start')
159
+ ax1.set_ylabel('Speaker ID')
160
 
161
+
162
+ # Setting ticks on y-axis (5,10,15,...)
163
+ step = 5
164
+ ax1.set_yticks(list(range(step,(pred_count+1)*step,step)))
165
+ # Labelling tickes of y-axis ('1','2','3',...)
166
+ pred_tick_list = range(1,pred_count+1)
167
+ ax1.set_yticklabels([str(element) for element in pred_tick_list])
168
+ x_tick_list = range(0,6000,60)
169
+ ax1.set_xticks(x_tick_list)
170
+ ax1.set_xticklabels([str(int(element/60)) for element in x_tick_list])
171
+ ax1.tick_params(axis='x', labelrotation=90)
172
+ # Setting graph attribute
173
+ ax1.grid(True)
174
+
175
+ pred_colors = su.colors(pred_count)
176
+ for j, row in enumerate(sortedSpeakerList):
177
+ ax1.broken_barh(row, ((j+1)*5-1, 3), facecolors =(pred_colors[j]))
178
+
179
+ f.set_figheight(5)
180
+ f.set_figwidth(15)
181
+
182
+ tab.pyplot(f)
183
+
184
+ tab.write("Total length of audio: {}h:{:02d}m:{:02d}s".format(int(totalSeconds/3600),int((totalSeconds%3600)/60),int(totalSeconds%60)))
185
+ for i,speaker in enumerate(all_speaker_times):
186
+ tab.write("Speaker {} spoke: {}h:{:02d}m:{:02d}s -> {:.2f}% of time".format(i,
187
+ int(speaker/3600),
188
+ int((speaker%3600)/60),
189
+ int(speaker%60),
190
+ 100*speaker/totalSeconds))
191
+
192
+
193
+ userid = st.text_input("user id:", "Guest")
194
+ colorPref = st.text_input("Favorite color?", "None")
195
+ radio = st.radio('Pick one:', ['Left','Right'])
196
+ selection = st.selectbox('Select', [1,2,3])
197
+ if st.button("Upload Files to Dataset"):
198
+ save_data({"color":colorPref,"direction":radio,"number":selection},
199
+ valid_files,
200
+ userid)
201
+ st.success('I think it worked!')
202
+