Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ def create_reranking_interface(task_data):
|
|
13 |
"""Save the current set of rankings."""
|
14 |
try:
|
15 |
# Check if all documents have rankings
|
16 |
-
all_ranked = all(r is not None and r != ""
|
17 |
if not all_ranked:
|
18 |
return "⚠️ Please assign a rank to all documents before submitting", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
19 |
|
@@ -98,44 +98,60 @@ def create_reranking_interface(task_data):
|
|
98 |
label=f"Document {i+1}",
|
99 |
interactive=False
|
100 |
)
|
101 |
-
doc_containers.append(doc_box)
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
return
|
122 |
-
return
|
123 |
|
124 |
-
def
|
125 |
-
def
|
126 |
-
|
127 |
-
|
|
|
128 |
|
129 |
-
# Connect
|
130 |
-
|
131 |
-
|
132 |
-
inputs=[
|
133 |
-
outputs=[
|
134 |
)
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
)
|
140 |
|
141 |
with gr.Row():
|
@@ -149,7 +165,7 @@ def create_reranking_interface(task_data):
|
|
149 |
"""Load a specific sample into the interface."""
|
150 |
sample = next((s for s in samples if s["id"] == sample_id), None)
|
151 |
if not sample:
|
152 |
-
return [query_text.value] + [d.value for d in doc_containers] + [
|
153 |
|
154 |
# Update query
|
155 |
new_query = sample["query"]
|
@@ -161,7 +177,7 @@ def create_reranking_interface(task_data):
|
|
161 |
new_docs.append(doc)
|
162 |
|
163 |
# Initialize rankings
|
164 |
-
new_rankings = [
|
165 |
|
166 |
# Check if this sample has already been annotated
|
167 |
existing_annotation = next((a for a in results["annotations"] if a["sample_id"] == sample_id), None)
|
@@ -169,7 +185,7 @@ def create_reranking_interface(task_data):
|
|
169 |
# Restore previous rankings
|
170 |
for i, rank in enumerate(existing_annotation["rankings"]):
|
171 |
if i < len(new_rankings) and rank is not None:
|
172 |
-
new_rankings[i] = rank
|
173 |
|
174 |
# Update progress
|
175 |
current_idx = samples.index(sample)
|
|
|
13 |
"""Save the current set of rankings."""
|
14 |
try:
|
15 |
# Check if all documents have rankings
|
16 |
+
all_ranked = all(r is not None and r != "" for r in rankings)
|
17 |
if not all_ranked:
|
18 |
return "⚠️ Please assign a rank to all documents before submitting", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
19 |
|
|
|
98 |
label=f"Document {i+1}",
|
99 |
interactive=False
|
100 |
)
|
101 |
+
doc_containers.append(doc_box)
|
102 |
|
103 |
+
# Use Dropdown instead of Number for ranking
|
104 |
+
# Ranks from 1 to N (number of candidates)
|
105 |
+
rank_dropdown = gr.Dropdown(
|
106 |
+
choices=[str(j) for j in range(1, len(samples[0]["candidates"])+1)],
|
107 |
+
label=f"Rank",
|
108 |
+
value=None,
|
109 |
+
interactive=True
|
110 |
+
)
|
111 |
+
ranking_dropdowns.append(rank_dropdown)
|
112 |
+
|
113 |
+
# Add quick rank buttons
|
114 |
+
with gr.Row():
|
115 |
+
rank_high_btn = gr.Button("Rank High (1-3)", size="sm")
|
116 |
+
rank_med_btn = gr.Button("Rank Medium (4-7)", size="sm")
|
117 |
+
rank_low_btn = gr.Button("Rank Low (8+)", size="sm")
|
118 |
+
|
119 |
+
# Set ranks directly instead of incrementing/decrementing
|
120 |
+
# Helper functions to quickly set ranks in different ranges
|
121 |
+
def set_high_rank(i):
|
122 |
+
def set_rank():
|
123 |
+
return ["1", "2", "3"][min(i, 2)] # First 3 docs get ranks 1,2,3
|
124 |
+
return set_rank
|
125 |
|
126 |
+
def set_medium_rank(i):
|
127 |
+
def set_rank():
|
128 |
+
base = 4
|
129 |
+
return str(min(base + i % 4, len(samples[0]["candidates"])))
|
130 |
+
return set_rank
|
131 |
|
132 |
+
def set_low_rank(i):
|
133 |
+
def set_rank():
|
134 |
+
base = 8
|
135 |
+
return str(min(base + i % 10, len(samples[0]["candidates"])))
|
136 |
+
return set_rank
|
137 |
|
138 |
+
# Connect rank buttons
|
139 |
+
rank_high_btn.click(
|
140 |
+
set_high_rank(i),
|
141 |
+
inputs=[],
|
142 |
+
outputs=[rank_dropdown]
|
143 |
)
|
144 |
+
|
145 |
+
rank_med_btn.click(
|
146 |
+
set_medium_rank(i),
|
147 |
+
inputs=[],
|
148 |
+
outputs=[rank_dropdown]
|
149 |
+
)
|
150 |
+
|
151 |
+
rank_low_btn.click(
|
152 |
+
set_low_rank(i),
|
153 |
+
inputs=[],
|
154 |
+
outputs=[rank_dropdown]
|
155 |
)
|
156 |
|
157 |
with gr.Row():
|
|
|
165 |
"""Load a specific sample into the interface."""
|
166 |
sample = next((s for s in samples if s["id"] == sample_id), None)
|
167 |
if not sample:
|
168 |
+
return [query_text.value] + [d.value for d in doc_containers] + [None] * len(ranking_dropdowns) + [current_sample_id.value, progress_text.value, status_box.value]
|
169 |
|
170 |
# Update query
|
171 |
new_query = sample["query"]
|
|
|
177 |
new_docs.append(doc)
|
178 |
|
179 |
# Initialize rankings
|
180 |
+
new_rankings = [None] * len(ranking_dropdowns)
|
181 |
|
182 |
# Check if this sample has already been annotated
|
183 |
existing_annotation = next((a for a in results["annotations"] if a["sample_id"] == sample_id), None)
|
|
|
185 |
# Restore previous rankings
|
186 |
for i, rank in enumerate(existing_annotation["rankings"]):
|
187 |
if i < len(new_rankings) and rank is not None:
|
188 |
+
new_rankings[i] = str(rank) # Convert to string for dropdown
|
189 |
|
190 |
# Update progress
|
191 |
current_idx = samples.index(sample)
|