Spaces:
Sleeping
Sleeping
provide an option to enter ID
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ INSTRUCTIONS = """
|
|
| 25 |
Please compare the two model outputs shown below and select which one you think is better.
|
| 26 |
- Choose "A is better" if the output from Model A (left) is superior
|
| 27 |
- Choose "B is better" if the output from Model B (right) is superior
|
| 28 |
-
- Choose "Tie" if
|
| 29 |
- Choose "Can't choose" if you cannot make a determination
|
| 30 |
"""
|
| 31 |
SAVE_EVERY_N_EXAMPLES = 5
|
|
@@ -35,11 +35,12 @@ class PairwiseLabeler:
|
|
| 35 |
def __init__(self):
|
| 36 |
self.current_index = defaultdict(int)
|
| 37 |
self.results = defaultdict(list)
|
| 38 |
-
self.
|
| 39 |
-
self.
|
|
|
|
| 40 |
|
| 41 |
def __len__(self):
|
| 42 |
-
return len(self.
|
| 43 |
|
| 44 |
def read_hf_dataset(self) -> pd.DataFrame:
|
| 45 |
try:
|
|
@@ -70,13 +71,16 @@ class PairwiseLabeler:
|
|
| 70 |
return pd.DataFrame(sample_data)
|
| 71 |
|
| 72 |
def get_current_pair(self, session_id):
|
| 73 |
-
if
|
|
|
|
|
|
|
|
|
|
| 74 |
if self.has_url_column:
|
| 75 |
return None, None, None, None
|
| 76 |
else:
|
| 77 |
return None, None, None
|
| 78 |
|
| 79 |
-
item = self.df.iloc[self.current_index[session_id]]
|
| 80 |
item_id = item.get(HF_INPUT_DATASET_ID_COLUMN, f"item_{self.current_index[session_id]}")
|
| 81 |
left_text = item.get(HF_INPUT_DATASET_COLUMN_A, "")
|
| 82 |
right_text = item.get(HF_INPUT_DATASET_COLUMN_B, "")
|
|
@@ -94,11 +98,6 @@ class PairwiseLabeler:
|
|
| 94 |
else:
|
| 95 |
return item_id, left_text, right_text, self.current_index[session_id]
|
| 96 |
|
| 97 |
-
# Get the current URL if available
|
| 98 |
-
current_url = None
|
| 99 |
-
if self.has_url_column:
|
| 100 |
-
current_url = self.df.iloc[self.current_index[session_id]].get(HF_INPUT_DATASET_URL_COLUMN, "")
|
| 101 |
-
|
| 102 |
# Record the judgment
|
| 103 |
result = {
|
| 104 |
"item_id": item_id,
|
|
@@ -106,7 +105,7 @@ class PairwiseLabeler:
|
|
| 106 |
"timestamp": datetime.datetime.now().isoformat(),
|
| 107 |
"labeler_id": session_id
|
| 108 |
}
|
| 109 |
-
|
| 110 |
self.results[session_id].append(result)
|
| 111 |
|
| 112 |
# Move to next item
|
|
@@ -159,27 +158,45 @@ class PairwiseLabeler:
|
|
| 159 |
# Initialize the labeler
|
| 160 |
labeler = PairwiseLabeler()
|
| 161 |
|
| 162 |
-
# Create a
|
| 163 |
-
def
|
| 164 |
return str(uuid.uuid4())[:8]
|
| 165 |
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
# State for the session ID
|
| 168 |
session_id = gr.State(value=None)
|
|
|
|
| 169 |
|
| 170 |
-
#
|
| 171 |
gr.Markdown(INSTRUCTIONS)
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
# URL display component - only shown if URL column is defined
|
| 174 |
-
|
| 175 |
if labeler.has_url_column:
|
| 176 |
-
url_display = gr.HTML(label="Reference URL")
|
| 177 |
-
|
| 178 |
-
session_id_display = gr.Textbox(label="Session Information", interactive=False)
|
| 179 |
|
|
|
|
| 180 |
with gr.Row():
|
| 181 |
with gr.Column():
|
| 182 |
left_output = gr.Textbox(
|
|
|
|
| 183 |
label="Model A Output",
|
| 184 |
lines=10,
|
| 185 |
interactive=False
|
|
@@ -187,68 +204,109 @@ with gr.Blocks() as app:
|
|
| 187 |
|
| 188 |
with gr.Column():
|
| 189 |
right_output = gr.Textbox(
|
|
|
|
| 190 |
label="Model B Output",
|
| 191 |
lines=10,
|
| 192 |
interactive=False
|
| 193 |
)
|
| 194 |
|
| 195 |
-
item_id = gr.Textbox(visible=False)
|
| 196 |
|
|
|
|
| 197 |
with gr.Row():
|
| 198 |
-
left_btn = gr.Button("⬅️ A is better", variant="primary")
|
| 199 |
-
right_btn = gr.Button("➡️ B is better", variant="primary")
|
| 200 |
-
tie_btn = gr.Button("🤝 Tie", variant="primary")
|
| 201 |
-
cant_choose_btn = gr.Button("🤔 Can't choose")
|
| 202 |
-
|
| 203 |
-
current_sample_sld = gr.Slider(minimum=0, maximum=len(labeler), step=1,
|
| 204 |
-
interactive=False,
|
| 205 |
-
label='sample_ind',
|
| 206 |
-
info=f"Samples labeled (out of {len(labeler)})",
|
| 207 |
-
show_label=False,
|
| 208 |
-
container=False,
|
| 209 |
-
scale=5)
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
# Initialize the session and get the first pair
|
| 212 |
-
def init_session():
|
| 213 |
-
|
|
|
|
| 214 |
|
|
|
|
| 215 |
if labeler.has_url_column:
|
| 216 |
-
initial_id, initial_left, initial_right, initial_url = labeler.get_current_pair(
|
| 217 |
url_html = f'<a href="{initial_url}" target="_blank">{initial_url}</a>' if initial_url else ""
|
| 218 |
|
| 219 |
return (
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
)
|
| 228 |
else:
|
| 229 |
-
initial_id, initial_left, initial_right = labeler.get_current_pair(
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
return (
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
)
|
| 239 |
|
| 240 |
-
#
|
| 241 |
if labeler.has_url_column:
|
| 242 |
-
|
| 243 |
init_session,
|
| 244 |
-
inputs=
|
| 245 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
)
|
| 247 |
else:
|
| 248 |
-
|
| 249 |
init_session,
|
| 250 |
-
inputs=
|
| 251 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
)
|
| 253 |
|
| 254 |
def judge_left(session_id, item_id, left_text, right_text):
|
|
@@ -264,17 +322,29 @@ with gr.Blocks() as app:
|
|
| 264 |
return judge("Can't choose", session_id, item_id, left_text, right_text)
|
| 265 |
|
| 266 |
def judge(choice, session_id, item_id, left_text, right_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
if labeler.has_url_column:
|
| 268 |
new_id, new_left, new_right, new_url, new_index = labeler.submit_judgment(
|
| 269 |
item_id, left_text, right_text, choice, session_id
|
| 270 |
)
|
| 271 |
url_html = f'<a href="{new_url}" target="_blank">{new_url}</a>' if new_url else ""
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
else:
|
| 274 |
new_id, new_left, new_right, new_index = labeler.submit_judgment(
|
| 275 |
item_id, left_text, right_text, choice, session_id
|
| 276 |
)
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
if labeler.has_url_column:
|
| 280 |
left_btn.click(
|
|
|
|
| 25 |
Please compare the two model outputs shown below and select which one you think is better.
|
| 26 |
- Choose "A is better" if the output from Model A (left) is superior
|
| 27 |
- Choose "B is better" if the output from Model B (right) is superior
|
| 28 |
+
- Choose "Tie" if they are equally good or bad
|
| 29 |
- Choose "Can't choose" if you cannot make a determination
|
| 30 |
"""
|
| 31 |
SAVE_EVERY_N_EXAMPLES = 5
|
|
|
|
| 35 |
def __init__(self):
|
| 36 |
self.current_index = defaultdict(int)
|
| 37 |
self.results = defaultdict(list)
|
| 38 |
+
self._df = self.read_hf_dataset()
|
| 39 |
+
self.df = {}
|
| 40 |
+
self.has_url_column = HF_INPUT_DATASET_URL_COLUMN and HF_INPUT_DATASET_URL_COLUMN in self._df.columns
|
| 41 |
|
| 42 |
def __len__(self):
|
| 43 |
+
return len(self._df)
|
| 44 |
|
| 45 |
def read_hf_dataset(self) -> pd.DataFrame:
|
| 46 |
try:
|
|
|
|
| 71 |
return pd.DataFrame(sample_data)
|
| 72 |
|
| 73 |
def get_current_pair(self, session_id):
|
| 74 |
+
if session_id not in self.df:
|
| 75 |
+
self.df[session_id] = self._df.sample(frac=1).reset_index(drop=True)
|
| 76 |
+
|
| 77 |
+
if self.current_index[session_id] >= len(self.df[session_id]):
|
| 78 |
if self.has_url_column:
|
| 79 |
return None, None, None, None
|
| 80 |
else:
|
| 81 |
return None, None, None
|
| 82 |
|
| 83 |
+
item = self.df[session_id].iloc[self.current_index[session_id]]
|
| 84 |
item_id = item.get(HF_INPUT_DATASET_ID_COLUMN, f"item_{self.current_index[session_id]}")
|
| 85 |
left_text = item.get(HF_INPUT_DATASET_COLUMN_A, "")
|
| 86 |
right_text = item.get(HF_INPUT_DATASET_COLUMN_B, "")
|
|
|
|
| 98 |
else:
|
| 99 |
return item_id, left_text, right_text, self.current_index[session_id]
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# Record the judgment
|
| 102 |
result = {
|
| 103 |
"item_id": item_id,
|
|
|
|
| 105 |
"timestamp": datetime.datetime.now().isoformat(),
|
| 106 |
"labeler_id": session_id
|
| 107 |
}
|
| 108 |
+
|
| 109 |
self.results[session_id].append(result)
|
| 110 |
|
| 111 |
# Move to next item
|
|
|
|
| 158 |
# Initialize the labeler
|
| 159 |
labeler = PairwiseLabeler()
|
| 160 |
|
| 161 |
+
# Create a default session ID
|
| 162 |
+
def create_default_session():
|
| 163 |
return str(uuid.uuid4())[:8]
|
| 164 |
|
| 165 |
+
# Create a demo session ID for initial display
|
| 166 |
+
demo_session = create_default_session()
|
| 167 |
+
demo_item_id, demo_left_text, demo_right_text = None, "", ""
|
| 168 |
+
demo_url = None
|
| 169 |
+
if labeler.has_url_column:
|
| 170 |
+
demo_item_id, demo_left_text, demo_right_text, demo_url = labeler.get_current_pair(demo_session)
|
| 171 |
+
else:
|
| 172 |
+
demo_item_id, demo_left_text, demo_right_text = labeler.get_current_pair(demo_session)
|
| 173 |
+
|
| 174 |
+
with gr.Blocks(css="footer {visibility: hidden}") as app:
|
| 175 |
# State for the session ID
|
| 176 |
session_id = gr.State(value=None)
|
| 177 |
+
is_session_started = gr.State(value=False)
|
| 178 |
|
| 179 |
+
# Header with instructions
|
| 180 |
gr.Markdown(INSTRUCTIONS)
|
| 181 |
|
| 182 |
+
# Session and progress bar in one line
|
| 183 |
+
user_session_id = gr.Textbox(
|
| 184 |
+
placeholder="Enter your unique ID or leave blank for random",
|
| 185 |
+
label="Session ID",
|
| 186 |
+
scale=2
|
| 187 |
+
)
|
| 188 |
+
start_btn = gr.Button("Start Session", variant="primary", scale=1)
|
| 189 |
+
|
| 190 |
# URL display component - only shown if URL column is defined
|
| 191 |
+
print(labeler.has_url_column)
|
| 192 |
if labeler.has_url_column:
|
| 193 |
+
url_display = gr.HTML(label="Reference URL", value="", visible=True)
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
# Main content area (shown with placeholder text)
|
| 196 |
with gr.Row():
|
| 197 |
with gr.Column():
|
| 198 |
left_output = gr.Textbox(
|
| 199 |
+
value=demo_left_text,
|
| 200 |
label="Model A Output",
|
| 201 |
lines=10,
|
| 202 |
interactive=False
|
|
|
|
| 204 |
|
| 205 |
with gr.Column():
|
| 206 |
right_output = gr.Textbox(
|
| 207 |
+
value=demo_right_text,
|
| 208 |
label="Model B Output",
|
| 209 |
lines=10,
|
| 210 |
interactive=False
|
| 211 |
)
|
| 212 |
|
| 213 |
+
item_id = gr.Textbox(value=demo_item_id, visible=False)
|
| 214 |
|
| 215 |
+
# Buttons row (initially disabled)
|
| 216 |
with gr.Row():
|
| 217 |
+
left_btn = gr.Button("⬅️ A is better", variant="primary", interactive=False)
|
| 218 |
+
right_btn = gr.Button("➡️ B is better", variant="primary", interactive=False)
|
| 219 |
+
tie_btn = gr.Button("🤝 Tie", variant="primary", interactive=False)
|
| 220 |
+
cant_choose_btn = gr.Button("🤔 Can't choose", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
+
# Progress slider (initially disabled)
|
| 223 |
+
current_sample_sld = gr.Slider(
|
| 224 |
+
minimum=0,
|
| 225 |
+
maximum=len(labeler),
|
| 226 |
+
step=1,
|
| 227 |
+
value=0,
|
| 228 |
+
interactive=False,
|
| 229 |
+
label='Progress',
|
| 230 |
+
show_label=False
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
# Initialize the session and get the first pair
|
| 234 |
+
def init_session(entered_id):
|
| 235 |
+
# Use entered ID or generate a new one if empty
|
| 236 |
+
session_id_value = entered_id.strip() if entered_id and entered_id.strip() else create_default_session()
|
| 237 |
|
| 238 |
+
# Get the initial data for this session ID
|
| 239 |
if labeler.has_url_column:
|
| 240 |
+
initial_id, initial_left, initial_right, initial_url = labeler.get_current_pair(session_id_value)
|
| 241 |
url_html = f'<a href="{initial_url}" target="_blank">{initial_url}</a>' if initial_url else ""
|
| 242 |
|
| 243 |
return (
|
| 244 |
+
session_id_value, # session_id state
|
| 245 |
+
True, # is_session_started state
|
| 246 |
+
labeler.current_index[session_id_value], # current_sample_sld
|
| 247 |
+
url_html, # url_display
|
| 248 |
+
initial_left, # left_output
|
| 249 |
+
initial_right, # right_output
|
| 250 |
+
initial_id, # item_id
|
| 251 |
+
gr.update(interactive=True), # left_btn
|
| 252 |
+
gr.update(interactive=True), # right_btn
|
| 253 |
+
gr.update(interactive=True), # tie_btn
|
| 254 |
+
gr.update(interactive=True) # cant_choose_btn
|
| 255 |
)
|
| 256 |
else:
|
| 257 |
+
initial_id, initial_left, initial_right = labeler.get_current_pair(session_id_value)
|
| 258 |
+
|
| 259 |
+
# Create session info text
|
| 260 |
+
progress_text = f"Session: {session_id_value} | {labeler.current_index[session_id_value] + 1}/{len(labeler)}"
|
| 261 |
|
| 262 |
return (
|
| 263 |
+
session_id_value, # session_id state
|
| 264 |
+
True, # is_session_started state
|
| 265 |
+
labeler.current_index[session_id_value], # current_sample_sld
|
| 266 |
+
initial_left, # left_output
|
| 267 |
+
initial_right, # right_output
|
| 268 |
+
initial_id, # item_id
|
| 269 |
+
gr.update(interactive=True), # left_btn
|
| 270 |
+
gr.update(interactive=True), # right_btn
|
| 271 |
+
gr.update(interactive=True), # tie_btn
|
| 272 |
+
gr.update(interactive=True) # cant_choose_btn
|
| 273 |
)
|
| 274 |
|
| 275 |
+
# Connect the start button
|
| 276 |
if labeler.has_url_column:
|
| 277 |
+
start_btn.click(
|
| 278 |
init_session,
|
| 279 |
+
inputs=[user_session_id],
|
| 280 |
+
outputs=[
|
| 281 |
+
session_id,
|
| 282 |
+
is_session_started,
|
| 283 |
+
current_sample_sld,
|
| 284 |
+
url_display,
|
| 285 |
+
left_output,
|
| 286 |
+
right_output,
|
| 287 |
+
item_id,
|
| 288 |
+
left_btn,
|
| 289 |
+
right_btn,
|
| 290 |
+
tie_btn,
|
| 291 |
+
cant_choose_btn
|
| 292 |
+
]
|
| 293 |
)
|
| 294 |
else:
|
| 295 |
+
start_btn.click(
|
| 296 |
init_session,
|
| 297 |
+
inputs=[user_session_id],
|
| 298 |
+
outputs=[
|
| 299 |
+
session_id,
|
| 300 |
+
is_session_started,
|
| 301 |
+
current_sample_sld,
|
| 302 |
+
left_output,
|
| 303 |
+
right_output,
|
| 304 |
+
item_id,
|
| 305 |
+
left_btn,
|
| 306 |
+
right_btn,
|
| 307 |
+
tie_btn,
|
| 308 |
+
cant_choose_btn
|
| 309 |
+
]
|
| 310 |
)
|
| 311 |
|
| 312 |
def judge_left(session_id, item_id, left_text, right_text):
|
|
|
|
| 322 |
return judge("Can't choose", session_id, item_id, left_text, right_text)
|
| 323 |
|
| 324 |
def judge(choice, session_id, item_id, left_text, right_text):
|
| 325 |
+
if not session_id:
|
| 326 |
+
# Session not initialized, do nothing
|
| 327 |
+
return item_id, left_text, right_text, 0, "Not started"
|
| 328 |
+
|
| 329 |
if labeler.has_url_column:
|
| 330 |
new_id, new_left, new_right, new_url, new_index = labeler.submit_judgment(
|
| 331 |
item_id, left_text, right_text, choice, session_id
|
| 332 |
)
|
| 333 |
url_html = f'<a href="{new_url}" target="_blank">{new_url}</a>' if new_url else ""
|
| 334 |
+
|
| 335 |
+
# Update session info text with new progress
|
| 336 |
+
progress_text = f"Session: {session_id} | {new_index + 1}/{len(labeler)}" if new_index < len(labeler) else f"Session: {session_id} | Complete! {len(labeler)}/{len(labeler)}"
|
| 337 |
+
|
| 338 |
+
return new_id, new_left, new_right, url_html, new_index, progress_text
|
| 339 |
else:
|
| 340 |
new_id, new_left, new_right, new_index = labeler.submit_judgment(
|
| 341 |
item_id, left_text, right_text, choice, session_id
|
| 342 |
)
|
| 343 |
+
|
| 344 |
+
# Update session info text with new progress
|
| 345 |
+
progress_text = f"Session: {session_id} | {new_index + 1}/{len(labeler)}" if new_index < len(labeler) else f"Session: {session_id} | Complete! {len(labeler)}/{len(labeler)}"
|
| 346 |
+
|
| 347 |
+
return new_id, new_left, new_right, new_index, progress_text
|
| 348 |
|
| 349 |
if labeler.has_url_column:
|
| 350 |
left_btn.click(
|