Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,41 +11,51 @@ def create_reranking_interface(task_data):
|
|
11 |
|
12 |
def save_ranking(rankings, sample_id):
|
13 |
"""Save the current set of rankings."""
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
51 |
gr.Markdown(f"# {task_data['task_name']} - Human Reranking Evaluation")
|
|
|
11 |
|
12 |
def save_ranking(rankings, sample_id):
|
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 |
+
|
20 |
+
# Convert rankings to integers with better error handling
|
21 |
+
try:
|
22 |
+
processed_rankings = [int(r) for r in rankings]
|
23 |
+
except ValueError:
|
24 |
+
return "⚠️ Invalid ranking value. Please use only numbers.", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
25 |
+
|
26 |
+
# Check for duplicate rankings
|
27 |
+
if len(set(processed_rankings)) != len(processed_rankings):
|
28 |
+
return "⚠️ Each document must have a unique rank. Please review your rankings.", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
29 |
|
30 |
+
# Store this annotation
|
31 |
+
existing_idx = next((i for i, a in enumerate(results["annotations"]) if a["sample_id"] == sample_id), None)
|
32 |
+
if existing_idx is not None:
|
33 |
+
results["annotations"][existing_idx] = {
|
34 |
+
"sample_id": sample_id,
|
35 |
+
"rankings": processed_rankings
|
36 |
+
}
|
37 |
+
else:
|
38 |
+
results["annotations"].append({
|
39 |
+
"sample_id": sample_id,
|
40 |
+
"rankings": processed_rankings
|
41 |
+
})
|
42 |
+
|
43 |
+
completed_samples[sample_id] = True
|
44 |
+
success_msg = f"✅ Rankings for query '{sample_id}' successfully saved!"
|
45 |
+
progress = f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
46 |
+
|
47 |
+
# Auto-save results after each submission with error handling
|
48 |
+
try:
|
49 |
+
output_path = f"{task_data['task_name']}_human_results.json"
|
50 |
+
with open(output_path, "w") as f:
|
51 |
+
json.dump(results, f, indent=2)
|
52 |
+
except Exception as e:
|
53 |
+
return f"⚠️ Rankings saved in memory but couldn't write to file: {str(e)}", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
54 |
+
|
55 |
+
return success_msg, progress
|
56 |
+
except Exception as e:
|
57 |
+
# Catch-all for any other unexpected errors
|
58 |
+
return f"⚠️ An error occurred: {str(e)}", f"Progress: {sum(completed_samples.values())}/{len(samples)}"
|
59 |
|
60 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
61 |
gr.Markdown(f"# {task_data['task_name']} - Human Reranking Evaluation")
|