-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_extract_hf.py
More file actions
203 lines (172 loc) · 7.21 KB
/
Copy pathbatch_extract_hf.py
File metadata and controls
203 lines (172 loc) · 7.21 KB
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
#!/usr/bin/env python3
"""Batch extraction driver for pediatric genetic reports using the Hugging Face backend."""
from __future__ import annotations
import argparse
import csv
import json
import sys
import traceback
from pathlib import Path
from typing import Any
from main import HFBackend, extract, load_report, resolve_model
from schema import GeneticReportExtraction, get_extraction_model
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Batch extract pediatric genetic reports with the HF backend"
)
parser.add_argument("--input-path", type=Path, required=True)
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--fail-dir", type=Path, required=True)
parser.add_argument("--status-tsv", type=Path, required=True)
parser.add_argument("--aggregate-csv", type=Path, default=None)
parser.add_argument("--pattern", default="*.txt")
parser.add_argument("--limit", type=int, default=0)
parser.add_argument("--model", default="oss-120b")
parser.add_argument("--num-gpus", type=int, default=1)
parser.add_argument(
"--dtype",
choices=["auto", "bfloat16", "float16", "float32"],
default="auto",
)
parser.add_argument(
"--input-format",
choices=["auto", "text", "pdf"],
default="text",
)
parser.add_argument("--temperature", type=float, default=0.0)
parser.add_argument("--top-p", type=float, default=1.0)
parser.add_argument("--max-new-tokens", type=int, default=12000)
parser.add_argument("--max-retries", type=int, default=3)
parser.add_argument("--skip-existing", action="store_true")
return parser.parse_args()
def flatten(obj: Any, prefix: str = "") -> dict[str, Any]:
out: dict[str, Any] = {}
if isinstance(obj, dict):
for key, value in obj.items():
new_key = f"{prefix}.{key}" if prefix else key
if isinstance(value, dict):
out.update(flatten(value, new_key))
elif isinstance(value, list):
out[new_key] = json.dumps(value, ensure_ascii=False)
else:
out[new_key] = value
else:
out[prefix or "value"] = obj
return out
def discover_files(input_path: Path, pattern: str, limit: int) -> list[Path]:
if input_path.is_file():
files = [input_path]
else:
files = sorted(path for path in input_path.glob(pattern) if path.is_file())
if limit > 0:
files = files[:limit]
return files
def infer_input_format(requested: str) -> str | None:
if requested == "auto":
return None
return requested
def ensure_report_file_name(
result: GeneticReportExtraction,
source_path: Path,
) -> GeneticReportExtraction:
if result.test_info.file:
return result
test_info = result.test_info.model_copy(update={"file": source_path.name})
return result.model_copy(update={"test_info": test_info})
def write_aggregate_csv(json_paths: list[Path], csv_path: Path) -> None:
rows: list[dict[str, Any]] = []
fieldnames = ["source_file", "json_path"]
for json_path in json_paths:
payload = json.loads(json_path.read_text(encoding="utf-8"))
row = {
"source_file": payload.get("test_info", {}).get("file")
or json_path.stem.replace(".extracted", ""),
"json_path": str(json_path),
}
row.update(flatten(payload))
rows.append(row)
for key in row:
if key not in fieldnames:
fieldnames.append(key)
with csv_path.open("w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
def main() -> None:
args = parse_args()
args.output_dir.mkdir(parents=True, exist_ok=True)
args.fail_dir.mkdir(parents=True, exist_ok=True)
args.status_tsv.parent.mkdir(parents=True, exist_ok=True)
if args.aggregate_csv is not None:
args.aggregate_csv.parent.mkdir(parents=True, exist_ok=True)
files = discover_files(args.input_path, args.pattern, args.limit)
if not files:
raise SystemExit(
f"no files found under {args.input_path} with pattern {args.pattern}"
)
model_id = resolve_model(args.model)
print(f"[info] loading HF model once: {args.model} -> {model_id}", flush=True)
backend = HFBackend(model_id, num_gpus=args.num_gpus, dtype=args.dtype)
model_cls = get_extraction_model()
ok = 0
failed = 0
written_jsons: list[Path] = []
with args.status_tsv.open("w", encoding="utf-8", newline="") as status_handle:
status_handle.write("source_file\tjson_path\tstatus\tmessage\n")
for index, path in enumerate(files, start=1):
out_path = args.output_dir / f"{path.stem}.extracted.json"
err_path = args.fail_dir / f"{path.stem}.error.txt"
if args.skip_existing and out_path.exists():
print(f"[skip] {index}/{len(files)} {path.name}", flush=True)
status_handle.write(f"{path}\t{out_path}\tskip\t\n")
written_jsons.append(out_path)
continue
print(f"[run] {index}/{len(files)} {path.name}", flush=True)
try:
report_text = load_report(path, infer_input_format(args.input_format))
result = extract(
report_text,
backend,
model_cls=model_cls,
temperature=args.temperature,
top_p=args.top_p,
max_new_tokens=args.max_new_tokens,
max_retries=args.max_retries,
)
if not isinstance(result, GeneticReportExtraction):
result = GeneticReportExtraction.model_validate(
result.model_dump(mode="json")
)
result = ensure_report_file_name(result, path)
out_path.write_text(
json.dumps(
result.model_dump(mode="json"),
indent=2,
ensure_ascii=False,
),
encoding="utf-8",
)
if err_path.exists():
err_path.unlink()
status_handle.write(f"{path}\t{out_path}\tok\t\n")
written_jsons.append(out_path)
ok += 1
print(f"[ok] {path.name} -> {out_path.name}", flush=True)
except Exception as exc:
failed += 1
err_path.write_text(
f"{type(exc).__name__}: {exc}\n\n{traceback.format_exc()}",
encoding="utf-8",
)
status_handle.write(f"{path}\t{out_path}\tfail\t{err_path}\n")
print(f"[fail] {path.name}: {exc}", file=sys.stderr, flush=True)
if args.aggregate_csv is not None:
write_aggregate_csv(written_jsons, args.aggregate_csv)
print(f"[csv] wrote {args.aggregate_csv}", flush=True)
print(
f"[done] ok={ok} failed={failed} output_dir={args.output_dir}",
flush=True,
)
if __name__ == "__main__":
main()