Spaces:
Sleeping
Sleeping
Fix: Save annotations in persistent storage
Browse files
app.py
CHANGED
|
@@ -214,8 +214,10 @@ total_annotations = len(file_list)
|
|
| 214 |
# Initialize an empty DataFrame to store annotations
|
| 215 |
annotations = pd.DataFrame(columns=['sample_id', 'sentence', 'emotion', 'confidence', 'comments'])
|
| 216 |
current_index = {"index": 0} # Dictionary to allow modifying inside functions
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
| 219 |
def get_storage():
|
| 220 |
#source: https://discuss.huggingface.co/t/accessing-data-folder-of-persistent-storage/46681/2
|
| 221 |
files = [
|
|
@@ -233,19 +235,23 @@ def get_storage():
|
|
| 233 |
return files, f"{usage/(1024.0 ** 3):.3f}GB"
|
| 234 |
|
| 235 |
|
| 236 |
-
def load_first_example(index, participant_id):
|
| 237 |
""" Loads and first example and updates index"""
|
|
|
|
|
|
|
| 238 |
|
| 239 |
path_ann = f'{persistent_storage}/{participant_id}_annotations.csv'
|
| 240 |
print(path_ann)
|
| 241 |
if os.path.exists(path_ann):
|
| 242 |
-
annotations = pd.read_csv(path_ann
|
|
|
|
| 243 |
current_index['index'] = len(annotations)
|
| 244 |
print('path was found')
|
| 245 |
print(annotations)
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
| 249 |
|
| 250 |
def load_example(index):
|
| 251 |
"""Loads the example in row #index from dataframe file_list.
|
|
@@ -260,10 +266,11 @@ def load_example(index):
|
|
| 260 |
annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["SAMPLE ID"], "emotion": 'Blank', "confidence": 0,
|
| 261 |
"comments": ''}
|
| 262 |
)
|
|
|
|
| 263 |
return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], current_index['index'] + 1, previous_annotation["comments"])
|
| 264 |
|
| 265 |
|
| 266 |
-
def save_annotation(emotions, confidence, comments, participant_id):
|
| 267 |
"""Save the annotation for the current example."""
|
| 268 |
|
| 269 |
idx = current_index["index"]
|
|
@@ -277,29 +284,30 @@ def save_annotation(emotions, confidence, comments, participant_id):
|
|
| 277 |
[emotions, confidence, comments]
|
| 278 |
else:
|
| 279 |
annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments]
|
| 280 |
-
ann_completed.
|
| 281 |
annotations.to_csv(f"{persistent_storage}/{participant_id}_annotations.csv", index=False) # Save to a CSV file
|
| 282 |
|
|
|
|
| 283 |
|
| 284 |
-
def next_example(emotions, confidence, comments, participant_id):
|
| 285 |
"""Move to the next example."""
|
| 286 |
if emotions == "Blank":
|
| 287 |
gr.Warning("Please fill out the emotion section. 'Blank' is not a valid emotion.")
|
| 288 |
else:
|
| 289 |
|
| 290 |
-
save_annotation(emotions, confidence, comments, participant_id)
|
| 291 |
if current_index["index"] < len(file_list) - 1:
|
| 292 |
current_index["index"] += 1
|
| 293 |
-
return load_example(current_index["index"])
|
| 294 |
|
| 295 |
-
def previous_example(emotion, confidence, comments, participant_id):
|
| 296 |
"""Move to the previous example."""
|
| 297 |
if emotion.value != "Blank":
|
| 298 |
-
save_annotation(emotion, confidence, comments, participant_id)
|
| 299 |
if current_index["index"] > 0:
|
| 300 |
current_index["index"] -= 1
|
| 301 |
-
return load_example(current_index["index"])
|
| 302 |
-
return load_example(current_index["index"])
|
| 303 |
|
| 304 |
def deactivate_participant_id(participant_id, lets_go):
|
| 305 |
participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
|
|
@@ -350,14 +358,13 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
|
|
| 350 |
angry_int_audio = gr.Audio(value=f'{persistent_storage}/emotion_examples/angry_intense.wav', label = "Anger (High Intensity)")
|
| 351 |
|
| 352 |
agreement = gr.Checkbox(value = False, label = "I agree", info = "I agree to have my annotations, comments, and questionnaire answers used for research purposes. I understand that any personal information will be anonymized.", interactive = True)
|
| 353 |
-
|
| 354 |
-
|
| 355 |
|
|
|
|
| 356 |
with gr.Tab("Annotation Interface"):
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
# Row with progress bar
|
|
|
|
| 361 |
gr.HTML("""
|
| 362 |
<div id="myProgress">
|
| 363 |
<div id="myBar">
|
|
@@ -365,7 +372,7 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
|
|
| 365 |
</div>
|
| 366 |
</div>
|
| 367 |
""", padding = False)
|
| 368 |
-
|
| 369 |
# Row with audio player
|
| 370 |
with gr.Row():
|
| 371 |
audio_player = gr.Audio(value= 'test.mp3', label="Audio", type="filepath", interactive=False, show_download_button = False, show_share_button = False)
|
|
@@ -394,34 +401,33 @@ with (gr.Blocks(theme=gr.themes.Soft(), css = css) as demo):
|
|
| 394 |
# Go back
|
| 395 |
previous_button.click(
|
| 396 |
previous_example,
|
| 397 |
-
inputs=[emotions, confidence, comments, participant_id],
|
| 398 |
-
outputs=[sentence_text, audio_player, emotions, confidence, ann_completed, comments],
|
| 399 |
)
|
| 400 |
|
| 401 |
# Go to the next example
|
| 402 |
next_button.click(
|
| 403 |
next_example,
|
| 404 |
-
inputs=[emotions, confidence, comments, participant_id],
|
| 405 |
-
outputs=[sentence_text, audio_player, emotions, confidence, ann_completed, comments],
|
| 406 |
-
)
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
|
| 411 |
-
lets_go.click(None, [], [ann_completed, total], js = js_progress_bar)
|
| 412 |
lets_go.click(deactivate_participant_id, [participant_id, lets_go], [participant_id, lets_go])
|
| 413 |
lets_go.click(activate_elements, [emotions, confidence, comments, next_button, previous_button], [emotions, confidence, comments, next_button, previous_button])
|
| 414 |
-
lets_go.click(load_first_example, inputs = [gr.Number(current_index["index"], visible = False), participant_id], outputs = [sentence_text, audio_player, emotions, confidence, ann_completed, comments])
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
|
|
|
| 425 |
|
| 426 |
demo.launch()
|
| 427 |
|
|
|
|
| 214 |
# Initialize an empty DataFrame to store annotations
|
| 215 |
annotations = pd.DataFrame(columns=['sample_id', 'sentence', 'emotion', 'confidence', 'comments'])
|
| 216 |
current_index = {"index": 0} # Dictionary to allow modifying inside functions
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
def get_storage():
|
| 222 |
#source: https://discuss.huggingface.co/t/accessing-data-folder-of-persistent-storage/46681/2
|
| 223 |
files = [
|
|
|
|
| 235 |
return files, f"{usage/(1024.0 ** 3):.3f}GB"
|
| 236 |
|
| 237 |
|
| 238 |
+
def load_first_example(index, participant_id, ann_completed):
|
| 239 |
""" Loads and first example and updates index"""
|
| 240 |
+
global annotations
|
| 241 |
+
global current_index
|
| 242 |
|
| 243 |
path_ann = f'{persistent_storage}/{participant_id}_annotations.csv'
|
| 244 |
print(path_ann)
|
| 245 |
if os.path.exists(path_ann):
|
| 246 |
+
annotations = pd.read_csv(path_ann)
|
| 247 |
+
|
| 248 |
current_index['index'] = len(annotations)
|
| 249 |
print('path was found')
|
| 250 |
print(annotations)
|
| 251 |
+
print(len(annotations))
|
| 252 |
+
ann_completed = gr.Number(len(annotations) - 1, visible=False)
|
| 253 |
+
print(len(annotations))
|
| 254 |
+
return ann_completed, *load_example(current_index["index"])
|
| 255 |
|
| 256 |
def load_example(index):
|
| 257 |
"""Loads the example in row #index from dataframe file_list.
|
|
|
|
| 266 |
annotations.iloc[index].to_dict() if index < len(annotations) else {"sample_id": row["SAMPLE ID"], "emotion": 'Blank', "confidence": 0,
|
| 267 |
"comments": ''}
|
| 268 |
)
|
| 269 |
+
print(previous_annotation)
|
| 270 |
return (sentence, audio_path, previous_annotation['emotion'], previous_annotation['confidence'], current_index['index'] + 1, previous_annotation["comments"])
|
| 271 |
|
| 272 |
|
| 273 |
+
def save_annotation(emotions, confidence, comments, participant_id, ann_completed):
|
| 274 |
"""Save the annotation for the current example."""
|
| 275 |
|
| 276 |
idx = current_index["index"]
|
|
|
|
| 284 |
[emotions, confidence, comments]
|
| 285 |
else:
|
| 286 |
annotations.loc[len(annotations)] = [sample_id, sentence, emotions, confidence, comments]
|
| 287 |
+
ann_completed = gr.Number(ann_completed + 1, visible=False)
|
| 288 |
annotations.to_csv(f"{persistent_storage}/{participant_id}_annotations.csv", index=False) # Save to a CSV file
|
| 289 |
|
| 290 |
+
return ann_completed
|
| 291 |
|
| 292 |
+
def next_example(emotions, confidence, comments, participant_id, ann_completed):
|
| 293 |
"""Move to the next example."""
|
| 294 |
if emotions == "Blank":
|
| 295 |
gr.Warning("Please fill out the emotion section. 'Blank' is not a valid emotion.")
|
| 296 |
else:
|
| 297 |
|
| 298 |
+
ann_completed = save_annotation(emotions, confidence, comments, participant_id, ann_completed)
|
| 299 |
if current_index["index"] < len(file_list) - 1:
|
| 300 |
current_index["index"] += 1
|
| 301 |
+
return ann_completed, *load_example(current_index["index"])
|
| 302 |
|
| 303 |
+
def previous_example(emotion, confidence, comments, participant_id, ann_completed):
|
| 304 |
"""Move to the previous example."""
|
| 305 |
if emotion.value != "Blank":
|
| 306 |
+
ann_completed = save_annotation(emotion, confidence, comments, participant_id, ann_completed)
|
| 307 |
if current_index["index"] > 0:
|
| 308 |
current_index["index"] -= 1
|
| 309 |
+
return ann_completed, *load_example(current_index["index"])
|
| 310 |
+
return ann_completed, *load_example(current_index["index"])
|
| 311 |
|
| 312 |
def deactivate_participant_id(participant_id, lets_go):
|
| 313 |
participant_id = gr.Textbox(label='What is your participant ID?', value = participant_id, interactive = False)
|
|
|
|
| 358 |
angry_int_audio = gr.Audio(value=f'{persistent_storage}/emotion_examples/angry_intense.wav', label = "Anger (High Intensity)")
|
| 359 |
|
| 360 |
agreement = gr.Checkbox(value = False, label = "I agree", info = "I agree to have my annotations, comments, and questionnaire answers used for research purposes. I understand that any personal information will be anonymized.", interactive = True)
|
|
|
|
|
|
|
| 361 |
|
| 362 |
+
|
| 363 |
with gr.Tab("Annotation Interface"):
|
| 364 |
+
ann_completed = gr.Number(0, visible=False)
|
| 365 |
+
total = gr.Number(total_annotations, visible=False)
|
|
|
|
| 366 |
# Row with progress bar
|
| 367 |
+
|
| 368 |
gr.HTML("""
|
| 369 |
<div id="myProgress">
|
| 370 |
<div id="myBar">
|
|
|
|
| 372 |
</div>
|
| 373 |
</div>
|
| 374 |
""", padding = False)
|
| 375 |
+
|
| 376 |
# Row with audio player
|
| 377 |
with gr.Row():
|
| 378 |
audio_player = gr.Audio(value= 'test.mp3', label="Audio", type="filepath", interactive=False, show_download_button = False, show_share_button = False)
|
|
|
|
| 401 |
# Go back
|
| 402 |
previous_button.click(
|
| 403 |
previous_example,
|
| 404 |
+
inputs=[emotions, confidence, comments, participant_id, ann_completed],
|
| 405 |
+
outputs=[ ann_completed, sentence_text, audio_player, emotions, confidence, ann_completed, comments],
|
| 406 |
)
|
| 407 |
|
| 408 |
# Go to the next example
|
| 409 |
next_button.click(
|
| 410 |
next_example,
|
| 411 |
+
inputs=[emotions, confidence, comments, participant_id, ann_completed],
|
| 412 |
+
outputs=[ann_completed, sentence_text, audio_player, emotions, confidence, ann_completed, comments],
|
| 413 |
+
).then(None, [], [ann_completed, total], js = js_progress_bar)
|
| 414 |
+
|
| 415 |
+
|
|
|
|
| 416 |
|
|
|
|
| 417 |
lets_go.click(deactivate_participant_id, [participant_id, lets_go], [participant_id, lets_go])
|
| 418 |
lets_go.click(activate_elements, [emotions, confidence, comments, next_button, previous_button], [emotions, confidence, comments, next_button, previous_button])
|
| 419 |
+
lets_go.click(load_first_example, inputs = [gr.Number(current_index["index"], visible = False), participant_id, ann_completed], outputs = [ann_completed, sentence_text, audio_player, emotions, confidence, ann_completed, comments]).then( None, [], [ann_completed, total], js = js_progress_bar)
|
| 420 |
+
#lets_go.click(None, [], [ann_completed, total], js = js_progress_bar)
|
| 421 |
+
|
| 422 |
+
#with gr.Tab("Access Files"):
|
| 423 |
+
# with gr.Row():
|
| 424 |
+
# with gr.Column():
|
| 425 |
+
# btn = gr.Button("Run")
|
| 426 |
+
# with gr.Column():
|
| 427 |
+
# files = gr.Files(label="Files")
|
| 428 |
+
# storage = gr.Text(label="Total Usage")
|
| 429 |
+
# btn.click(get_storage, inputs=None, outputs=[files, storage], postprocess=False)
|
| 430 |
+
|
| 431 |
|
| 432 |
demo.launch()
|
| 433 |
|