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 != "" 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,6 +98,8 @@ def create_reranking_interface(task_data):
|
|
98 |
label=f"Document {i+1}",
|
99 |
interactive=False
|
100 |
)
|
|
|
|
|
101 |
with gr.Column():
|
102 |
rank_number = gr.Number(
|
103 |
value=0, # Start with 0
|
@@ -107,18 +109,31 @@ def create_reranking_interface(task_data):
|
|
107 |
step=1,
|
108 |
precision=0 # Integer only
|
109 |
)
|
|
|
|
|
110 |
with gr.Row():
|
111 |
up_btn = gr.Button("↑", size="sm")
|
112 |
down_btn = gr.Button("↓", size="sm")
|
113 |
|
114 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
up_btn.click(
|
116 |
-
|
117 |
inputs=[rank_number],
|
118 |
outputs=[rank_number]
|
119 |
)
|
120 |
down_btn.click(
|
121 |
-
|
122 |
inputs=[rank_number],
|
123 |
outputs=[rank_number]
|
124 |
)
|
@@ -134,7 +149,7 @@ def create_reranking_interface(task_data):
|
|
134 |
"""Load a specific sample into the interface."""
|
135 |
sample = next((s for s in samples if s["id"] == sample_id), None)
|
136 |
if not sample:
|
137 |
-
return [query_text.value] + [d.value for d in doc_containers] + [
|
138 |
|
139 |
# Update query
|
140 |
new_query = sample["query"]
|
@@ -146,7 +161,7 @@ def create_reranking_interface(task_data):
|
|
146 |
new_docs.append(doc)
|
147 |
|
148 |
# Initialize rankings
|
149 |
-
new_rankings = [
|
150 |
|
151 |
# Check if this sample has already been annotated
|
152 |
existing_annotation = next((a for a in results["annotations"] if a["sample_id"] == sample_id), None)
|
@@ -154,7 +169,7 @@ def create_reranking_interface(task_data):
|
|
154 |
# Restore previous rankings
|
155 |
for i, rank in enumerate(existing_annotation["rankings"]):
|
156 |
if i < len(new_rankings) and rank is not None:
|
157 |
-
new_rankings[i] =
|
158 |
|
159 |
# Update progress
|
160 |
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 != "" and r != 0 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) # Add to doc_containers list
|
102 |
+
|
103 |
with gr.Column():
|
104 |
rank_number = gr.Number(
|
105 |
value=0, # Start with 0
|
|
|
109 |
step=1,
|
110 |
precision=0 # Integer only
|
111 |
)
|
112 |
+
ranking_dropdowns.append(rank_number) # Add to ranking_dropdowns list
|
113 |
+
|
114 |
with gr.Row():
|
115 |
up_btn = gr.Button("↑", size="sm")
|
116 |
down_btn = gr.Button("↓", size="sm")
|
117 |
|
118 |
+
# Create proper closures for the button handlers
|
119 |
+
def make_up_handler(idx):
|
120 |
+
def up_handler(x):
|
121 |
+
return max(1, x-1)
|
122 |
+
return up_handler
|
123 |
+
|
124 |
+
def make_down_handler(idx, max_val):
|
125 |
+
def down_handler(x):
|
126 |
+
return min(max_val, x+1)
|
127 |
+
return down_handler
|
128 |
+
|
129 |
+
# Connect up/down buttons to increase/decrease rank with proper handlers
|
130 |
up_btn.click(
|
131 |
+
make_up_handler(i),
|
132 |
inputs=[rank_number],
|
133 |
outputs=[rank_number]
|
134 |
)
|
135 |
down_btn.click(
|
136 |
+
make_down_handler(i, len(samples[0]["candidates"])),
|
137 |
inputs=[rank_number],
|
138 |
outputs=[rank_number]
|
139 |
)
|
|
|
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] + [0] * len(ranking_dropdowns) + [current_sample_id.value, progress_text.value, status_box.value]
|
153 |
|
154 |
# Update query
|
155 |
new_query = sample["query"]
|
|
|
161 |
new_docs.append(doc)
|
162 |
|
163 |
# Initialize rankings
|
164 |
+
new_rankings = [0] * len(ranking_dropdowns)
|
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 |
# 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)
|