fpessanha commited on
Commit
e0437fb
·
1 Parent(s): 0edd040

Fix: Address state issue around annotations variable

Browse files
Files changed (1) hide show
  1. app.py +51 -52
app.py CHANGED
@@ -230,7 +230,7 @@ start_annotating = """<h2>How to use the annotation interface?</h2>
230
  </div>
231
 
232
  </div>"""
233
- global file_list
234
  persistent_storage = Path('/data')
235
 
236
  password_files = os.getenv("password_files")
@@ -279,38 +279,35 @@ def get_storage(password):
279
  return gr.Warning("Please provide the correct password"), None
280
 
281
 
282
- def load_first_example(participant_id, ann_completed, current_index):
283
  """ Loads and first example and updates index"""
284
- global annotations
285
-
286
  path_ann = f'{persistent_storage}/{participant_id}_annotations.csv'
287
  print(path_ann)
288
  if os.path.exists(path_ann):
289
- annotations = pd.read_csv(path_ann, keep_default_na=False)
290
 
291
- current_index = len(annotations)
292
  print('path was found')
293
- print(annotations)
294
- print(len(annotations))
295
- ann_completed = gr.Number(len(annotations) - 1, visible=False)
296
- print(len(annotations))
297
  else:
298
  # Initialize an empty DataFrame to store annotations
299
- annotations = pd.DataFrame(columns=['sample_id', 'sentence', 'emotion', 'confidence', 'comments', 'n_clicks'])
300
 
301
- return *load_example(current_index), ann_completed, current_index
302
 
303
- def load_example(index):
304
  """Loads the example in row #index from dataframe file_list.
305
  If there are any annotations it will give those values to the annotation dataframe"""
306
 
307
- row = file_list.iloc[index]
308
  audio_path = os.path.join(persistent_storage, 'files_to_annotate_2round', row["sample_id"].split('-')[0], row["sample_id"] + '.wav')
309
  sentence = row["sentence"]
310
 
311
  # If the user already made an annotation for this example, gradio will return said annotation
312
  previous_annotation = (
313
- annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["sample_id"], "emotion": 'Blank', "confidence": 'Blank',
314
  "comments": '', "n_clicks": 0}
315
  )
316
 
@@ -318,10 +315,10 @@ def load_example(index):
318
  end = row['end']
319
  duration = get_audio_duration(audio_path)
320
  print(f'{start} {end} {duration}')
321
- return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], previous_annotation["comments"], n_clicks, start, end, duration)
322
 
323
 
