fpessanha commited on
Commit
d9b07cf
·
1 Parent(s): 1924fb2

Feat: Deal with csv files; remove interaction when no participant ID"

Browse files
Files changed (3) hide show
  1. annotations.csv +1 -4
  2. app.py +51 -32
  3. io_annotations.csv +3 -0
annotations.csv CHANGED
@@ -1,5 +1,2 @@
1
  sample_id,sentence,emotion,confidence,comments
2
- 007-0023,I was mostly hanging out in East Village in the clubs.,Neutral,20,
3
- 007-0034,"And we became friends, and then he told me that his lover had died of AIDS.",Angry,30,
4
- 007-0086,"I mean, given the size of ACT UP, I mean, really, you're talking about a handful.",Neutral,70,
5
- 007-0129,"And why is he like, you know, why are you putting him in yellow face?",Angry,0,
 
1
  sample_id,sentence,emotion,confidence,comments
2
+ 007-0023,I was mostly hanging out in East Village in the clubs.,Neutral,0,
 
 
 
app.py CHANGED
@@ -17,6 +17,12 @@ css = """#myProgress {
17
  border-radius: 2px;
18
  }
19
 
 
 
 
 
 
 
20
  #progressText {
21
  position: absolute;
22
  top: 50%;
@@ -26,13 +32,13 @@ css = """#myProgress {
26
  font-weight: bold;
27
  font-size: 14px;
28
  """
29
-
30
  js_progress_bar = """
31
  function move(n_ann, total_ann) {
 
32
  var elem = document.getElementById("myBar");
33
  elem.style.width = n_ann/total_ann * 100 + "%";
34
  progressText.innerText = 'Completed: ' + n_ann + '/' + total_ann
35
- }
36
  """
37
  # List of all audio files to annotate
38
  file_list = pd.read_excel(os.path.join('combined_annotations.xlsx'))
@@ -53,13 +59,13 @@ def load_example(index):
53
 
54
  # If the user already made an annotation for this example, gradio will return said annotation
55
  previous_annotation = (
56
- annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["SAMPLE ID"], "emotion": '', "confidence": 0,
57
  "comments": ''}
58
  )
59
  return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], current_index['index'] + 1, previous_annotation["comments"])
60
 
61
 
62
- def save_annotation(emotions, confidence, comments):
63
  """Save the annotation for the current example."""
64
 
65
  idx = current_index["index"]
@@ -74,38 +80,48 @@ def save_annotation(emotions, confidence, comments):
74
  else:
75
  annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments]
76
  ann_completed.value += 1
77
- annotations.to_csv("annotations.csv", index=False) # Save to a CSV file
78
 
79
 
80
- def next_example(emotions, confidence, comments):
81
  """Move to the next example."""
82
  if emotions == "Blank":
83
- raise gr.Warning("Please fill out the emotion section")
84
  else:
85
 
86
- save_annotation(emotions, confidence, comments)
87
  if current_index["index"] < len(file_list) - 1:
88
  current_index["index"] += 1
89
  return load_example(current_index["index"])
90
 
91
- def previous_example(emotion, confidence, comments):
92
  """Move to the previous example."""
93
  if emotion.value != "Blank":
94
- save_annotation(emotion, confidence, comments)
95
  if current_index["index"] > 0:
96
  current_index["index"] -= 1
97
  return load_example(current_index["index"])
98
  return load_example(current_index["index"])
99
 
 
 
 
 
100
 
 
 
 
 
 
 
 
 
101
  # ===================
102
  # Gradio Interface
103
  # ===================
104
- audio_path = 'test.mp3'
105
 
106
  with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
107
- participant_id = gr.Textbox(label='What is your participant ID?', interactive = True)
108
- lets_go = gr.Button("Let's go!")
109
 
110
  ann_completed = gr.Number(1, visible=False)
111
  total = gr.Number(total_annotations, visible=False)
@@ -114,60 +130,63 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
114
  gr.HTML("""
115
  <div id="myProgress">
116
  <div id="myBar">
117
- <span id="progressText">Completed: /</span>
118
  </div>
