-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreview.py
More file actions
283 lines (239 loc) · 8.16 KB
/
review.py
File metadata and controls
283 lines (239 loc) · 8.16 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
review.py — CLI entry point and orchestrator for ReviewPanel
Usage:
python review.py # auto-detect, top-medical
python review.py JAMA # auto-detect + JAMA profile
python review.py JAMA ./manuscript.tex # explicit file
python review.py --model phi4 CJEM ./paper.md # custom model
"""
import sys
import argparse
from datetime import date
from pathlib import Path
from manuscript import discover_manuscript
from agents import run_all_agents
# ---------------------------------------------------------------------------
# Known journal names and their profile file stems
# ---------------------------------------------------------------------------
_JOURNAL_FILES = {
"JAMA": "JAMA.json",
"CJEM": "CJEM.json",
"AnnalsEM": "Annals_of_EM.json",
"Resuscitation": "Resuscitation.json",
}
_KNOWN_JOURNALS = set(_JOURNAL_FILES.keys()) | {
"NEJM", "Lancet", "BMJ", "AJEM", "JAMIA", "BMCMedEd", "SimHealthcare",
}
KB_BASE = Path(__file__).parent / "knowledge_base"
# ---------------------------------------------------------------------------
# Argument parsing
# ---------------------------------------------------------------------------
def parse_args(argv: list[str] | None = None):
"""
Parse CLI arguments.
Positional tokens (not starting with --):
- First token that matches a known journal name → journal
- Remaining token(s) treated as the file path
Returns (namespace) with attributes: journal, file_path, model, verbose
"""
parser = argparse.ArgumentParser(
prog="review.py",
description="Run 6-agent pre-submission medical review via Ollama",
)
parser.add_argument(
"--model",
default="qwen2.5:7b",
help="Ollama model to use (default: qwen2.5:7b)",
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Print extra debug info",
)
# We use nargs='*' for positional to allow 0, 1, or 2 positional args
parser.add_argument(
"positional",
nargs="*",
metavar="[JOURNAL] [FILE]",
help="Optional journal name and/or manuscript file path",
)
args = parser.parse_args(argv)
journal = "top-medical"
file_path: str | None = None
remaining = list(args.positional)
# First positional token: check if it's a journal keyword
if remaining and remaining[0] in _KNOWN_JOURNALS:
journal = remaining.pop(0)
# Any remaining token is the file path
if remaining:
file_path = remaining[0]
return argparse.Namespace(
journal=journal,
file_path=file_path,
model=args.model,
verbose=args.verbose,
)
# ---------------------------------------------------------------------------
# Journal profile loader
# ---------------------------------------------------------------------------
def load_journal_profile(journal: str, kb_base: Path = KB_BASE) -> str:
"""
Load the JSON profile for the target journal.
Returns empty string for 'top-medical' or unrecognised journals.
"""
if journal == "top-medical" or journal not in _JOURNAL_FILES:
return ""
profile_path = kb_base / "journal_profiles" / _JOURNAL_FILES[journal]
try:
return profile_path.read_text(encoding="utf-8", errors="replace")
except OSError as e:
print(
f"[WARNING] Could not load journal profile {profile_path}: {e}",
file=sys.stderr,
)
return ""
# ---------------------------------------------------------------------------
# Report builder
# ---------------------------------------------------------------------------
_SECTION_HEADERS = {
1: "1. Medical Style, Grammar & Reporting Guidelines",
2: "2. Internal Consistency & PICO Verification",
3: "3. Clinical Claims, Causality & Confounding",
4: "4. Biostatistics, Methodology & Notation",
5: "5. Tables, Figures & Clinical Documentation",
6: "6. Clinical Impact & Adversarial Referee",
}
def build_report(
agent_outputs: list[str],
manuscript_data: dict,
journal: str,
model: str,
) -> Path:
"""
Assemble all 6 agent outputs into a single Markdown report.
Saves to CWD as PRE_SUBMISSION_MEDICAL_REVIEW_YYYY-MM-DD.md.
Returns the Path of the saved file.
"""
today = date.today().isoformat()
filename = f"PRE_SUBMISSION_MEDICAL_REVIEW_{today}.md"
output_path = Path.cwd() / filename
lines: list[str] = []
# ---- Header ----
lines += [
"# Medical Pre-Submission Referee Report",
"",
f"**Date:** {today}",
f"**Target Journal:** {journal}",
f"**Manuscript:** {manuscript_data['title']}",
f"**Source:** {manuscript_data['source_path']}",
f"**Model:** {model}",
"",
"---",
"",
]
# ---- Overall assessment placeholder ----
lines += [
"## Overall Assessment",
"",
"> *See Priority Action Items section at the end for the consolidated "
"triage.*",
"",
"---",
"",
]
# ---- Agent sections ----
for i, output in enumerate(agent_outputs, start=1):
header = _SECTION_HEADERS[i]
lines += [
f"## {header}",
"",
output.strip(),
"",
"---",
"",
]
# ---- Priority Action Items ----
lines += [
"## Priority Action Items",
"",
"*(Synthesised from all 6 agent reviews above)*",
"",
"### Critical",
"",
"<!-- Reviewer: list critical items here -->",
"",
"### Major",
"",
"<!-- Reviewer: list major items here -->",
"",
"### Minor",
"",
"<!-- Reviewer: list minor items here -->",
"",
]
report_text = "\n".join(lines)
output_path.write_text(report_text, encoding="utf-8")
return output_path
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
def main():
args = parse_args()
print("=" * 60)
print("ReviewPanel")
print("=" * 60)
print(f"Journal : {args.journal}")
print(f"Model : {args.model}")
# Phase 1 — Discover manuscript
print("\n[Phase 1] Discovering manuscript ...")
try:
manuscript_data = discover_manuscript(args.file_path)
except FileNotFoundError as e:
print(f"[ERROR] {e}", file=sys.stderr)
sys.exit(1)
print(f" Title : {manuscript_data['title']}")
print(f" Source : {manuscript_data['source_path']}")
print(f" Characters : {len(manuscript_data['full_text']):,}")
journal_profile_text = load_journal_profile(args.journal)
if journal_profile_text:
print(f" Journal profile loaded for {args.journal}.")
else:
print(f" No journal profile (top-medical standards).")
# Phase 2 — Run agents
print("\n[Phase 2] Running 6 review agents sequentially ...")
agent_outputs = run_all_agents(
manuscript_data=manuscript_data,
journal=args.journal,
journal_profile_text=journal_profile_text,
model=args.model,
verbose=args.verbose,
)
# Phase 3 — Build and save report
print("\n[Phase 3] Building report ...")
report_path = build_report(
agent_outputs=agent_outputs,
manuscript_data=manuscript_data,
journal=args.journal,
model=args.model,
)
print(f"\n{'=' * 60}")
print(f"Report saved: {report_path}")
print(f"{'=' * 60}")
# Summary: show Agent 6 recommendation snippet
if agent_outputs:
last = agent_outputs[-1]
# Find Part 5 recommendation
import re
m = re.search(
r"(?:Part\s*5|Recommendation)[^\n]*\n+(.*?)(?:\n\n|\*\*Part\s*6|$)",
last,
re.IGNORECASE | re.DOTALL,
)
if m:
snippet = m.group(1).strip()[:300]
print(f"\nAdversarial Referee Recommendation:\n{snippet}")
print(
f"\nReview complete. Open {report_path.name} to read the full report."
)
if __name__ == "__main__":
main()