pearmut 0.2.8__py3-none-any.whl → 0.2.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pearmut/app.py CHANGED
@@ -1,5 +1,7 @@
1
+ import collections
1
2
  import json
2
3
  import os
4
+ import statistics
3
5
  from typing import Any
4
6
 
5
7
  from fastapi import FastAPI, Query
@@ -12,6 +14,7 @@ from .assignment import get_i_item, get_next_item, reset_task, update_progress
12
14
  from .utils import (
13
15
  ROOT,
14
16
  check_validation_threshold,
17
+ get_db_log,
15
18
  load_progress_data,
16
19
  save_db_payload,
17
20
  save_progress_data,
@@ -191,6 +194,58 @@ async def _dashboard_data(request: DashboardDataRequest):
191
194
  )
192
195
 
193
196
 
197
+ class DashboardResultsRequest(BaseModel):
198
+ campaign_id: str
199
+ token: str
200
+
201
+
202
+ @app.post("/dashboard-results")
203
+ async def _dashboard_results(request: DashboardResultsRequest):
204
+ campaign_id = request.campaign_id
205
+ token = request.token
206
+
207
+ if campaign_id not in progress_data:
208
+ return JSONResponse(content="Unknown campaign ID", status_code=400)
209
+
210
+ # Check if token is valid
211
+ if token != tasks_data[campaign_id]["token"]:
212
+ return JSONResponse(content="Invalid token", status_code=400)
213
+
214
+ # Compute model scores from annotations
215
+ model_scores = collections.defaultdict(dict)
216
+
217
+ # Iterate through all tasks to find items with 'model' field
218
+ log = get_db_log(campaign_id)
219
+ for entry in log:
220
+ if "item" not in entry or "annotations" not in entry:
221
+ continue
222
+ for item, annotation in zip(entry["item"], entry["annotations"]):
223
+ if "model" in item:
224
+ # pointwise
225
+ if "score" in annotation:
226
+ # make sure to only keep the latest score for each item
227
+ # json.dumps(item) creates a unique item key
228
+ model_scores[item["model"]][json.dumps(item)] = annotation["score"]
229
+ elif "models" in item:
230
+ # listwise
231
+ for model, annotation_cand in zip(item["models"], annotation):
232
+ if "score" in annotation_cand:
233
+ model_scores[model][json.dumps(item)] = (
234
+ annotation_cand["score"]
235
+ )
236
+
237
+ results = [
238
+ {
239
+ "model": model,
240
+ "score": statistics.mean(scores.values()),
241
+ "count": len(scores),
242
+ }
243
+ for model, scores in model_scores.items()
244
+ ]
245
+ results.sort(key=lambda x: x["score"], reverse=True)
246
+ return JSONResponse(content=results, status_code=200)
247
+
248
+
194
249
  class ResetTaskRequest(BaseModel):
195
250
  campaign_id: str
196
251
  user_id: str
@@ -236,7 +291,11 @@ async def _download_annotations(
236
291
  with open(output_path, "r") as f:
237
292
  output[campaign_id] = [json.loads(x) for x in f.readlines()]
238
293
 
239
- return JSONResponse(content=output, status_code=200)
294
+ return JSONResponse(
295
+ content=output,
296
+ status_code=200,
297
+ headers={"Content-Disposition": 'inline; filename="annotations.json"'}
298
+ )
240
299
 
241
300
 
242
301
  @app.get("/download-progress")
@@ -260,7 +319,11 @@ async def _download_progress(
260
319
 
261
320
  output[cid] = progress_data[cid]
262
321
 
263
- return JSONResponse(content=output, status_code=200)
322
+ return JSONResponse(
323
+ content=output,
324
+ status_code=200,
325
+ headers={"Content-Disposition": 'inline; filename="progress.json"'}
326
+ )
264
327
 
265
328
 
266
329
  static_dir = f"{os.path.dirname(os.path.abspath(__file__))}/static/"
pearmut/cli.py CHANGED
@@ -41,7 +41,9 @@ def _run(args_unknown):
41
41
  args.server + "/dashboard.html?" + "&".join([
42
42
  f"campaign_id={urllib.parse.quote_plus(campaign_id)}&token={campaign_data["token"]}"
43
43
  for campaign_id, campaign_data in tasks_data.items()
44
- ])
44
+ ]),
45
+ # this is important to flush
46
+ flush=True,
45
47
  )
46
48
 
47
49
  uvicorn.run(