Spaces:
Sleeping
Sleeping
Feat: Add click counter
Browse files
app.py
CHANGED
|
@@ -217,7 +217,7 @@ global file_list
|
|
| 217 |
global total_annotations
|
| 218 |
|
| 219 |
# Initialize an empty DataFrame to store annotations
|
| 220 |
-
annotations = pd.DataFrame(columns=['sample_id', 'sentence', 'emotion', 'confidence', 'comments'])
|
| 221 |
|
| 222 |
password_files = os.getenv("password_files")
|
| 223 |
|
|
@@ -267,7 +267,7 @@ def load_first_example(participant_id, ann_completed, current_index):
|
|
| 267 |
path_ann = f'{persistent_storage}/{participant_id}_annotations.csv'
|
| 268 |
print(path_ann)
|
| 269 |
if os.path.exists(path_ann):
|
| 270 |
-
annotations = pd.read_csv(path_ann)
|
| 271 |
|
| 272 |
current_index = len(annotations)
|
| 273 |
print('path was found')
|
|
@@ -288,17 +288,17 @@ def load_example(index):
|
|
| 288 |
# If the user already made an annotation for this example, gradio will return said annotation
|
| 289 |
previous_annotation = (
|
| 290 |
annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["sample_id"], "emotion": 'Blank', "confidence": 0,
|
| 291 |
-
"comments": ''}
|
| 292 |
)
|
| 293 |
|
| 294 |
start = row['start']
|
| 295 |
end = row['end']
|
| 296 |
duration = get_audio_duration(audio_path)
|
| 297 |
print(f'{start} {end} {duration}')
|
| 298 |
-
return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], previous_annotation["comments"], start, end, duration)
|
| 299 |
|
| 300 |
|
| 301 |
-
def save_annotation(emotions, confidence, comments, participant_id, ann_completed, current_index):
|
| 302 |
"""Save the annotation for the current example."""
|
| 303 |
|
| 304 |
row = file_list.iloc[current_index]
|
|
@@ -307,50 +307,50 @@ def save_annotation(emotions, confidence, comments, participant_id, ann_complete
|
|
| 307 |
|
| 308 |
# Update or append annotation
|
| 309 |
if sample_id in annotations["sample_id"].values:
|
| 310 |
-
annotations.loc[annotations["sample_id"] == sample_id, ["emotion", "confidence", "comments"]] = \
|
| 311 |
-
[emotions, confidence, comments]
|
| 312 |
else:
|
| 313 |
-
annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments]
|
| 314 |
ann_completed = gr.Number(ann_completed + 1, visible=False)
|
| 315 |
annotations.to_csv(f"{persistent_storage}/{participant_id}_annotations.csv", index=False) # Save to a CSV file
|
| 316 |
|
| 317 |
return ann_completed
|
| 318 |
|
| 319 |
-
def next_example(emotions, confidence, comments, participant_id, ann_completed, current_index):
|
| 320 |
"""Move to the next example."""
|
| 321 |
|
| 322 |
if emotions == "Blank":
|
| 323 |
gr.Warning("Please fill out the emotion section. 'Blank' is not a valid emotion.")
|
| 324 |
else:
|
| 325 |
|
| 326 |
-
ann_completed = save_annotation(emotions, confidence, comments, participant_id, ann_completed, current_index)
|
| 327 |
if current_index < len(file_list) - 1:
|
| 328 |
current_index += 1
|
| 329 |
return *load_example(current_index), ann_completed, current_index
|
| 330 |
|
| 331 |
-
def previous_example(emotion, confidence, comments, participant_id, ann_completed, current_index):
|
| 332 |
"""Move to the previous example."""
|
| 333 |
|
| 334 |
if emotion != "Blank":
|
| 335 |
-
ann_completed = save_annotation(emotion, confidence, comments, participant_id, ann_completed, current_index)
|
| 336 |
if current_index > 0:
|
| 337 |
current_index -= 1
|
| 338 |
|
| 339 |
return *load_example(current_index), ann_completed, current_index
|
| 340 |
|
| 341 |
-
def deactivate_participant_id(participant_id, lets_go, total, previous_button, next_button, sentence_text, audio_player, emotions, confidence, comments, ann_completed, current_index):
|
| 342 |
global file_list
|
| 343 |
global total_annotations
|
| 344 |
if participant_id in possible_ids.keys():
|
| 345 |
-
file_list = pd.read_csv(os.path.join(persistent_storage, 'files_to_annotate_2round', f'group_{possible_ids[participant_id]}.csv'))
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
total_annotations = len(file_list)
|
| 350 |
total = gr.Number(total_annotations, visible=False)
|
|
|
|
| 351 |
|
| 352 |
-
|
| 353 |
-
sentence, audio_player, emotions, confidence, comments, start, end, duration, ann_completed, current_index = load_first_example(participant_id, ann_completed, current_index)
|
| 354 |
|
| 355 |
participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
|
| 356 |
lets_go = gr.Button("Participant selected!", interactive = False)
|
|
@@ -362,8 +362,12 @@ def deactivate_participant_id(participant_id, lets_go, total, previous_button, n
|
|
| 362 |
|
| 363 |
else:
|
| 364 |
gr.Warning("Please insert a valid participant ID")
|
| 365 |
-
return participant_id, lets_go, total, previous_button, next_button, sentence_text, audio_player, emotions, confidence, comments, start, end, duration, ann_completed, current_index
|
|
|
|
|
|
|
|
|
|
| 366 |
|
|
|
|
| 367 |
# ===================
|
| 368 |
# Gradio Interface
|
| 369 |
# ===================
|
|
@@ -408,6 +412,8 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
|
|
| 408 |
start = gr.Number(0, visible = False)
|
| 409 |
end = gr.Number(0, visible = False)
|
| 410 |
duration = gr.Number(0, visible = False)
|
|
|
|
|
|
|
| 411 |
# Row with progress bar
|
| 412 |
|
| 413 |
gr.HTML("""
|
|
@@ -446,19 +452,19 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
|
|
| 446 |
# Go back
|
| 447 |
previous_button.click(
|
| 448 |
previous_example,
|
| 449 |
-
inputs=[emotions, confidence, comments, participant_id, ann_completed, current_index],
|
| 450 |
-
outputs=[sentence_text, audio_player, emotions, confidence, comments, start, end, duration, ann_completed, current_index],).then(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
|
| 451 |
|
| 452 |
# Go to the next example
|
| 453 |
next_button.click(
|
| 454 |
next_example,
|
| 455 |
-
inputs=[emotions, confidence, comments, participant_id, ann_completed, current_index],
|
| 456 |
-
outputs=[sentence_text, audio_player, emotions, confidence, comments, start, end, duration, ann_completed, current_index],).then(None, [], [start, end, duration, current_index,ann_completed, total], js = js_progress_bar)
|
| 457 |
|
| 458 |
buttons = [previous_button, next_button]
|
| 459 |
data = [sentence_text, audio_player, emotions, confidence, comments]
|
| 460 |
-
lets_go.click(deactivate_participant_id, [participant_id, lets_go, total, *buttons, *data, ann_completed, current_index], [participant_id, lets_go, total, *buttons, *data, start, end, duration, ann_completed, current_index]).then( None, [], [start, end, duration, current_index, ann_completed, total], js = js_progress_bar)
|
| 461 |
-
|
| 462 |
|
| 463 |
with gr.Tab("Access Files"):
|
| 464 |
with gr.Row():
|
|
|
|
| 217 |
global total_annotations
|
| 218 |
|
| 219 |
# Initialize an empty DataFrame to store annotations
|
| 220 |
+
annotations = pd.DataFrame(columns=['sample_id', 'sentence', 'emotion', 'confidence', 'comments', 'n_clicks'])
|
| 221 |
|
| 222 |
password_files = os.getenv("password_files")
|
| 223 |
|
|
|
|
| 267 |
path_ann = f'{persistent_storage}/{participant_id}_annotations.csv'
|
| 268 |
print(path_ann)
|
| 269 |
if os.path.exists(path_ann):
|
| 270 |
+
annotations = pd.read_csv(path_ann, keep_default_na=False)
|
| 271 |
|
| 272 |
current_index = len(annotations)
|
| 273 |
print('path was found')
|
|
|
|
| 288 |
# If the user already made an annotation for this example, gradio will return said annotation
|
| 289 |
previous_annotation = (
|
| 290 |
annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["sample_id"], "emotion": 'Blank', "confidence": 0,
|
| 291 |
+
"comments": '', "n_clicks": 0}
|
| 292 |
)
|
| 293 |
|
| 294 |
start = row['start']
|
| 295 |
end = row['end']
|
| 296 |
duration = get_audio_duration(audio_path)
|
| 297 |
print(f'{start} {end} {duration}')
|
| 298 |
+
return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], previous_annotation["comments"], n_clicks, start, end, duration)
|
| 299 |
|
| 300 |
|
| 301 |
+
def save_annotation(emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
|
| 302 |
"""Save the annotation for the current example."""
|
| 303 |
|
| 304 |
row = file_list.iloc[current_index]
|
|
|
|
| 307 |
|
| 308 |
# Update or append annotation
|
| 309 |
if sample_id in annotations["sample_id"].values:
|
| 310 |
+
annotations.loc[annotations["sample_id"] == sample_id, ["emotion", "confidence", "comments", "n_clicks"]] = \
|
| 311 |
+
[emotions, confidence, comments, n_clicks]
|
| 312 |
else:
|
| 313 |
+
annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments, n_clicks]
|
| 314 |
ann_completed = gr.Number(ann_completed + 1, visible=False)
|
| 315 |
annotations.to_csv(f"{persistent_storage}/{participant_id}_annotations.csv", index=False) # Save to a CSV file
|
| 316 |
|
| 317 |
return ann_completed
|
| 318 |
|
| 319 |
+
def next_example(emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
|
| 320 |
"""Move to the next example."""
|
| 321 |
|
| 322 |
if emotions == "Blank":
|
| 323 |
gr.Warning("Please fill out the emotion section. 'Blank' is not a valid emotion.")
|
| 324 |
else:
|
| 325 |
|
| 326 |
+
ann_completed = save_annotation(emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index)
|
| 327 |
if current_index < len(file_list) - 1:
|
| 328 |
current_index += 1
|
| 329 |
return *load_example(current_index), ann_completed, current_index
|
| 330 |
|
| 331 |
+
def previous_example(emotion, confidence, comments, n_clicks, participant_id, ann_completed, current_index):
|
| 332 |
"""Move to the previous example."""
|
| 333 |
|
| 334 |
if emotion != "Blank":
|
| 335 |
+
ann_completed = save_annotation(emotion, confidence, comments, n_clicks, participant_id, ann_completed, current_index)
|
| 336 |
if current_index > 0:
|
| 337 |
current_index -= 1
|
| 338 |
|
| 339 |
return *load_example(current_index), ann_completed, current_index
|
| 340 |
|
| 341 |
+
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):
|
| 342 |
global file_list
|
| 343 |
global total_annotations
|
| 344 |
if participant_id in possible_ids.keys():
|
| 345 |
+
file_list = pd.read_csv(os.path.join(persistent_storage, 'files_to_annotate_2round', f'group_{possible_ids[participant_id]}.csv'), keep_default_na=False)
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
total_annotations = len(file_list)
|
| 350 |
total = gr.Number(total_annotations, visible=False)
|
| 351 |
+
|
| 352 |
|
| 353 |
+
sentence, audio_player, emotions, confidence, comments, n_clicks, start, end, duration, ann_completed, current_index = load_first_example(participant_id, ann_completed, current_index)
|
|
|
|
| 354 |
|
| 355 |
participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
|
| 356 |
lets_go = gr.Button("Participant selected!", interactive = False)
|
|
|
|
| 362 |
|
| 363 |
else:
|
| 364 |
gr.Warning("Please insert a valid participant ID")
|
| 365 |
+
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
|
| 366 |
+
|
| 367 |
+
def count_clicks(n_clicks):
|
| 368 |
+
n_clicks = gr.Number(n_clicks + 1, visible = False)
|
| 369 |
|
| 370 |
+
return n_clicks
|
| 371 |
# ===================
|
| 372 |
# Gradio Interface
|
| 373 |
# ===================
|
|
|
|
| 412 |
start = gr.Number(0, visible = False)
|
| 413 |
end = gr.Number(0, visible = False)
|
| 414 |
duration = gr.Number(0, visible = False)
|
| 415 |
+
n_clicks = gr.Number(0, visible = False)
|
| 416 |
+
|
| 417 |
# Row with progress bar
|
| 418 |
|
| 419 |
gr.HTML("""
|
|
|
|
| 452 |
# Go back
|
| 453 |
previous_button.click(
|
| 454 |
previous_example,
|
| 455 |
+
inputs=[emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index],
|
| 456 |
+
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)
|
| 457 |
|
| 458 |
# Go to the next example
|
| 459 |
next_button.click(
|
| 460 |
next_example,
|
| 461 |
+
inputs=[emotions, confidence, comments, n_clicks, participant_id, ann_completed, current_index],
|
| 462 |
+
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)
|
| 463 |
|
| 464 |
buttons = [previous_button, next_button]
|
| 465 |
data = [sentence_text, audio_player, emotions, confidence, comments]
|
| 466 |
+
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)
|
| 467 |
+
audio_player.play(count_clicks, [n_clicks], [n_clicks])
|
| 468 |
|
| 469 |
with gr.Tab("Access Files"):
|
| 470 |
with gr.Row():
|