Spaces:
Sleeping
Sleeping
Load tasks from the published dataset (v1.0)
Browse files
app.py
CHANGED
|
@@ -49,14 +49,106 @@ GA_HEAD = """<script async src="https://www.googletagmanager.com/gtag/js?id=G-W0
|
|
| 49 |
# ]
|
| 50 |
# }, ...
|
| 51 |
# }
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Friendly dropdown: (label, value) so the menu reads as titles, not opaque IDs.
|
|
|
|
| 57 |
TASK_CHOICES = [
|
| 58 |
-
(
|
| 59 |
-
for tid, t in
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
]
|
| 61 |
|
| 62 |
|
|
|
|
| 49 |
# ]
|
| 50 |
# }, ...
|
| 51 |
# }
|
| 52 |
+
# The task bank is the published dataset:
|
| 53 |
+
# https://huggingface.co/datasets/CentificAIResearch/Healthcare.pdf
|
| 54 |
+
# It is downloaded once and cached. The bundled tasks.json is kept only as a
|
| 55 |
+
# fallback so a Hub outage degrades the Space to the local demo tasks instead of
|
| 56 |
+
# taking it down. Set HEALTHCARE_PDF_LOCAL=1 to force the local file.
|
| 57 |
+
DATASET_REPO = os.environ.get("HEALTHCARE_PDF_DATASET", "CentificAIResearch/Healthcare.pdf")
|
| 58 |
+
DATASET_REVISION = os.environ.get("HEALTHCARE_PDF_REVISION", "v1.0")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _tasks_from_hub():
|
| 62 |
+
"""Map the published dataset onto the task-bank shape documented above.
|
| 63 |
+
|
| 64 |
+
Anchors live in their own column keyed by id; each sub-question references
|
| 65 |
+
them by `anchor_ids`, so they are resolved here into the display strings the
|
| 66 |
+
UI expects ("a1: <quote>").
|
| 67 |
+
"""
|
| 68 |
+
import pandas as pd
|
| 69 |
+
from huggingface_hub import snapshot_download
|
| 70 |
+
|
| 71 |
+
root = snapshot_download(DATASET_REPO, repo_type="dataset", revision=DATASET_REVISION)
|
| 72 |
+
cols = ["task_id", "persona", "occupation", "aspect", "doc_id", "document_name",
|
| 73 |
+
"document_file", "case_setup", "task_prompt", "sub_questions", "anchors",
|
| 74 |
+
"difficulty_band", "n_models_solved"]
|
| 75 |
+
# `document` (the embedded PDF bytes) is deliberately not read -- the same
|
| 76 |
+
# files are on disk under pdfs/ and fitz needs a path, not bytes.
|
| 77 |
+
df = pd.read_parquet(os.path.join(root, "data", "test.parquet"), columns=cols)
|
| 78 |
+
|
| 79 |
+
tasks = {}
|
| 80 |
+
for r in df.itertuples():
|
| 81 |
+
by_id = {a["id"]: a for a in r.anchors}
|
| 82 |
+
tasks[r.task_id] = {
|
| 83 |
+
"occupation": r.occupation,
|
| 84 |
+
"persona": r.persona,
|
| 85 |
+
"aspect": r.aspect,
|
| 86 |
+
"doc_pid": r.doc_id,
|
| 87 |
+
"doc_title": r.document_name,
|
| 88 |
+
"pdf_path": os.path.join(root, "pdfs", r.document_file),
|
| 89 |
+
"prompt": r.task_prompt or r.case_setup,
|
| 90 |
+
"case_setup": r.case_setup,
|
| 91 |
+
"difficulty_band": r.difficulty_band,
|
| 92 |
+
"n_models_solved": int(r.n_models_solved),
|
| 93 |
+
"sub_questions": [
|
| 94 |
+
{"q_no": int(s["q_no"]),
|
| 95 |
+
"question": s["question"],
|
| 96 |
+
"reference_answer": s["reference_answer"],
|
| 97 |
+
"task_types": list(s["task_types"]),
|
| 98 |
+
"anchors": [f"{aid}: {by_id[aid]['locator_quote']}"
|
| 99 |
+
for aid in s["anchor_ids"] if aid in by_id]}
|
| 100 |
+
for s in r.sub_questions
|
| 101 |
+
],
|
| 102 |
+
}
|
| 103 |
+
return tasks
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def _load_tasks():
|
| 107 |
+
if os.environ.get("HEALTHCARE_PDF_LOCAL") == "1":
|
| 108 |
+
with open("tasks.json") as f:
|
| 109 |
+
return json.load(f), "local (forced)"
|
| 110 |
+
try:
|
| 111 |
+
return _tasks_from_hub(), f"{DATASET_REPO}@{DATASET_REVISION}"
|
| 112 |
+
except Exception as exc: # noqa: BLE001 -- never take the Space down over this
|
| 113 |
+
print(f"[tasks] Hub load failed ({type(exc).__name__}: {exc}); "
|
| 114 |
+
f"falling back to bundled tasks.json")
|
| 115 |
+
with open("tasks.json") as f:
|
| 116 |
+
return json.load(f), "local (fallback)"
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
TASKS, TASK_SOURCE = _load_tasks()
|
| 120 |
+
print(f"[tasks] loaded {len(TASKS)} tasks from {TASK_SOURCE}")
|
| 121 |
+
|
| 122 |
+
_OCC_SHORT = {
|
| 123 |
+
"emergency_medicine_physicians": "Emergency Medicine",
|
| 124 |
+
"family_medicine_physicians": "Family Medicine",
|
| 125 |
+
"general_internal_medicine_physicians": "General Internal Med",
|
| 126 |
+
"nurse_practitioners": "Nurse Practitioners",
|
| 127 |
+
"physician_assistants": "Physician Assistants",
|
| 128 |
+
"pharmacists": "Pharmacists",
|
| 129 |
+
"pharmacy_technicians": "Pharmacy Technicians",
|
| 130 |
+
}
|
| 131 |
+
_BAND_ORDER = {"unsolved": 0, "hard": 1, "mid": 2, "easy": 3, "ceiling": 4}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _task_label(tid, t):
|
| 135 |
+
"""Dropdown label: difficulty, occupation, then the document it is grounded in."""
|
| 136 |
+
occ = _OCC_SHORT.get(t.get("occupation", ""), t.get("occupation", ""))
|
| 137 |
+
band = t.get("difficulty_band")
|
| 138 |
+
head = f"[{band}] " if band else ""
|
| 139 |
+
title = (t.get("doc_title") or "")[:60]
|
| 140 |
+
return f"{head}{occ} 路 {title}" if occ else f"{tid} 路 {title}"
|
| 141 |
+
|
| 142 |
|
| 143 |
# Friendly dropdown: (label, value) so the menu reads as titles, not opaque IDs.
|
| 144 |
+
# Hardest first, so the interesting cases are at the top of the list.
|
| 145 |
TASK_CHOICES = [
|
| 146 |
+
(_task_label(tid, t), tid)
|
| 147 |
+
for tid, t in sorted(
|
| 148 |
+
TASKS.items(),
|
| 149 |
+
key=lambda kv: (_BAND_ORDER.get(kv[1].get("difficulty_band"), 9),
|
| 150 |
+
kv[1].get("occupation", ""), kv[0]),
|
| 151 |
+
)
|
| 152 |
]
|
| 153 |
|
| 154 |
|