Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -52,7 +52,58 @@ def create_reranking_interface(task_data):
|
|
52 |
# Return specific error message
|
53 |
return f"Error: {str(e)}", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
54 |
|
55 |
-
with gr.Blocks(theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
gr.Markdown(f"# {task_data['task_name']} - Human Reranking Evaluation")
|
57 |
|
58 |
with gr.Accordion("Instructions", open=True):
|
@@ -73,13 +124,13 @@ def create_reranking_interface(task_data):
|
|
73 |
|
74 |
current_sample_id = gr.State(value=samples[0]["id"])
|
75 |
|
76 |
-
with gr.Row():
|
77 |
progress_text = gr.Textbox(label="Progress", value=f"Progress: 0/{len(samples)}", interactive=False)
|
78 |
-
status_box = gr.Textbox(label="Status", value="Ready to start evaluation", interactive=False)
|
79 |
|
80 |
with gr.Group():
|
81 |
gr.Markdown("## Query:")
|
82 |
-
query_text = gr.Textbox(value=samples[0]["query"], label="", interactive=False)
|
83 |
|
84 |
gr.Markdown("## Documents (Arrange in order of relevance, most relevant at top):")
|
85 |
|
@@ -118,9 +169,14 @@ def create_reranking_interface(task_data):
|
|
118 |
for i in range(len(document_containers)):
|
119 |
if i < len(docs):
|
120 |
doc_idx = current_order[i]
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
122 |
else:
|
123 |
-
document_containers[i].value
|
124 |
|
125 |
# Status message
|
126 |
status = f"Viewing query {samples.index(sample) + 1} of {len(samples)}"
|
@@ -131,17 +187,32 @@ def create_reranking_interface(task_data):
|
|
131 |
|
132 |
# Create document display containers with up/down buttons
|
133 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
for i in range(20): # Now handling up to 20 documents per sample
|
135 |
with gr.Group():
|
136 |
-
with gr.
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
document_containers.append(doc_text)
|
139 |
|
140 |
-
with gr.Row():
|
141 |
-
# Create unique buttons for each document to avoid closure issues
|
142 |
-
up_btn = gr.Button(f"⬆️ Move Up #{i+1}", size="sm")
|
143 |
-
down_btn = gr.Button(f"⬇️ Move Down #{i+1}", size="sm")
|
144 |
-
|
145 |
# Create a closure that properly captures the current index
|
146 |
def make_up_handler(idx):
|
147 |
def up_handler():
|
@@ -157,11 +228,18 @@ def create_reranking_interface(task_data):
|
|
157 |
if sample:
|
158 |
docs = sample["candidates"]
|
159 |
updates = []
|
160 |
-
for j
|
161 |
-
if j < len(
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
else:
|
164 |
-
updates.append(gr.update(value=""))
|
165 |
return updates
|
166 |
return [gr.update() for _ in document_containers]
|
167 |
return up_handler
|
@@ -180,11 +258,18 @@ def create_reranking_interface(task_data):
|
|
180 |
if sample:
|
181 |
docs = sample["candidates"]
|
182 |
updates = []
|
183 |
-
for j
|
184 |
-
if j < len(
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
else:
|
187 |
-
updates.append(gr.update(value=""))
|
188 |
return updates
|
189 |
return [gr.update() for _ in document_containers]
|
190 |
return down_handler
|
@@ -202,12 +287,15 @@ def create_reranking_interface(task_data):
|
|
202 |
outputs=document_containers
|
203 |
)
|
204 |
|
205 |
-
with gr.Row():
|
206 |
-
prev_btn = gr.Button("← Previous Query", size="
|
207 |
-
submit_btn = gr.Button("Submit Rankings", size="lg", variant="primary")
|
208 |
-
next_btn = gr.Button("Next Query →", size="
|
209 |
|
210 |
-
|
|
|
|
|
|
|
211 |
|
212 |
# Initialize the document list for the first sample
|
213 |
status_val, progress_val = initialize_document_list(samples[0]["id"])
|
@@ -273,6 +361,40 @@ def create_reranking_interface(task_data):
|
|
273 |
outputs=[status_box, progress_text]
|
274 |
)
|
275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
# Save all results
|
277 |
def save_results():
|
278 |
"""Save all collected results to a file."""
|
|
|
52 |
# Return specific error message
|
53 |
return f"Error: {str(e)}", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
54 |
|
55 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
56 |
+
.document-text {
|
57 |
+
font-size: 16px;
|
58 |
+
padding: 10px;
|
59 |
+
border-radius: 8px;
|
60 |
+
background-color: #f8f9fa;
|
61 |
+
border-left: 5px solid #4CAF50;
|
62 |
+
}
|
63 |
+
.query-box {
|
64 |
+
background-color: #e3f2fd;
|
65 |
+
padding: 16px;
|
66 |
+
border-radius: 8px;
|
67 |
+
border-left: 5px solid #2196F3;
|
68 |
+
font-size: 18px;
|
69 |
+
margin-bottom: 20px;
|
70 |
+
}
|
71 |
+
.progress-indicator {
|
72 |
+
font-weight: bold;
|
73 |
+
text-align: center;
|
74 |
+
padding: 12px;
|
75 |
+
background-color: #f1f8e9;
|
76 |
+
border-radius: 8px;
|
77 |
+
margin: 10px 0;
|
78 |
+
}
|
79 |
+
.status-box {
|
80 |
+
font-weight: bold;
|
81 |
+
text-align: center;
|
82 |
+
}
|
83 |
+
.ranking-box {
|
84 |
+
border: 1px solid #e0e0e0;
|
85 |
+
border-radius: 8px;
|
86 |
+
padding: 15px;
|
87 |
+
margin-bottom: 12px;
|
88 |
+
transition: all 0.3s;
|
89 |
+
}
|
90 |
+
.ranking-box:hover {
|
91 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
92 |
+
}
|
93 |
+
/* Add different colors for rank levels */
|
94 |
+
.rank-1, .rank-2, .rank-3 {
|
95 |
+
border-left: 5px solid #4CAF50; /* Green for top ranks */
|
96 |
+
}
|
97 |
+
.rank-4, .rank-5, .rank-6, .rank-7 {
|
98 |
+
border-left: 5px solid #FFC107; /* Yellow for mid ranks */
|
99 |
+
}
|
100 |
+
.rank-8, .rank-9, .rank-10 {
|
101 |
+
border-left: 5px solid #FF5722; /* Orange for lower ranks */
|
102 |
+
}
|
103 |
+
.rank-11, .rank-12, .rank-13, .rank-14, .rank-15, .rank-16, .rank-17, .rank-18, .rank-19, .rank-20 {
|
104 |
+
border-left: 5px solid #9E9E9E; /* Gray for lowest ranks */
|
105 |
+
}
|
106 |
+
""") as demo:
|
107 |
gr.Markdown(f"# {task_data['task_name']} - Human Reranking Evaluation")
|
108 |
|
109 |
with gr.Accordion("Instructions", open=True):
|
|
|
124 |
|
125 |
current_sample_id = gr.State(value=samples[0]["id"])
|
126 |
|
127 |
+
with gr.Row(elem_classes=["progress-indicator"]):
|
128 |
progress_text = gr.Textbox(label="Progress", value=f"Progress: 0/{len(samples)}", interactive=False)
|
129 |
+
status_box = gr.Textbox(label="Status", value="Ready to start evaluation", interactive=False, elem_classes=["status-box"])
|
130 |
|
131 |
with gr.Group():
|
132 |
gr.Markdown("## Query:")
|
133 |
+
query_text = gr.Textbox(value=samples[0]["query"], label="", interactive=False, elem_classes=["query-box"])
|
134 |
|
135 |
gr.Markdown("## Documents (Arrange in order of relevance, most relevant at top):")
|
136 |
|
|
|
169 |
for i in range(len(document_containers)):
|
170 |
if i < len(docs):
|
171 |
doc_idx = current_order[i]
|
172 |
+
rank_class = f"rank-{i+1}"
|
173 |
+
document_containers[i].update(
|
174 |
+
value=f"[RANK {i+1}] Document {doc_idx+1}: {docs[doc_idx]}",
|
175 |
+
visible=True,
|
176 |
+
elem_classes=["document-text", rank_class]
|
177 |
+
)
|
178 |
else:
|
179 |
+
document_containers[i].update(value="", visible=False)
|
180 |
|
181 |
# Status message
|
182 |
status = f"Viewing query {samples.index(sample) + 1} of {len(samples)}"
|
|
|
187 |
|
188 |
# Create document display containers with up/down buttons
|
189 |
with gr.Column():
|
190 |
+
gr.Markdown("""
|
191 |
+
### Instructions for Ranking:
|
192 |
+
- Documents are initially shown in their original order
|
193 |
+
- The most relevant document should be at the TOP (Rank 1)
|
194 |
+
- Use the "Move Up" and "Move Down" buttons to rearrange documents
|
195 |
+
- The rank is shown at the beginning of each document: [RANK X]
|
196 |
+
- When you're satisfied with the order, click "Submit Rankings"
|
197 |
+
""")
|
198 |
+
|
199 |
for i in range(20): # Now handling up to 20 documents per sample
|
200 |
with gr.Group():
|
201 |
+
with gr.Box():
|
202 |
+
with gr.Row():
|
203 |
+
rank_label = gr.Markdown(f"**Document #{i+1}**")
|
204 |
+
with gr.Column(scale=1):
|
205 |
+
up_btn = gr.Button("⬆️ Move Up", size="sm", variant="primary")
|
206 |
+
down_btn = gr.Button("⬇️ Move Down", size="sm", variant="secondary")
|
207 |
+
|
208 |
+
doc_text = gr.Textbox(
|
209 |
+
label="",
|
210 |
+
interactive=False,
|
211 |
+
elem_id=f"doc-text-{i}",
|
212 |
+
elem_classes=["document-text"]
|
213 |
+
)
|
214 |
document_containers.append(doc_text)
|
215 |
|
|
|
|
|
|
|
|
|
|
|
216 |
# Create a closure that properly captures the current index
|
217 |
def make_up_handler(idx):
|
218 |
def up_handler():
|
|
|
228 |
if sample:
|
229 |
docs = sample["candidates"]
|
230 |
updates = []
|
231 |
+
for j in range(len(document_containers)):
|
232 |
+
if j < len(current_order) and j < len(docs):
|
233 |
+
doc_idx = current_order[j]
|
234 |
+
# Add rank-specific class for styling
|
235 |
+
rank_class = f"rank-{j+1}"
|
236 |
+
updates.append(gr.update(
|
237 |
+
value=f"[RANK {j+1}] Document {doc_idx+1}: {docs[doc_idx]}",
|
238 |
+
visible=True,
|
239 |
+
elem_classes=["document-text", rank_class]
|
240 |
+
))
|
241 |
else:
|
242 |
+
updates.append(gr.update(value="", visible=False))
|
243 |
return updates
|
244 |
return [gr.update() for _ in document_containers]
|
245 |
return up_handler
|
|
|
258 |
if sample:
|
259 |
docs = sample["candidates"]
|
260 |
updates = []
|
261 |
+
for j in range(len(document_containers)):
|
262 |
+
if j < len(current_order) and j < len(docs):
|
263 |
+
doc_idx = current_order[j]
|
264 |
+
# Add rank-specific class for styling
|
265 |
+
rank_class = f"rank-{j+1}"
|
266 |
+
updates.append(gr.update(
|
267 |
+
value=f"[RANK {j+1}] Document {doc_idx+1}: {docs[doc_idx]}",
|
268 |
+
visible=True,
|
269 |
+
elem_classes=["document-text", rank_class]
|
270 |
+
))
|
271 |
else:
|
272 |
+
updates.append(gr.update(value="", visible=False))
|
273 |
return updates
|
274 |
return [gr.update() for _ in document_containers]
|
275 |
return down_handler
|
|
|
287 |
outputs=document_containers
|
288 |
)
|
289 |
|
290 |
+
with gr.Row(equal_height=True):
|
291 |
+
prev_btn = gr.Button("← Previous Query", size="lg", variant="secondary")
|
292 |
+
submit_btn = gr.Button("💾 Submit Rankings", size="lg", variant="primary")
|
293 |
+
next_btn = gr.Button("Next Query →", size="lg", variant="secondary")
|
294 |
|
295 |
+
with gr.Row():
|
296 |
+
save_btn = gr.Button("💾 Save All Results", variant="primary", size="lg")
|
297 |
+
# Add a clear button to reset rankings
|
298 |
+
clear_btn = gr.Button("🔄 Reset Order", variant="secondary", size="lg")
|
299 |
|
300 |
# Initialize the document list for the first sample
|
301 |
status_val, progress_val = initialize_document_list(samples[0]["id"])
|
|
|
361 |
outputs=[status_box, progress_text]
|
362 |
)
|
363 |
|
364 |
+
# Add function to reset the current ranking order
|
365 |
+
def reset_order():
|
366 |
+
nonlocal current_order
|
367 |
+
sample = next((s for s in samples if s["id"] == current_sample_id.value), None)
|
368 |
+
if not sample:
|
369 |
+
return [gr.update() for _ in document_containers]
|
370 |
+
|
371 |
+
docs = sample["candidates"]
|
372 |
+
# Reset to original order
|
373 |
+
current_order = list(range(len(docs)))
|
374 |
+
|
375 |
+
# Update UI
|
376 |
+
updates = []
|
377 |
+
for i in range(len(document_containers)):
|
378 |
+
if i < len(docs):
|
379 |
+
doc_idx = current_order[i]
|
380 |
+
rank_class = f"rank-{i+1}"
|
381 |
+
updates.append(gr.update(
|
382 |
+
value=f"[RANK {i+1}] Document {doc_idx+1}: {docs[doc_idx]}",
|
383 |
+
visible=True,
|
384 |
+
elem_classes=["document-text", rank_class]
|
385 |
+
))
|
386 |
+
else:
|
387 |
+
updates.append(gr.update(value="", visible=False))
|
388 |
+
|
389 |
+
return updates
|
390 |
+
|
391 |
+
# Connect reset button
|
392 |
+
clear_btn.click(
|
393 |
+
reset_order,
|
394 |
+
inputs=[],
|
395 |
+
outputs=document_containers
|
396 |
+
)
|
397 |
+
|
398 |
# Save all results
|
399 |
def save_results():
|
400 |
"""Save all collected results to a file."""
|