119
  </div>""")
120
 
121
  # Row with audio player
122
  with gr.Row():
123
- audio_player = gr.Audio(value=audio_path, label="Audio", type="filepath", interactive=False, visible=False)
124
 
125
  # Hidden row with corresponding sentence
126
  with gr.Row():
127
  accordion = gr.Accordion(label="Click to see the sentence", open=False)
128
  with accordion:
129
- sentence_text = gr.Textbox(label="Sentence", interactive=False)
130
-
131
  # Row for emotion annotation and confidence
132
  with gr.Row():
133
- emotions = gr.Radio(["Blank", "Joy", "Sad", "Angry", "Neutral"], label="Predominant Emotion", value = "Blank")
134
 
135
  with gr.Row():
136
- confidence = gr.Slider(label="Confidence (%)", minimum=0, maximum=100, step=10)
137
  # Instructions for emotion annotation
138
  with gr.Sidebar():
139
- happy_words = gr.Textbox(label = "Happy")
 
 
140
 
141
 
 
 
 
 
142
  # Next and Previous Buttons
143
  with gr.Row():
144
- previous_button = gr.Button("Previous Example")
145
- next_button = gr.Button("Next Example")
146
-
147
-
148
- # Comment section
149
- comments = gr.Textbox(label="Comments", interactive=True)
150
-
151
-
152
 
153
  # Go back
154
  previous_button.click(
155
  previous_example,
156
- inputs=[emotions, confidence, comments],
157
  outputs=[sentence_text, audio_player, emotions, confidence, ann_completed, comments],
158
  )
159
 
160
  # Go to the next example
161
  next_button.click(
162
  next_example,
163
- inputs=[emotions, confidence, comments],
164
  outputs=[sentence_text, audio_player, emotions, confidence, ann_completed, comments],
165
  )
166
-
167
  #Update progress bar
168
  next_button.click(None, [], [ann_completed, total], js = js_progress_bar)
169
 
170
- lets_go.click(load_example, inputs = [gr.Number(current_index["index"], visible = False)], outputs = [sentence_text, audio_playe, emotions, confidence, ann_completed, comments])
 
 
 
 
 
171
 
172
  demo.launch()
173
 
 
17
  border-radius: 2px;
18
  }
19
 
20
+ #myHideBlock {
21
+ width: 110%;
22
+ height: 110%;
23
+ background-color: blue;
24
+ }
25
+
26
  #progressText {
27
  position: absolute;
28
  top: 50%;
 
32
  font-weight: bold;
33
  font-size: 14px;
34
  """
 
35
  js_progress_bar = """
36
  function move(n_ann, total_ann) {
37
+
38
  var elem = document.getElementById("myBar");
39
  elem.style.width = n_ann/total_ann * 100 + "%";
40
  progressText.innerText = 'Completed: ' + n_ann + '/' + total_ann
41
+ }
42
  """
43
  # List of all audio files to annotate
44
  file_list = pd.read_excel(os.path.join('combined_annotations.xlsx'))
 
59
 
60
  # If the user already made an annotation for this example, gradio will return said annotation
61
  previous_annotation = (
62
+ annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["SAMPLE ID"], "emotion": 'Blank', "confidence": 0,
63
  "comments": ''}
64
  )
65
  return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], current_index['index'] + 1, previous_annotation["comments"])
66
 
67
 
68
+ def save_annotation(emotions, confidence, comments, participant_id):
69
  """Save the annotation for the current example."""
70
 
71
  idx = current_index["index"]
 
80
  else:
81
  annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments]
82
  ann_completed.value += 1
83
+ annotations.to_csv(f"{participant_id}_annotations.csv", index=False) # Save to a CSV file
84
 
85
 
86
+ def next_example(emotions, confidence, comments, participant_id):
87
  """Move to the next example."""
88
  if emotions == "Blank":
89
+ gr.Warning("Please fill out the emotion section")
90
  else:
91
 
92
+ save_annotation(emotions, confidence, comments, participant_id)
93
  if current_index["index"] < len(file_list) - 1:
94
  current_index["index"] += 1
95
  return load_example(current_index["index"])
96
 
97
+ def previous_example(emotion, confidence, comments, participant_id):
98
  """Move to the previous example."""
99
  if emotion.value != "Blank":
100
+ save_annotation(emotion, confidence, comments, participant_id)
101
  if current_index["index"] > 0:
102
  current_index["index"] -= 1
103
  return load_example(current_index["index"])
104
  return load_example(current_index["index"])
105
 
