Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -305,25 +305,27 @@ def create_reranking_interface(task_data):
|
|
305 |
""")
|
306 |
|
307 |
def validate_rankings(*rankings):
|
308 |
-
"""
|
309 |
results = []
|
310 |
all_valid = True
|
311 |
for rank in rankings:
|
312 |
if rank is None or rank == "":
|
313 |
-
|
|
|
314 |
all_valid = False
|
315 |
else:
|
316 |
-
|
|
|
317 |
|
318 |
return results + [all_valid] # Return validation indicators and validity flag
|
319 |
|
320 |
def on_ranking_change(*rankings):
|
321 |
-
"""
|
322 |
validation_results = validate_rankings(*rankings)
|
323 |
return validation_results[:-1] # Return only the validation indicators
|
324 |
|
325 |
def submit_rankings(*args):
|
326 |
-
"""Submit rankings with
|
327 |
# Get the last argument (sample_id) and the rankings
|
328 |
if len(args) < 1:
|
329 |
return "Error: No arguments provided", progress_text.value
|
@@ -340,7 +342,8 @@ def create_reranking_interface(task_data):
|
|
340 |
all_valid = validation_results[-1] # Last item is validity flag
|
341 |
validation_indicators_values = validation_results[:-1] # Remove validity flag
|
342 |
|
343 |
-
# Update validation indicators
|
|
|
344 |
for i, result in enumerate(validation_indicators_values):
|
345 |
if i < len(validation_indicators):
|
346 |
validation_indicators[i].update(value=result)
|
@@ -357,13 +360,13 @@ def create_reranking_interface(task_data):
|
|
357 |
else:
|
358 |
dup_ranks[r] = [i]
|
359 |
|
360 |
-
#
|
361 |
for rank, indices in dup_ranks.items():
|
362 |
if len(indices) > 1:
|
363 |
for idx in indices:
|
364 |
if idx < len(validation_indicators):
|
365 |
validation_indicators[idx].update(
|
366 |
-
value=f
|
367 |
)
|
368 |
|
369 |
return "⚠️ Each document must have a unique rank. Please fix duplicate rankings.", progress_text.value
|
@@ -377,11 +380,11 @@ def create_reranking_interface(task_data):
|
|
377 |
# Save the validated rankings
|
378 |
status, progress = save_ranking(rankings, sample_id)
|
379 |
|
380 |
-
# Provide clear success feedback
|
381 |
if "✅" in status:
|
382 |
for i in range(len(validation_indicators)):
|
383 |
validation_indicators[i].update(
|
384 |
-
value=
|
385 |
)
|
386 |
|
387 |
return status, progress
|
@@ -487,14 +490,22 @@ def create_reranking_interface(task_data):
|
|
487 |
|
488 |
# Define functions for the quick ranking buttons
|
489 |
def assign_sequential_ranks():
|
490 |
-
|
|
|
|
|
491 |
|
492 |
def assign_reverse_ranks():
|
493 |
n = len(samples[0]["candidates"])
|
494 |
-
|
|
|
|
|
495 |
|
496 |
def clear_rankings():
|
497 |
-
|
|
|
|
|
|
|
|
|
498 |
|
499 |
# Connect quick ranking buttons
|
500 |
sequential_btn.click(
|
@@ -524,23 +535,37 @@ def create_reranking_interface(task_data):
|
|
524 |
|
525 |
# Auto-save and navigate events
|
526 |
def handle_next(current_id, auto_save, *rankings):
|
527 |
-
# First, handle auto-save
|
528 |
-
|
529 |
-
|
|
|
|
|
|
|
|
|
|
|
530 |
outputs = load_sample(new_id)
|
531 |
-
#
|
532 |
-
|
533 |
-
|
|
|
|
|
534 |
return outputs
|
535 |
|
536 |
def handle_prev(current_id, auto_save, *rankings):
|
537 |
-
# First, handle auto-save
|
538 |
-
|
539 |
-
|
|
|
|
|
|
|
|
|
|
|
540 |
outputs = load_sample(new_id)
|
541 |
-
#
|
542 |
-
|
543 |
-
|
|
|
|
|
544 |
return outputs
|
545 |
|
546 |
# Connect navigation with Gradio 3.x syntax
|
@@ -573,34 +598,14 @@ def create_reranking_interface(task_data):
|
|
573 |
outputs=[auto_save_enabled]
|
574 |
)
|
575 |
|
576 |
-
#
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
# Add a real-time validation for the entire set to check for duplicates
|
585 |
-
def validate_all_inputs(*rankings):
|
586 |
-
"""Check all inputs for duplicate ranks and provide feedback."""
|
587 |
-
validation_results = validate_rankings(*rankings)
|
588 |
-
all_valid = validation_results[-1]
|
589 |
-
validation_indicators_values = validation_results[:-1]
|
590 |
-
|
591 |
-
# Show clear button status based on validation
|
592 |
-
submit_status = "Ready to submit" if all_valid else "Please assign unique ranks to all documents"
|
593 |
-
|
594 |
-
return validation_indicators_values + [submit_status]
|
595 |
|
596 |
-
# Connect this validation to all ranking inputs
|
597 |
-
for ranking in ranking_inputs:
|
598 |
-
ranking.change(
|
599 |
-
fn=validate_all_inputs,
|
600 |
-
inputs=ranking_inputs,
|
601 |
-
outputs=validation_indicators + [status_box]
|
602 |
-
)
|
603 |
-
|
604 |
# Helper function for ranking - sort documents by rankings
|
605 |
def rank_by_relevance(*args):
|
606 |
"""Sorts the documents by their current rankings for a clearer view."""
|
@@ -991,7 +996,7 @@ def create_main_app():
|
|
991 |
|
992 |
return app
|
993 |
|
994 |
-
# Create app
|
995 |
demo = create_main_app()
|
996 |
|
997 |
if __name__ == "__main__":
|
|
|
305 |
""")
|
306 |
|
307 |
def validate_rankings(*rankings):
|
308 |
+
"""Simplified validation with less HTML for better performance."""
|
309 |
results = []
|
310 |
all_valid = True
|
311 |
for rank in rankings:
|
312 |
if rank is None or rank == "":
|
313 |
+
# Use simpler HTML with less styling for faster rendering
|
314 |
+
results.append("⚠️ Missing")
|
315 |
all_valid = False
|
316 |
else:
|
317 |
+
# Use simpler HTML with less styling for faster rendering
|
318 |
+
results.append("✓ Rank " + str(rank))
|
319 |
|
320 |
return results + [all_valid] # Return validation indicators and validity flag
|
321 |
|
322 |
def on_ranking_change(*rankings):
|
323 |
+
"""Simplified validation for better performance."""
|
324 |
validation_results = validate_rankings(*rankings)
|
325 |
return validation_results[:-1] # Return only the validation indicators
|
326 |
|
327 |
def submit_rankings(*args):
|
328 |
+
"""Submit rankings with more efficient validation."""
|
329 |
# Get the last argument (sample_id) and the rankings
|
330 |
if len(args) < 1:
|
331 |
return "Error: No arguments provided", progress_text.value
|
|
|
342 |
all_valid = validation_results[-1] # Last item is validity flag
|
343 |
validation_indicators_values = validation_results[:-1] # Remove validity flag
|
344 |
|
345 |
+
# Update validation indicators - less frequently
|
346 |
+
# Only update if really needed
|
347 |
for i, result in enumerate(validation_indicators_values):
|
348 |
if i < len(validation_indicators):
|
349 |
validation_indicators[i].update(value=result)
|
|
|
360 |
else:
|
361 |
dup_ranks[r] = [i]
|
362 |
|
363 |
+
# Use simpler HTML for duplicate messages
|
364 |
for rank, indices in dup_ranks.items():
|
365 |
if len(indices) > 1:
|
366 |
for idx in indices:
|
367 |
if idx < len(validation_indicators):
|
368 |
validation_indicators[idx].update(
|
369 |
+
value=f"⚠️ Duplicate {rank}"
|
370 |
)
|
371 |
|
372 |
return "⚠️ Each document must have a unique rank. Please fix duplicate rankings.", progress_text.value
|
|
|
380 |
# Save the validated rankings
|
381 |
status, progress = save_ranking(rankings, sample_id)
|
382 |
|
383 |
+
# Provide clear success feedback - with simpler HTML
|
384 |
if "✅" in status:
|
385 |
for i in range(len(validation_indicators)):
|
386 |
validation_indicators[i].update(
|
387 |
+
value="✓ Saved"
|
388 |
)
|
389 |
|
390 |
return status, progress
|
|
|
490 |
|
491 |
# Define functions for the quick ranking buttons
|
492 |
def assign_sequential_ranks():
|
493 |
+
values = [str(i+1) for i in range(len(samples[0]["candidates"]))]
|
494 |
+
# Skip validation until all ranks are assigned
|
495 |
+
return values
|
496 |
|
497 |
def assign_reverse_ranks():
|
498 |
n = len(samples[0]["candidates"])
|
499 |
+
values = [str(n-i) for i in range(n)]
|
500 |
+
# Skip validation until all ranks are assigned
|
501 |
+
return values
|
502 |
|
503 |
def clear_rankings():
|
504 |
+
values = [""] * len(samples[0]["candidates"])
|
505 |
+
# Clear validation indicators when clearing rankings
|
506 |
+
for indicator in validation_indicators:
|
507 |
+
indicator.update(value="")
|
508 |
+
return values
|
509 |
|
510 |
# Connect quick ranking buttons
|
511 |
sequential_btn.click(
|
|
|
535 |
|
536 |
# Auto-save and navigate events
|
537 |
def handle_next(current_id, auto_save, *rankings):
|
538 |
+
# First, handle auto-save - only if needed
|
539 |
+
if auto_save and any(r != "" for r in rankings):
|
540 |
+
new_id, status, progress = auto_save_and_navigate("next", current_id, auto_save, *rankings)
|
541 |
+
else:
|
542 |
+
new_id = next_sample(current_id)
|
543 |
+
status, progress = "", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
544 |
+
|
545 |
+
# Then, load the new sample with minimal validation
|
546 |
outputs = load_sample(new_id)
|
547 |
+
# Update only status and progress if needed
|
548 |
+
if status:
|
549 |
+
outputs[-2] = progress
|
550 |
+
outputs[-1] = status
|
551 |
+
|
552 |
return outputs
|
553 |
|
554 |
def handle_prev(current_id, auto_save, *rankings):
|
555 |
+
# First, handle auto-save - only if needed
|
556 |
+
if auto_save and any(r != "" for r in rankings):
|
557 |
+
new_id, status, progress = auto_save_and_navigate("prev", current_id, auto_save, *rankings)
|
558 |
+
else:
|
559 |
+
new_id = prev_sample(current_id)
|
560 |
+
status, progress = "", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
561 |
+
|
562 |
+
# Then, load the new sample with minimal validation
|
563 |
outputs = load_sample(new_id)
|
564 |
+
# Update only status and progress if needed
|
565 |
+
if status:
|
566 |
+
outputs[-2] = progress
|
567 |
+
outputs[-1] = status
|
568 |
+
|
569 |
return outputs
|
570 |
|
571 |
# Connect navigation with Gradio 3.x syntax
|
|
|
598 |
outputs=[auto_save_enabled]
|
599 |
)
|
600 |
|
601 |
+
# Reduce frequency of validation
|
602 |
+
# Only connect validation to the first ranking input to reduce event handlers
|
603 |
+
ranking_inputs[0].change(
|
604 |
+
fn=on_ranking_change,
|
605 |
+
inputs=ranking_inputs,
|
606 |
+
outputs=validation_indicators
|
607 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
608 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
609 |
# Helper function for ranking - sort documents by rankings
|
610 |
def rank_by_relevance(*args):
|
611 |
"""Sorts the documents by their current rankings for a clearer view."""
|
|
|
996 |
|
997 |
return app
|
998 |
|
999 |
+
# Create the app
|
1000 |
demo = create_main_app()
|
1001 |
|
1002 |
if __name__ == "__main__":
|