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