106
+ def deactivate_participant_id(participant_id, lets_go):
107
+ participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
108
+ lets_go = gr.Button("Participant selected!", interactive = False)
109
+ return participant_id, lets_go
110
 
111
+ def activate_elements(emotions, confidence, comments, next_button, previous_button):
112
+ emotions = gr.Radio(["Blank", "Joy", "Sad", "Angry", "Neutral"], label="Predominant Emotion", value = "Blank", interactive = True)
113
+ confidence = gr.Slider(label="Confidence (%)", minimum=0, maximum=100, step=10, interactive = True)
114
+ comments = gr.Textbox(label="Comments", interactive=True)
115
+ previous_button = gr.Button("Previous Example", interactive = True)
116
+ next_button = gr.Button("Next Example", interactive = True)
117
+
118
+ return emotions, confidence, comments, next_button, previous_button
119
  # ===================
120
  # Gradio Interface
121
  # ===================
122
+
123
 
124
  with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
 
 
125
 
126
  ann_completed = gr.Number(1, visible=False)
127
  total = gr.Number(total_annotations, visible=False)
 
130
  gr.HTML("""
131
  <div id="myProgress">
132
  <div id="myBar">
133
+ <span id="progressText">Press "Let's go!" to start</span>
134
  </div>
135
  </div>""")
136
 
137
  # Row with audio player
138
  with gr.Row():
139
+ audio_player = gr.Audio(value= 'test.mp3', label="Audio", type="filepath", interactive=False)
140
 
141
  # Hidden row with corresponding sentence
142
  with gr.Row():
143
  accordion = gr.Accordion(label="Click to see the sentence", open=False)
144
  with accordion:
145
+ sentence_text = gr.Textbox(label="Transcription", interactive=False, value = 'This is a sentence.')
 
146
  # Row for emotion annotation and confidence
147
  with gr.Row():
148
+ emotions = gr.Radio(["Blank", "Joy", "Sad", "Angry", "Neutral"], label="Predominant Emotion", value = "Blank", interactive = False)
149
 
150
  with gr.Row():
151
+ confidence = gr.Slider(label="Confidence (%)", minimum=0, maximum=100, step=10, interactive = False)
152
  # Instructions for emotion annotation
153
  with gr.Sidebar():
154
+ participant_id = gr.Textbox(label='What is your participant ID?', interactive = True)
155
+ lets_go = gr.Button("Let's go!")
156
+ #happy_words = gr.Textbox(label = "Happy")
157
 
158
 
159
+ with gr.Row():
160
+ # Comment section
161
+ comments = gr.Textbox(label="Comments", interactive=False)
162
+
163
  # Next and Previous Buttons
164
  with gr.Row():
165
+ previous_button = gr.Button("Previous Example", interactive = False)
166
+ next_button = gr.Button("Next Example", interactive = False)
 
 
 
 
 
 
167
 
168
  # Go back
169
  previous_button.click(
170
  previous_example,
171
+ inputs=[emotions, confidence, comments, participant_id],
172
  outputs=[sentence_text, audio_player, emotions, confidence, ann_completed, comments],
173
  )
174
 
175
  # Go to the next example
176
  next_button.click(
177
  next_example,
178
+ inputs=[emotions, confidence, comments, participant_id],
179
  outputs=[sentence_text, audio_player, emotions, confidence, ann_completed, comments],
180
  )
 
181
  #Update progress bar
182
  next_button.click(None, [], [ann_completed, total], js = js_progress_bar)
183
 
184
+
185
+ lets_go.click(None, [], [ann_completed, total], js = js_progress_bar)
186
+ lets_go.click(deactivate_participant_id, [participant_id, lets_go], [participant_id, lets_go])
187
+ lets_go.click(activate_elements, [emotions, confidence, comments, next_button, previous_button], [emotions, confidence, comments, next_button, previous_button])
188
+ lets_go.click(load_example, inputs = [gr.Number(current_index["index"], visible = False)], outputs = [sentence_text, audio_player, emotions, confidence, ann_completed, comments])
189
+
190
 
191
  demo.launch()
192
 
io_annotations.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ sample_id,sentence,emotion,confidence,comments
2
+ 007-0023,I was mostly hanging out in East Village in the clubs.,Neutral,0,
3
+ 007-0034,"And we became friends, and then he told me that his lover had died of AIDS.",Neutral,0,