Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -301,7 +301,61 @@ def create_reranking_interface(task_data):
|
|
301 |
margin: 0 auto;
|
302 |
font-size: 12px !important;
|
303 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
""")
|
306 |
|
307 |
def validate_rankings(*rankings):
|
@@ -485,36 +539,75 @@ def create_reranking_interface(task_data):
|
|
485 |
except Exception as e:
|
486 |
return f"Error saving results: {str(e)}"
|
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 |
-
|
|
|
|
|
|
|
500 |
sequential_btn.click(
|
501 |
fn=assign_sequential_ranks,
|
502 |
inputs=None,
|
503 |
-
outputs=ranking_inputs
|
504 |
)
|
505 |
|
506 |
reverse_btn.click(
|
507 |
fn=assign_reverse_ranks,
|
508 |
inputs=None,
|
509 |
-
outputs=ranking_inputs
|
510 |
)
|
511 |
|
512 |
clear_btn.click(
|
513 |
fn=clear_rankings,
|
514 |
inputs=None,
|
515 |
-
outputs=ranking_inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
516 |
)
|
517 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
518 |
# Wire up events (Gradio 3.x syntax)
|
519 |
submit_btn.click(
|
520 |
fn=submit_rankings,
|
@@ -573,14 +666,6 @@ def create_reranking_interface(task_data):
|
|
573 |
outputs=[auto_save_enabled]
|
574 |
)
|
575 |
|
576 |
-
# Connect validation to ranking inputs for real-time feedback
|
577 |
-
for ranking in ranking_inputs:
|
578 |
-
ranking.change(
|
579 |
-
fn=on_ranking_change,
|
580 |
-
inputs=ranking_inputs,
|
581 |
-
outputs=validation_indicators
|
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."""
|
|
|
301 |
margin: 0 auto;
|
302 |
font-size: 12px !important;
|
303 |
}
|
304 |
+
|
305 |
+
/* Add styling for validation button */
|
306 |
+
.validate-btn {
|
307 |
+
margin-top: 10px !important;
|
308 |
+
width: 100% !important;
|
309 |
+
}
|
310 |
+
|
311 |
+
/* Add loading animation for dropdowns */
|
312 |
+
.rank-dropdown.loading select {
|
313 |
+
background-color: #f0f0f0 !important;
|
314 |
+
cursor: wait !important;
|
315 |
+
}
|
316 |
+
|
317 |
+
/* Quick tools improvements */
|
318 |
+
.tools-container button {
|
319 |
+
transition: all 0.3s ease !important;
|
320 |
+
}
|
321 |
+
|
322 |
+
.tools-container button:active {
|
323 |
+
transform: scale(0.98) !important;
|
324 |
+
}
|
325 |
+
|
326 |
+
/* Add this to make dropdown updates more responsive */
|
327 |
+
.rank-dropdown select {
|
328 |
+
transition: background-color 0.2s ease !important;
|
329 |
+
}
|
330 |
</style>
|
331 |
+
|
332 |
+
<script>
|
333 |
+
// Add simple loading state indication for quick rank buttons
|
334 |
+
function addLoadingBehavior() {
|
335 |
+
const quickButtons = document.querySelectorAll('.tools-container button');
|
336 |
+
if (quickButtons) {
|
337 |
+
quickButtons.forEach(btn => {
|
338 |
+
btn.addEventListener('click', function() {
|
339 |
+
// Show loading state in the button
|
340 |
+
const originalText = this.textContent;
|
341 |
+
this.textContent = "Applying...";
|
342 |
+
|
343 |
+
// Reset after a short delay
|
344 |
+
setTimeout(() => {
|
345 |
+
this.textContent = originalText;
|
346 |
+
}, 1000);
|
347 |
+
});
|
348 |
+
});
|
349 |
+
}
|
350 |
+
}
|
351 |
+
|
352 |
+
// Run when DOM is loaded
|
353 |
+
if (document.readyState === 'loading') {
|
354 |
+
document.addEventListener('DOMContentLoaded', addLoadingBehavior);
|
355 |
+
} else {
|
356 |
+
addLoadingBehavior();
|
357 |
+
}
|
358 |
+
</script>
|
359 |
""")
|
360 |
|
361 |
def validate_rankings(*rankings):
|
|
|
539 |
except Exception as e:
|
540 |
return f"Error saving results: {str(e)}"
|
541 |
|
542 |
+
# Define optimized functions for the quick ranking buttons
|
543 |
def assign_sequential_ranks():
|
544 |
+
"""Assign sequential rankings (1,2,3...) to all documents."""
|
545 |
+
sequential_ranks = [str(i+1) for i in range(len(samples[0]["candidates"]))]
|
546 |
+
# Return ranks plus a success message
|
547 |
+
return sequential_ranks + ["✅ Applied sequential ranking"]
|
548 |
|
549 |
def assign_reverse_ranks():
|
550 |
+
"""Assign reverse rankings (n,n-1...) to all documents."""
|
551 |
n = len(samples[0]["candidates"])
|
552 |
+
reverse_ranks = [str(n-i) for i in range(n)]
|
553 |
+
# Return ranks plus a success message
|
554 |
+
return reverse_ranks + ["✅ Applied reverse ranking"]
|
555 |
|
556 |
def clear_rankings():
|
557 |
+
"""Clear all rankings."""
|
558 |
+
empty_ranks = [""] * len(samples[0]["candidates"])
|
559 |
+
# Return empty ranks plus a success message
|
560 |
+
return empty_ranks + ["✅ Cleared all rankings"]
|
561 |
+
|
562 |
+
# Connect quick ranking buttons - add status message output
|
563 |
sequential_btn.click(
|
564 |
fn=assign_sequential_ranks,
|
565 |
inputs=None,
|
566 |
+
outputs=ranking_inputs + [status_box]
|
567 |
)
|
568 |
|
569 |
reverse_btn.click(
|
570 |
fn=assign_reverse_ranks,
|
571 |
inputs=None,
|
572 |
+
outputs=ranking_inputs + [status_box]
|
573 |
)
|
574 |
|
575 |
clear_btn.click(
|
576 |
fn=clear_rankings,
|
577 |
inputs=None,
|
578 |
+
outputs=ranking_inputs + [status_box]
|
579 |
+
)
|
580 |
+
|
581 |
+
# Optimize validation - reduce redundant validations
|
582 |
+
# Instead of connecting validation to each ranking input change,
|
583 |
+
# add a "Validate Rankings" button that runs validation on demand
|
584 |
+
|
585 |
+
with gr.Row():
|
586 |
+
validate_btn = gr.Button("Validate Rankings", variant="secondary")
|
587 |
+
|
588 |
+
# Connect validate button to the right function
|
589 |
+
validate_btn.click(
|
590 |
+
fn=lambda *rankings: validate_rankings(*rankings)[:-1] + ["Validation complete"],
|
591 |
+
inputs=ranking_inputs,
|
592 |
+
outputs=validation_indicators + [status_box]
|
593 |
)
|
594 |
|
595 |
+
# Add a simpler validation for individual inputs that doesn't cascade to all inputs
|
596 |
+
def validate_single_input(rank, index):
|
597 |
+
"""Lightweight validation for a single input."""
|
598 |
+
if rank is None or rank == "":
|
599 |
+
return '<span style="color: #dc3545;">⚠️</span>'
|
600 |
+
else:
|
601 |
+
return f'<span style="color: #28a745;">✓ {rank}</span>'
|
602 |
+
|
603 |
+
# Connect simpler validation to each input
|
604 |
+
for i, ranking in enumerate(ranking_inputs):
|
605 |
+
ranking.change(
|
606 |
+
fn=lambda r, idx=i: validate_single_input(r, idx),
|
607 |
+
inputs=[ranking],
|
608 |
+
outputs=[validation_indicators[i]]
|
609 |
+
)
|
610 |
+
|
611 |
# Wire up events (Gradio 3.x syntax)
|
612 |
submit_btn.click(
|
613 |
fn=submit_rankings,
|
|
|
666 |
outputs=[auto_save_enabled]
|
667 |
)
|
668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
669 |
# Add a real-time validation for the entire set to check for duplicates
|
670 |
def validate_all_inputs(*rankings):
|
671 |
"""Check all inputs for duplicate ranks and provide feedback."""
|