Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -100,10 +100,18 @@ def create_reranking_interface(task_data):
|
|
100 |
1. Read the query at the top
|
101 |
2. Review each document carefully
|
102 |
3. Assign a rank to each document (1 = most relevant, higher numbers = less relevant)
|
|
|
|
|
103 |
4. Each document must have a unique rank
|
104 |
-
5. Click "Submit Rankings"
|
105 |
6. Use "Previous" and "Next" to navigate between queries
|
106 |
-
7. Your rankings are automatically saved when you submit or navigate
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
""".format(instructions=task_data.get("instructions", "Rank documents by their relevance to the query.")))
|
108 |
|
109 |
# Hidden state variables
|
@@ -168,6 +176,15 @@ def create_reranking_interface(task_data):
|
|
168 |
)
|
169 |
doc_containers.append(doc_box)
|
170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
with gr.Column(scale=2):
|
172 |
# Dropdown for ranking
|
173 |
rank_input = gr.Dropdown(
|
@@ -198,27 +215,38 @@ def create_reranking_interface(task_data):
|
|
198 |
gr.HTML("""
|
199 |
<style>
|
200 |
.query-text textarea {
|
201 |
-
font-size:
|
202 |
font-weight: bold !important;
|
203 |
background-color: #f8f9fa !important;
|
204 |
border-left: 4px solid #2c7be5 !important;
|
205 |
padding-left: 10px !important;
|
|
|
206 |
}
|
207 |
|
208 |
.document-row {
|
209 |
border-bottom: 1px solid #e0e0e0;
|
210 |
-
padding:
|
211 |
-
margin-bottom:
|
212 |
}
|
213 |
|
214 |
.document-text textarea {
|
215 |
-
font-size:
|
216 |
-
line-height: 1.
|
|
|
217 |
}
|
218 |
|
219 |
.rank-dropdown select {
|
220 |
font-weight: bold !important;
|
|
|
221 |
text-align: center !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
}
|
223 |
|
224 |
.tool-button button {
|
@@ -253,20 +281,6 @@ def create_reranking_interface(task_data):
|
|
253 |
font-weight: bold;
|
254 |
margin: 0 auto;
|
255 |
}
|
256 |
-
|
257 |
-
.rank-dropdown select {
|
258 |
-
font-weight: bold !important;
|
259 |
-
font-size: 16px !important;
|
260 |
-
text-align: center !important;
|
261 |
-
padding: 8px !important;
|
262 |
-
border-radius: 5px !important;
|
263 |
-
border: 2px solid #2c7be5 !important;
|
264 |
-
}
|
265 |
-
|
266 |
-
.rank-dropdown select:focus {
|
267 |
-
border-color: #007bff !important;
|
268 |
-
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25) !important;
|
269 |
-
}
|
270 |
</style>
|
271 |
""")
|
272 |
|
@@ -637,126 +651,25 @@ def create_reranking_interface(task_data):
|
|
637 |
|
638 |
return [result]
|
639 |
|
640 |
-
#
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
# Add a ranking preview section that shows documents in their ranked order
|
653 |
-
def generate_ranking_preview(*rankings):
|
654 |
-
"""Creates a visual preview of current rankings."""
|
655 |
-
# Create list of (index, rank) pairs for valid rankings
|
656 |
-
ranked_docs = []
|
657 |
-
for i, rank in enumerate(rankings):
|
658 |
-
if rank and rank.strip():
|
659 |
-
try:
|
660 |
-
ranked_docs.append((i, int(rank)))
|
661 |
-
except:
|
662 |
-
continue
|
663 |
-
|
664 |
-
# Sort by rank
|
665 |
-
ranked_docs.sort(key=lambda x: x[1])
|
666 |
-
|
667 |
-
# Generate HTML for the preview
|
668 |
-
if not ranked_docs:
|
669 |
-
return "<p><i>No rankings assigned yet. Assign ranks to see a preview.</i></p>"
|
670 |
-
|
671 |
-
html = "<div class='ranking-preview'>"
|
672 |
-
html += "<h3>Current Ranking Preview</h3>"
|
673 |
-
html += "<ol class='ranked-docs'>"
|
674 |
-
|
675 |
-
for doc_idx, rank in ranked_docs:
|
676 |
-
if doc_idx < len(doc_containers):
|
677 |
-
doc_text = doc_containers[doc_idx].value
|
678 |
-
# Truncate if too long
|
679 |
-
if len(doc_text) > 100:
|
680 |
-
doc_text = doc_text[:97] + "..."
|
681 |
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
"""
|
689 |
-
|
690 |
-
html += "</ol></div>"
|
691 |
-
|
692 |
-
# Add CSS for the preview
|
693 |
-
html += """
|
694 |
-
<style>
|
695 |
-
.ranking-preview {
|
696 |
-
margin-top: 20px;
|
697 |
-
padding: 10px;
|
698 |
-
border: 1px solid #e0e0e0;
|
699 |
-
border-radius: 5px;
|
700 |
-
background-color: #f8f9fa;
|
701 |
-
}
|
702 |
-
|
703 |
-
.ranked-docs {
|
704 |
-
list-style-type: none;
|
705 |
-
padding: 0;
|
706 |
-
}
|
707 |
-
|
708 |
-
.ranked-doc {
|
709 |
-
display: flex;
|
710 |
-
align-items: center;
|
711 |
-
padding: 10px;
|
712 |
-
margin-bottom: 5px;
|
713 |
-
border: 1px solid #ddd;
|
714 |
-
border-radius: 5px;
|
715 |
-
background-color: white;
|
716 |
-
}
|
717 |
-
|
718 |
-
.rank-badge {
|
719 |
-
display: flex;
|
720 |
-
align-items: center;
|
721 |
-
justify-content: center;
|
722 |
-
width: 40px;
|
723 |
-
height: 40px;
|
724 |
-
border-radius: 50%;
|
725 |
-
background-color: #2c7be5;
|
726 |
-
color: white;
|
727 |
-
font-weight: bold;
|
728 |
-
margin-right: 10px;
|
729 |
-
}
|
730 |
-
|
731 |
-
.doc-index {
|
732 |
-
font-weight: bold;
|
733 |
-
width: 120px;
|
734 |
-
}
|
735 |
-
|
736 |
-
.doc-content {
|
737 |
-
flex-grow: 1;
|
738 |
-
overflow: hidden;
|
739 |
-
text-overflow: ellipsis;
|
740 |
-
}
|
741 |
-
</style>
|
742 |
-
"""
|
743 |
-
|
744 |
-
return html
|
745 |
-
|
746 |
-
# Add ranking preview
|
747 |
-
ranking_preview = gr.HTML("<p><i>No rankings assigned yet. Assign ranks to see a preview.</i></p>")
|
748 |
-
|
749 |
-
# Update the ranking preview whenever a ranking changes
|
750 |
-
for ranking in ranking_inputs:
|
751 |
-
ranking.change(
|
752 |
-
fn=generate_ranking_preview,
|
753 |
-
inputs=ranking_inputs,
|
754 |
-
outputs=[ranking_preview]
|
755 |
-
)
|
756 |
-
|
757 |
-
# Show preview section
|
758 |
-
with gr.Accordion("📊 Ranking Preview", open=True):
|
759 |
-
ranking_preview
|
760 |
|
761 |
return demo
|
762 |
|
|
|
100 |
1. Read the query at the top
|
101 |
2. Review each document carefully
|
102 |
3. Assign a rank to each document (1 = most relevant, higher numbers = less relevant)
|
103 |
+
- Use the rank buttons for quick ranking
|
104 |
+
- Or use the dropdown menus for more options
|
105 |
4. Each document must have a unique rank
|
106 |
+
5. Click "Submit Rankings" to save rankings for the current query
|
107 |
6. Use "Previous" and "Next" to navigate between queries
|
108 |
+
7. Your rankings are automatically saved when you submit or navigate (if auto-save is enabled)
|
109 |
+
8. Click "Save All Results" periodically to ensure all your work is saved to disk
|
110 |
+
|
111 |
+
**Button Explanations:**
|
112 |
+
- **Submit Rankings**: Saves rankings for the CURRENT query only
|
113 |
+
- **Save All Results**: Saves ALL submitted rankings to a file on disk
|
114 |
+
- **Auto-save**: When enabled, automatically saves rankings when navigating between queries
|
115 |
""".format(instructions=task_data.get("instructions", "Rank documents by their relevance to the query.")))
|
116 |
|
117 |
# Hidden state variables
|
|
|
176 |
)
|
177 |
doc_containers.append(doc_box)
|
178 |
|
179 |
+
# Add rank buttons for quick ranking
|
180 |
+
with gr.Column(scale=2):
|
181 |
+
# Create row of quick buttons for ranks 1-5
|
182 |
+
rank_buttons = []
|
183 |
+
with gr.Row():
|
184 |
+
for rank in range(1, min(6, len(samples[0]["candidates"])+1)):
|
185 |
+
btn = gr.Button(f"{rank}", size="sm")
|
186 |
+
rank_buttons.append((btn, rank))
|
187 |
+
|
188 |
with gr.Column(scale=2):
|
189 |
# Dropdown for ranking
|
190 |
rank_input = gr.Dropdown(
|
|
|
215 |
gr.HTML("""
|
216 |
<style>
|
217 |
.query-text textarea {
|
218 |
+
font-size: 18px !important;
|
219 |
font-weight: bold !important;
|
220 |
background-color: #f8f9fa !important;
|
221 |
border-left: 4px solid #2c7be5 !important;
|
222 |
padding-left: 10px !important;
|
223 |
+
line-height: 1.6 !important;
|
224 |
}
|
225 |
|
226 |
.document-row {
|
227 |
border-bottom: 1px solid #e0e0e0;
|
228 |
+
padding: 12px 0;
|
229 |
+
margin-bottom: 8px !important;
|
230 |
}
|
231 |
|
232 |
.document-text textarea {
|
233 |
+
font-size: 16px !important;
|
234 |
+
line-height: 1.6 !important;
|
235 |
+
padding: 10px !important;
|
236 |
}
|
237 |
|
238 |
.rank-dropdown select {
|
239 |
font-weight: bold !important;
|
240 |
+
font-size: 16px !important;
|
241 |
text-align: center !important;
|
242 |
+
padding: 8px !important;
|
243 |
+
border-radius: 5px !important;
|
244 |
+
border: 2px solid #2c7be5 !important;
|
245 |
+
}
|
246 |
+
|
247 |
+
.rank-dropdown select:focus {
|
248 |
+
border-color: #007bff !important;
|
249 |
+
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25) !important;
|
250 |
}
|
251 |
|
252 |
.tool-button button {
|
|
|
281 |
font-weight: bold;
|
282 |
margin: 0 auto;
|
283 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
</style>
|
285 |
""")
|
286 |
|
|
|
651 |
|
652 |
return [result]
|
653 |
|
654 |
+
# Connect rank buttons to their corresponding documents
|
655 |
+
for i, doc_rank_input in enumerate(ranking_inputs):
|
656 |
+
# Get the buttons for this row
|
657 |
+
row_buttons = rank_buttons[i*5:(i+1)*5] if i*5 < len(rank_buttons) else []
|
658 |
+
for btn, rank in row_buttons:
|
659 |
+
# Create a function to set this specific rank
|
660 |
+
def set_rank(rank_value=rank, doc_index=i):
|
661 |
+
def set_this_rank():
|
662 |
+
result = [""] * len(ranking_inputs)
|
663 |
+
result[doc_index] = str(rank_value)
|
664 |
+
return result
|
665 |
+
return set_this_rank
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
666 |
|
667 |
+
# Connect button to ranking input
|
668 |
+
btn.click(
|
669 |
+
fn=set_rank(),
|
670 |
+
inputs=None,
|
671 |
+
outputs=[doc_rank_input]
|
672 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
673 |
|
674 |
return demo
|
675 |
|