AdnanElAssadi commited on
Commit
4121979
·
verified ·
1 Parent(s): eade391

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -37
app.py CHANGED
@@ -52,20 +52,6 @@ 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
- def move_document_up(idx):
56
- nonlocal current_order
57
- if idx > 0:
58
- # Swap with the document above
59
- current_order[idx], current_order[idx-1] = current_order[idx-1], current_order[idx]
60
- return gr.update(value=f"Moved document up: {idx}")
61
-
62
- def move_document_down(idx):
63
- nonlocal current_order
64
- if idx < len(current_order) - 1:
65
- # Swap with the document below
66
- current_order[idx], current_order[idx+1] = current_order[idx+1], current_order[idx]
67
- return gr.update(value=f"Moved document down: {idx}")
68
-
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
  gr.Markdown(f"# {task_data['task_name']} - Human Reranking Evaluation")
71
 
@@ -117,14 +103,24 @@ def create_reranking_interface(task_data):
117
  # Check if this sample has already been annotated to restore ordering
118
  existing_annotation = next((a for a in results["annotations"] if a["sample_id"] == sample_id), None)
119
  if existing_annotation and "rankings" in existing_annotation:
120
- # Sort documents based on previous rankings
121
- # This is a simplified version that just keeps the original order for now
122
- pass
 
 
 
 
 
 
 
123
 
124
  # Update UI
125
- for i, doc_idx in enumerate(current_order):
126
- if i < len(document_containers) and doc_idx < len(docs):
 
127
  document_containers[i].value = f"Document {doc_idx+1} (Rank: {i+1}): {docs[doc_idx]}"
 
 
128
 
129
  # Status message
130
  status = f"Viewing query {samples.index(sample) + 1} of {len(samples)}"
@@ -135,37 +131,75 @@ def create_reranking_interface(task_data):
135
 
136
  # Create document display containers with up/down buttons
137
  with gr.Column():
138
- for i in range(10): # Assuming max 10 documents per sample
139
  with gr.Group():
140
  with gr.Row():
141
  doc_text = gr.Textbox(label=f"Document {i+1}", interactive=False)
142
  document_containers.append(doc_text)
143
 
144
  with gr.Row():
145
- up_btn = gr.Button(f"⬆️ Move Up", size="sm")
146
- down_btn = gr.Button(f"⬇️ Move Down", size="sm")
 
147
 
148
- # Connect the up/down buttons
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  up_btn.click(
150
- lambda idx=i: move_document_up(idx),
151
  inputs=[],
152
- outputs=[status_box]
153
- ).then(
154
- lambda: [gr.update(value=f"Document {current_order[j]+1} (Rank: {j+1}): {samples[0]['candidates'][current_order[j]]}")
155
- for j in range(len(current_order))],
156
- inputs=[],
157
- outputs=document_containers[:len(current_order)]
158
  )
159
 
160
  down_btn.click(
161
- lambda idx=i: move_document_down(idx),
162
- inputs=[],
163
- outputs=[status_box]
164
- ).then(
165
- lambda: [gr.update(value=f"Document {current_order[j]+1} (Rank: {j+1}): {samples[0]['candidates'][current_order[j]]}")
166
- for j in range(len(current_order))],
167
  inputs=[],
168
- outputs=document_containers[:len(current_order)]
169
  )
170
 
171
  with gr.Row():
 
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()) as demo:
56
  gr.Markdown(f"# {task_data['task_name']} - Human Reranking Evaluation")
57
 
 
103
  # Check if this sample has already been annotated to restore ordering
104
  existing_annotation = next((a for a in results["annotations"] if a["sample_id"] == sample_id), None)
105
  if existing_annotation and "rankings" in existing_annotation:
106
+ # Create pairs of (doc_idx, rank)
107
+ ranked_docs = []
108
+ for doc_idx, rank in enumerate(existing_annotation["rankings"]):
109
+ ranked_docs.append((doc_idx, rank))
110
+
111
+ # Sort by rank (ascending)
112
+ ranked_docs.sort(key=lambda x: x[1])
113
+
114
+ # Extract document indices in rank order
115
+ current_order = [doc[0] for doc in ranked_docs]
116
 
117
  # Update UI
118
+ for i in range(len(document_containers)):
119
+ if i < len(docs):
120
+ doc_idx = current_order[i]
121
  document_containers[i].value = f"Document {doc_idx+1} (Rank: {i+1}): {docs[doc_idx]}"
122
+ else:
123
+ document_containers[i].value = "" # Clear unused containers
124
 
125
  # Status message
126
  status = f"Viewing query {samples.index(sample) + 1} of {len(samples)}"
 
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.Row():
137
  doc_text = gr.Textbox(label=f"Document {i+1}", interactive=False)
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():
148
+ nonlocal current_order
149
+
150
+ # Only move if index is valid
151
+ if idx < len(current_order) and idx > 0:
152
+ # Swap with the document above
153
+ current_order[idx], current_order[idx-1] = current_order[idx-1], current_order[idx]
154
+
155
+ # Update all document displays with new order
156
+ sample = next((s for s in samples if s["id"] == current_sample_id.value), None)
157
+ if sample:
158
+ docs = sample["candidates"]
159
+ updates = []
160
+ for j, doc_idx in enumerate(current_order):
161
+ if j < len(document_containers) and doc_idx < len(docs):
162
+ updates.append(gr.update(value=f"Document {doc_idx+1} (Rank: {j+1}): {docs[doc_idx]}"))
163
+ else:
164
+ updates.append(gr.update(value=""))
165
+ return updates
166
+ return [gr.update() for _ in document_containers]
167
+ return up_handler
168
+
169
+ def make_down_handler(idx):
170
+ def down_handler():
171
+ nonlocal current_order
172
+
173
+ # Only move if index is valid
174
+ if idx < len(current_order) - 1:
175
+ # Swap with the document below
176
+ current_order[idx], current_order[idx+1] = current_order[idx+1], current_order[idx]
177
+
178
+ # Update all document displays with new order
179
+ sample = next((s for s in samples if s["id"] == current_sample_id.value), None)
180
+ if sample:
181
+ docs = sample["candidates"]
182
+ updates = []
183
+ for j, doc_idx in enumerate(current_order):
184
+ if j < len(document_containers) and doc_idx < len(docs):
185
+ updates.append(gr.update(value=f"Document {doc_idx+1} (Rank: {j+1}): {docs[doc_idx]}"))
186
+ else:
187
+ updates.append(gr.update(value=""))
188
+ return updates
189
+ return [gr.update() for _ in document_containers]
190
+ return down_handler
191
+
192
+ # Connect buttons with properly created handlers
193
  up_btn.click(
194
+ make_up_handler(i),
195
  inputs=[],
196
+ outputs=document_containers
 
 
 
 
 
197
  )
198
 
199
  down_btn.click(
200
+ make_down_handler(i),
 
 
 
 
 
201
  inputs=[],
202
+ outputs=document_containers
203
  )
204
 
205
  with gr.Row():