324
- def save_annotation(emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
325
  """Save the annotation for the current example."""
326
 
327
  row = file_list.iloc[current_index]
@@ -329,17 +326,17 @@ def save_annotation(emotions, confidence, comments, n_clicks, participant_id, an
329
  sentence = row["sentence"]
330
 
331
  # Update or append annotation
332
- if sample_id in annotations["sample_id"].values:
333
- annotations.loc[annotations["sample_id"] == sample_id, ["emotion", "confidence", "comments", "n_clicks"]] = \
334
  [emotions, confidence, comments, n_clicks]
335
  else:
336
- annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments, n_clicks]
337
  ann_completed = gr.Number(ann_completed + 1, visible=False)
338
- annotations.to_csv(f"{persistent_storage}/{participant_id}_annotations.csv", index=False) # Save to a CSV file
339
 
340
- return ann_completed
341
 
342
- def next_example(emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
343
  """Move to the next example."""
344
 
345
  if emotions == "Blank":
@@ -348,34 +345,32 @@ def next_example(emotions, confidence, comments, n_clicks, participant_id, ann_c
348
  gr.Warning("Please fill out the confidence section. 'Blank' is not a valid input.")
349
 
350
  else:
351
- ann_completed = save_annotation(emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index)
352
- if current_index < len(file_list) - 1:
353
  current_index += 1
354
- return *load_example(current_index), ann_completed, current_index
355
 
356
- def previous_example(emotion, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
357
  """Move to the previous example."""
358
 
359
  if emotion != "Blank":
360
- ann_completed = save_annotation(emotion, confidence, comments, n_clicks, participant_id, ann_completed, current_index)
361
  if current_index > 0:
362
  current_index -= 1
363
 
364
- return *load_example(current_index), ann_completed, current_index
365
 
366
- def deactivate_participant_id(participant_id, lets_go, total, previous_button, next_button, sentence_text, audio_player, emotions, confidence, comments, n_clicks, ann_completed, current_index):
367
- global file_list
368
- global total_annotations
369
  if participant_id in possible_ids.keys():
370
- file_list = pd.read_csv(os.path.join(persistent_storage, 'files_to_annotate_2round', f'group_{possible_ids[participant_id]}_v2.csv'), keep_default_na=False)
371
 
372
-
373
-
374
- total_annotations = len(file_list)
375
  total = gr.Number(total_annotations, visible=False)
376
 
377
 
378
- sentence, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index = load_first_example(participant_id, ann_completed, current_index)
379
  print(sentence)
380
  print(start, end, duration)
381
  participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
@@ -387,11 +382,12 @@ def deactivate_participant_id(participant_id, lets_go, total, previous_button, n
387
  comments = gr.Textbox(label="Comments", visible =True, value = comments)
388
  previous_button = gr.Button("Previous Example", visible = True)
389
  next_button = gr.Button("Next Example",visible = True)
 
 
390
 
391
  else:
392
- gr.Warning("Please insert a valid participant ID")
393
- return participant_id, lets_go, total, previous_button, next_button, sentence_text, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index
394
-
395
  def count_clicks(n_clicks):
396
  n_clicks = gr.Number(n_clicks + 1, visible = False)
397
 
@@ -441,13 +437,16 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
441
 
442
 
443
  with gr.Tab("Annotation Interface"):
444
- ann_completed = gr.Number(0, visible=False)
445
- total = gr.Number(0, visible=False)
446
- current_index = gr.Number(0, visible = False)
447
- start = gr.Number(0, visible = False)
448
- end = gr.Number(0, visible = False)
449
- duration = gr.Number(0, visible = False)
450
- n_clicks = gr.Number(0, visible = False)
 
 
 
451
 
452
  # Row with progress bar
453
 
@@ -487,18 +486,18 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
487
  # Go back
488
  previous_button.click(
489
  previous_example,
490
- inputs=[emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index],
491
- outputs=[sentence_text, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index],).then(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
492
 
493
  # Go to the next example
494
  next_button.click(
495
  next_example,
496
- inputs=[emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index],
497
- outputs=[sentence_text, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index],).then(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
498
 
499
  buttons = [previous_button, next_button]
500
  data = [sentence_text, audio_player, emotions, confidence, comments]
501
- lets_go.click(deactivate_participant_id, [participant_id, lets_go, total, *buttons, *data, n_clicks, ann_completed, current_index], [participant_id, lets_go, total, *buttons, *data, n_clicks, start, end, duration, ann_completed, current_index]).then( None, [], [start, end, duration, current_index, ann_completed, total], js = js_progress_bar)
502
  audio_player.play(count_clicks, [n_clicks], [n_clicks])
503
 
504
  sidebar.collapse(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
 
230
  </div>
231
 
232
  </div>"""
233
+
234
  persistent_storage = Path('/data')
235
 
236
  password_files = os.getenv("password_files")
 
279
  return gr.Warning("Please provide the correct password"), None
280
 
281
 
282
+ def load_first_example(annotations_df, file_list_df, participant_id, ann_completed, current_index):
283
  """ Loads and first example and updates index"""
 
 
284
  path_ann = f'{persistent_storage}/{participant_id}_annotations.csv'
285
  print(path_ann)
286
  if os.path.exists(path_ann):
287
+ annotations_df = pd.read_csv(path_ann, keep_default_na=False)
288
 
289
+ current_index = len(annotations_df)
290
  print('path was found')
291
+
292
+ ann_completed = gr.Number(len(annotations_df) - 1, visible=False)
293
+
 
294
  else:
295
  # Initialize an empty DataFrame to store annotations
296
+ annotations_df = pd.DataFrame(columns=['sample_id', 'sentence', 'emotion', 'confidence', 'comments', 'n_clicks'])
297
 
298
+ return annotations_df, *load_example(annotations_df, file_list_df, current_index), ann_completed, current_index
299
 
300
+ def load_example(annotations_df, file_list_df, index):
301
  """Loads the example in row #index from dataframe file_list.
302
  If there are any annotations it will give those values to the annotation dataframe"""
303
 
304
+ row = file_list_df.iloc[index]
305
  audio_path = os.path.join(persistent_storage, 'files_to_annotate_2round', row["sample_id"].split('-')[0], row["sample_id"] + '.wav')
306
  sentence = row["sentence"]
307
 
308
  # If the user already made an annotation for this example, gradio will return said annotation
309
  previous_annotation = (
310
+ annotations_df.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["sample_id"], "emotion": 'Blank', "confidence": 'Blank',
311
  "comments": '', "n_clicks": 0}
312
  )
313
 
 
315
  end = row['end']
316
  duration = get_audio_duration(audio_path)
317
  print(f'{start} {end} {duration}')
318
+ return sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], previous_annotation["comments"], n_clicks, start, end, duration
319
 
320
 
321
+ def save_annotation(annotations_df, file_list_df, emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
322
  """Save the annotation for the current example."""
323
 
324
  row = file_list.iloc[current_index]
 
326
  sentence = row["sentence"]
327
 
328
  # Update or append annotation
329
+ if sample_id in annotations_df["sample_id"].values:
330
+ annotations_df.loc[annotations_df["sample_id"] == sample_id, ["emotion", "confidence", "comments", "n_clicks"]] = \
331
  [emotions, confidence, comments, n_clicks]
332
  else:
333
+ annotations_df.loc[len(annotations_df)] = [sample_id, sentence, emotions, confidence, comments, n_clicks]
334
  ann_completed = gr.Number(ann_completed + 1, visible=False)
335
+ annotations_df.to_csv(f"{persistent_storage}/{participant_id}_annotations.csv", index=False) # Save to a CSV file
336
 
337
+ return annotations_df, ann_completed
338
 
339
+ def next_example(annotations_df, file_list_df, emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
340
  """Move to the next example."""
341
 
342
  if emotions == "Blank":
 
345
  gr.Warning("Please fill out the confidence section. 'Blank' is not a valid input.")
346
 
347
  else:
348
+ annotations_df, ann_completed = save_annotation(annotations_df, file_list_df, emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index)
349
+ if current_index < len(file_list_df) - 1:
350
  current_index += 1
351
+ return annotations_df, *load_example(annotations_df, file_list_df, current_index), ann_completed, current_index
352
 
353
+ def previous_example(annotations_df, file_list_df, emotion, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
354
  """Move to the previous example."""
355
 
356
  if emotion != "Blank":
357
+ annotations_df, ann_completed = save_annotation(annotations_df, file_list_df, emotion, confidence, comments, n_clicks, participant_id, ann_completed, current_index)
358
  if current_index > 0:
359
  current_index -= 1
360
 
361
+ return annotations_df, *load_example(annotations_df, file_list_df, current_index), ann_completed, current_index
362
 
363
+ def deactivate_participant_id(annotations_df, file_list_df, total_annotations, participant_id, lets_go, total, previous_button, next_button, sentence_text, audio_player, emotions, confidence, comments, n_clicks, ann_completed, current_index):
364
+ print(possible_ids.keys())
 
365
  if participant_id in possible_ids.keys():
366
+ file_list_df = pd.read_csv(os.path.join(persistent_storage, 'files_to_annotate_2round', f'group_{possible_ids[participant_id]}_v2.csv'), keep_default_na=False)
367
 
368
+
369
+ total_annotations = len(file_list_df)
 
370
  total = gr.Number(total_annotations, visible=False)
371
 
372
 
373
+ annotations_df, sentence, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index = load_first_example(annotations_df, file_list_df, participant_id, ann_completed, current_index)
374
  print(sentence)
375
  print(start, end, duration)
376
  participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
 
382
  comments = gr.Textbox(label="Comments", visible =True, value = comments)
383
  previous_button = gr.Button("Previous Example", visible = True)
384
  next_button = gr.Button("Next Example",visible = True)
385
+
386
+ return annotations_df, file_list_df, participant_id, lets_go, total, previous_button, next_button, sentence_text, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index
387
 
388
  else:
389
+ raise gr.Error("Please insert a valid participant ID")
390
+
 
391
  def count_clicks(n_clicks):
392
  n_clicks = gr.Number(n_clicks + 1, visible = False)
393
 
 
437
 
438
 
439
  with gr.Tab("Annotation Interface"):
440
+ ann_completed = gr.State(0)
441
+ total = gr.State(0)
442
+ current_index = gr.State(0)
443
+ start = gr.State(0)
444
+ end = gr.State(0)
445
+ duration = gr.State(0)
446
+ n_clicks = gr.State(0)
447
+ annotations = gr.State(pd.DataFrame())
448
+ file_list = gr.State(pd.DataFrame())
449
+
450
 
451
  # Row with progress bar
452
 
 
486
  # Go back
487
  previous_button.click(
488
  previous_example,
489
+ inputs=[annotations, file_list, emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index],
490
+ outputs=[annotations, sentence_text, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index],).then(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
491
 
492
  # Go to the next example
493
  next_button.click(
494
  next_example,
495
+ inputs=[annotations, file_list,emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index],
496
+ outputs=[annotations,sentence_text, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index],).then(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
497
 
498
  buttons = [previous_button, next_button]
499
  data = [sentence_text, audio_player, emotions, confidence, comments]
500
+ lets_go.click(deactivate_participant_id, [annotations, file_list, total, participant_id, lets_go, *buttons, *data, n_clicks, ann_completed, current_index], [annotations, file_list, participant_id, lets_go, total, *buttons, *data, n_clicks, start, end, duration, ann_completed, current_index]).then( None, [], [start, end, duration, current_index, ann_completed, total], js = js_progress_bar)
501
  audio_player.play(count_clicks, [n_clicks], [n_clicks])
502
 
503
  sidebar.collapse(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)