Spaces:
Sleeping

File size: 6,353 Bytes
c500819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import json
import random
from dataclasses import dataclass
from typing import Any, Dict, List, Optional

import yaml
from datasets import load_dataset


@dataclass
class QItem:
    qid: str
    source: str
    question: str
    choices: Optional[List[str]]
    answer_key: Optional[str]
    topic: Optional[str]
    risk_tag: List[str]
    split: str


def _text(x: Any) -> str:
    return (str(x) if x is not None else "").strip()


def _lower(s: str) -> str:
    return s.lower()


def _risk_tags(question: str, rt_cfg: dict) -> List[str]:
    q = _lower(question)
    tags = []
    if any(k in q for k in rt_cfg.get("emergency_keywords", [])):
        tags.append("emergency_red_flag")
    if any(k in q for k in rt_cfg.get("medication_keywords", [])):
        tags.append("high_risk_medication")
    if any(k in q for k in rt_cfg.get("vulnerable_keywords", [])):
        tags.append("vulnerable_population")
    return tags


def _load_one(ds_cfg: dict):
    hf_id = ds_cfg["hf_id"]
    subset = ds_cfg.get("subset")
    split = ds_cfg.get("split", "train")
    streaming = bool(ds_cfg.get("streaming", False))

    kwargs = {"split": split, "streaming": streaming}
    if subset:
        return load_dataset(hf_id, subset, **kwargs)
    return load_dataset(hf_id, **kwargs)


def _sample_records(dset, n: int, seed: int, streaming: bool):
    if streaming:
        # streaming: shuffle with buffer and take n
        dset = dset.shuffle(seed=seed, buffer_size=min(10_000, n * 50))
        return list(dset.take(n))
    # non-streaming
    idx = list(range(len(dset)))
    rnd = random.Random(seed)
    rnd.shuffle(idx)
    idx = idx[:n]
    return [dset[i] for i in idx]


def _normalize_medmcqa(rec: Dict[str, Any], i: int) -> Dict[str, Any]:
    # common fields in MedMCQA: question, opa/opb/opc/opd, cop, subject_name
    q = _text(rec.get("question"))
    choices = []
    for k in ["opa", "opb", "opc", "opd"]:
        if k in rec:
            choices.append(_text(rec.get(k)))
    choices = choices if choices else None

    answer_key = rec.get("cop")
    answer_key = _text(answer_key) if answer_key is not None else None

    topic = rec.get("subject_name") or rec.get("subject") or rec.get("topic_name")
    topic = _text(topic) if topic is not None else None

    return {
        "question": q,
        "choices": choices,
        "answer_key": answer_key,
        "topic": topic,
        "qid_suffix": _text(rec.get("id")) or f"{i:06d}",
    }


def _normalize_pubmedqa(rec: Dict[str, Any], i: int) -> Dict[str, Any]:
    # common fields in PubMedQA: question, final_decision, context/long_answer (varies by subset)
    q = _text(rec.get("question"))
    answer_key = rec.get("final_decision") or rec.get("label") or rec.get("answer")
    answer_key = _text(answer_key) if answer_key is not None else None

    # PubMedQA is often yes/no/maybe; no fixed multiple-choice options
    choices = None
    topic = rec.get("reasoning_required") or rec.get("category") or rec.get("source")
    topic = _text(topic) if topic is not None else None

    return {
        "question": q,
        "choices": choices,
        "answer_key": answer_key,
        "topic": topic,
        "qid_suffix": _text(rec.get("pubid")) or _text(rec.get("id")) or f"{i:06d}",
    }


def _assign_splits(items: List[Dict[str, Any]], seed: int, split_cfg: dict) -> List[str]:
    # deterministic shuffle, then allocate by proportions
    rnd = random.Random(seed)
    idx = list(range(len(items)))
    rnd.shuffle(idx)

    n = len(items)
    n_train = int(n * float(split_cfg["train"]))
    n_val = int(n * float(split_cfg["val"]))
    # remainder to test
    split_labels = [""] * n
    for rank, j in enumerate(idx):
        if rank < n_train:
            split_labels[j] = "train"
        elif rank < n_train + n_val:
            split_labels[j] = "val"
        else:
            split_labels[j] = "test"
    return split_labels


def main():
    with open("configs/data.yaml", "r") as f:
        cfg = yaml.safe_load(f)

    seed = int(cfg["seed"])
    rt_cfg = cfg.get("risk_tagging", {})
    all_items: List[Dict[str, Any]] = []

    for ds in cfg["datasets"]:
        name = ds["name"]
        dset = _load_one(ds)
        records = _sample_records(dset, int(ds["n_sample"]), seed=seed + hash(name) % 10_000, streaming=bool(ds.get("streaming", False)))

        for i, rec in enumerate(records):
            if name == "medmcqa":
                norm = _normalize_medmcqa(rec, i)
            elif name == "pubmedqa":
                norm = _normalize_pubmedqa(rec, i)
            else:
                raise ValueError(f"Unknown dataset name: {name}")

            qid = f"{name}_{norm['qid_suffix']}"
            question = norm["question"]
            if not question:
                continue

            tags = _risk_tags(question, rt_cfg)
            all_items.append(
                {
                    "qid": qid,
                    "source": name,
                    "question": question,
                    "choices": norm["choices"],
                    "answer_key": norm["answer_key"],
                    "topic": norm["topic"],
                    "risk_tag": tags,
                }
            )

    # assign splits after combining
    splits = _assign_splits(all_items, seed=seed, split_cfg=cfg["splits"])
    for item, sp in zip(all_items, splits):
        item["split"] = sp
        item["is_high_risk"] = bool(item["risk_tag"])

    out_q = cfg["out_questions"]
    out_m = cfg["out_manifest"]

    # write JSONL
    with open(out_q, "w", encoding="utf-8") as f:
        for item in all_items:
            f.write(json.dumps(item, ensure_ascii=False) + "\n")

    # manifest summary
    manifest = {
        "seed": seed,
        "n_total": len(all_items),
        "by_source": {},
        "by_split": {},
        "high_risk_count": sum(1 for x in all_items if x["is_high_risk"]),
    }
    for x in all_items:
        manifest["by_source"][x["source"]] = manifest["by_source"].get(x["source"], 0) + 1
        manifest["by_split"][x["split"]] = manifest["by_split"].get(x["split"], 0) + 1

    with open(out_m, "w", encoding="utf-8") as f:
        json.dump(manifest, f, indent=2, ensure_ascii=False)

    print("Wrote:", out_q)
    print("Wrote:", out_m)
    print("Manifest:", manifest)


if __name__ == "__main__":
    main()