Spaces:
Runtime error
Runtime error
File size: 11,326 Bytes
a83bf0d a7dac98 353c2cb a7dac98 353c2cb a7dac98 353c2cb a7dac98 a83bf0d a7dac98 353c2cb a7dac98 353c2cb a83bf0d 353c2cb a7dac98 a41f10a a7dac98 353c2cb a7dac98 353c2cb a7dac98 353c2cb a83bf0d 353c2cb a83bf0d 353c2cb a83bf0d 353c2cb a83bf0d 353c2cb a83bf0d 353c2cb a83bf0d 353c2cb a83bf0d 353c2cb a83bf0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | # import gradio as gr
# from data_handler import get_next_sample, annotate_text, get_generation_columns, load_progress
import gradio as gr
from data_handler import get_next_sample, annotate_text, get_generation_columns, load_progress
# Track the progress of all annotations
progress = load_progress() # Load previous progress from the progress file
annotation_history = list(progress.keys()) # Maintain a list of comment IDs in order of annotation
current_annotation_idx = -1 # Start from no previous annotation
def update_ui(dataset, generation_columns):
"""Update UI with the next sample or show completion message."""
sample = get_next_sample(dataset, progress)
if sample is None:
return gr.update(visible=False), gr.update(value="All comments annotated!"), gr.update(visible=False)
# Ensure to return the correct number of outputs for Gradio (textboxes + comment_id)
textboxes = [gr.update(value=sample[col]) for col in generation_columns]
return textboxes + [gr.update(value=sample["comment_id"])] # Return sample comment_id
def go_back_ui():
"""Go back to the previous annotated sample in the history."""
global current_annotation_idx
# Check if we have any previous annotations
if current_annotation_idx <= 0:
return gr.update(visible=False), gr.update(value="No previous annotations.")
# Move one step back
current_annotation_idx -= 1
# Get the previous annotated sample based on the current_annotation_idx
prev_annotated_id = annotation_history[current_annotation_idx]
prev_sample = progress[prev_annotated_id]
textboxes = [
gr.update(value=prev_sample["grammar"]),
gr.update(value=prev_sample["coherence"]),
gr.update(value=prev_sample["preferred_text"])
]
return textboxes + [gr.update(value=prev_annotated_id)] # Show the previous annotation
def create_ui(dataset, current_index):
"""Creates the Gradio UI for annotation."""
generation_columns = get_generation_columns(dataset)
with gr.Blocks() as demo:
gr.Markdown("## Annotate Generated Texts")
gr.Markdown("#### Rate each generation and select the best one")
textboxes, grammar_radios, coherence_radios = [], [], []
for col in generation_columns:
with gr.Row():
with gr.Column(scale=3):
gr.Markdown(f"### {col}")
text_input = gr.Textbox(label=f"Generated Text ({col})", lines=5)
textboxes.append(text_input)
with gr.Column(scale=1):
grammar = gr.Radio(choices=["-2", "-1", "0", "1", "2"], label="Grammar", interactive=True)
coherence = gr.Radio(choices=["-2", "-1", "0", "1", "2"], label="Coherence", interactive=True)
grammar_radios.append(grammar)
coherence_radios.append(coherence)
preferred_text = gr.Dropdown(choices=generation_columns, label="Select Best Generation")
comment_id_box = gr.Textbox(label="Comment ID", interactive=False) # Store comment_id
with gr.Row():
submit_btn = gr.Button("Submit Annotation")
go_back_btn = gr.Button("Go Back to Last Annotation")
output_message = gr.Textbox(label="Status", interactive=False)
# Set initial values
demo.load(lambda: update_ui(dataset, generation_columns), outputs=textboxes + [comment_id_box])
# Define button actions
submit_btn.click(
lambda grammar_scores, coherence_scores, preferred, comment_id: annotate_text(
dataset, comment_id, grammar_scores, coherence_scores, preferred, generation_columns, progress
),
inputs=grammar_radios + coherence_radios + [preferred_text, comment_id_box],
outputs=textboxes + [output_message]
)
go_back_btn.click(
go_back_ui,
outputs=textboxes + [comment_id_box, output_message]
)
return demo
# # Track the progress of all annotations
# progress = load_progress() # Load previous progress from the progress file
# annotation_history = list(progress.keys()) # Maintain a list of comment IDs in order of annotation
# current_annotation_idx = -1 # Start from no previous annotation
# def update_ui(dataset, generation_columns):
# """Update UI with the next sample or show completion message."""
# sample = get_next_sample(dataset, progress)
# if sample is None:
# return gr.update(visible=False), gr.update(value="All comments annotated!"), gr.update(visible=False)
# textboxes = [gr.update(value=sample[col]) for col in generation_columns]
# return textboxes + [gr.update(value=sample["comment_id"])] # Return sample comment_id
# def go_back_ui():
# """Go back to the previous annotated sample in the history."""
# global current_annotation_idx
# # Check if we have any previous annotations
# if current_annotation_idx <= 0:
# return gr.update(visible=False), gr.update(value="No previous annotations.")
# # Move one step back
# current_annotation_idx -= 1
# # Get the previous annotated sample based on the current_annotation_idx
# prev_annotated_id = annotation_history[current_annotation_idx]
# prev_sample = progress[prev_annotated_id]
# textboxes = [gr.update(value=prev_sample["grammar"]),
# gr.update(value=prev_sample["coherence"]),
# gr.update(value=prev_sample["preferred_text"])]
# return textboxes + [gr.update(value=prev_annotated_id)] # Show the previous annotation
# def create_ui(dataset, current_index):
# """Creates the Gradio UI for annotation."""
# generation_columns = get_generation_columns(dataset)
# with gr.Blocks() as demo:
# gr.Markdown("## Annotate Generated Texts")
# gr.Markdown("#### Rate each generation and select the best one")
# textboxes, grammar_radios, coherence_radios = [], [], []
# for col in generation_columns:
# with gr.Row():
# with gr.Column(scale=3):
# gr.Markdown(f"### {col}")
# text_input = gr.Textbox(label=f"Generated Text ({col})", lines=5)
# textboxes.append(text_input)
# with gr.Column(scale=1):
# grammar = gr.Radio(choices=["-2", "-1", "0", "1", "2"], label="Grammar", interactive=True)
# coherence = gr.Radio(choices=["-2", "-1", "0", "1", "2"], label="Coherence", interactive=True)
# grammar_radios.append(grammar)
# coherence_radios.append(coherence)
# preferred_text = gr.Dropdown(choices=generation_columns, label="Select Best Generation")
# comment_id_box = gr.Textbox(label="Comment ID", interactive=False) # Store comment_id
# with gr.Row():
# submit_btn = gr.Button("Submit Annotation")
# go_back_btn = gr.Button("Go Back to Last Annotation")
# output_message = gr.Textbox(label="Status", interactive=False)
# # Set initial values
# demo.load(lambda: update_ui(dataset, generation_columns), outputs=textboxes + [comment_id_box])
# # Define button actions
# submit_btn.click(
# lambda grammar_scores, coherence_scores, preferred, comment_id: annotate_text(
# dataset, comment_id, grammar_scores, coherence_scores, preferred, generation_columns, progress
# ),
# inputs=grammar_radios + coherence_radios + [preferred_text, comment_id_box],
# outputs=textboxes + [output_message]
# )
# go_back_btn.click(
# go_back_ui,
# outputs=textboxes + [comment_id_box, output_message]
# )
# return demo
# # # Store the history of annotated samples
# # last_sample = None # To store the previous sample for "Go Back"
# # def update_ui(dataset, generation_columns):
# # """Update UI with the next sample or show completion message."""
# # global last_sample
# # sample = get_next_sample(dataset)
# # if sample is None:
# # return gr.update(visible=False), gr.update(value="All comments annotated!"), gr.update(visible=False)
# # last_sample = sample # Store the current sample for going back
# # textboxes = [gr.update(value=sample[col]) for col in generation_columns]
# # return textboxes + [gr.update(value=sample["comment_id"])] # Return sample comment_id
# # def go_back_ui(dataset, generation_columns):
# # """Return the previous annotated sample (if available)."""
# # global last_sample
# # if last_sample is None:
# # return gr.update(visible=False), gr.update(value="No previous sample.")
# # textboxes = [gr.update(value=last_sample[col]) for col in generation_columns]
# # return textboxes + [gr.update(value=last_sample["comment_id"])] # Display previous comment_id
# # def create_ui(dataset):
# # """Creates the Gradio UI for annotation."""
# # generation_columns = get_generation_columns(dataset)
# # with gr.Blocks() as demo:
# # gr.Markdown("## Annotate Generated Texts")
# # gr.Markdown("#### Rate each generation and select the best one")
# # textboxes, grammar_radios, coherence_radios = [], [], []
# # for col in generation_columns:
# # with gr.Row():
# # with gr.Column(scale=3):
# # gr.Markdown(f"### {col}")
# # text_input = gr.Textbox(label=f"Generated Text ({col})", lines=5)
# # textboxes.append(text_input)
# # with gr.Column(scale=1):
# # grammar = gr.Radio(choices=["-2", "-1", "0", "1", "2"], label="Grammar", interactive=True)
# # coherence = gr.Radio(choices=["-2", "-1", "0", "1", "2"], label="Coherence", interactive=True)
# # grammar_radios.append(grammar)
# # coherence_radios.append(coherence)
# # preferred_text = gr.Dropdown(choices=generation_columns, label="Select Best Generation")
# # comment_id_box = gr.Textbox(label="Comment ID", interactive=False) # Store comment_id
# # with gr.Row():
# # submit_btn = gr.Button("Submit Annotation")
# # go_back_btn = gr.Button("Go Back to Last Annotation")
# # output_message = gr.Textbox(label="Status", interactive=False)
# # # Set initial values
# # demo.load(lambda: update_ui(dataset, generation_columns), outputs=textboxes + [comment_id_box])
# # # Define button actions
# # submit_btn.click(
# # lambda grammar_scores, coherence_scores, preferred, comment_id: annotate_text(
# # dataset, comment_id, grammar_scores, coherence_scores, preferred, generation_columns
# # ),
# # inputs=grammar_radios + coherence_radios + [preferred_text, comment_id_box],
# # outputs=textboxes + [output_message]
# # )
# # go_back_btn.click(
# # lambda: go_back_ui(dataset, generation_columns),
# # outputs=textboxes + [comment_id_box, output_message]
# # )
# # return demo |