samwjya Claude Opus 5.5 commited on
Commit Β·
b137166
1
Parent(s): 0357ad6
Repair CLI: main.py runs the full pipeline
Browse filesIt imported ensemble functions that no longer existed and crashed on start.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main.py
CHANGED
|
@@ -1,57 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import argparse
|
| 2 |
import sys
|
| 3 |
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def parse_args() -> argparse.Namespace:
|
| 10 |
parser = argparse.ArgumentParser(
|
| 11 |
prog="paperrank",
|
| 12 |
-
description="
|
| 13 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
| 14 |
)
|
| 15 |
-
parser.add_argument("
|
| 16 |
-
parser.add_argument(
|
| 17 |
-
|
| 18 |
-
help="Number of candidate papers to fetch from OpenAlex",
|
| 19 |
-
)
|
| 20 |
-
parser.add_argument(
|
| 21 |
-
"--k", type=int, default=10,
|
| 22 |
-
help="Top-k cutoff for display and evaluation metrics (NDCG@k, MRR)",
|
| 23 |
-
)
|
| 24 |
-
parser.add_argument(
|
| 25 |
-
"--relevant-ids", nargs="*", default=None, metavar="PAPER_ID",
|
| 26 |
-
help="OpenAlex paper IDs to treat as relevant (for NDCG/MRR). "
|
| 27 |
-
"If omitted, pseudo-relevance (OpenAlex top-k) is used.",
|
| 28 |
-
)
|
| 29 |
return parser.parse_args()
|
| 30 |
|
| 31 |
|
| 32 |
def main() -> None:
|
|
|
|
| 33 |
args = parse_args()
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
sys.exit(1)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
"""PaperRank command line: the same pipeline as the web app, printed to the terminal.
|
| 2 |
+
|
| 3 |
+
python main.py "effects of microplastics on gut microbiome in zebrafish larvae"
|
| 4 |
+
"""
|
| 5 |
import argparse
|
| 6 |
import sys
|
| 7 |
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
from analyzer import LLMError, assess_coverage, decompose, find_gaps
|
| 13 |
+
from reranker import AVAILABLE_MODELS, DEFAULT_MODEL, load_model, rerank
|
| 14 |
+
from retriever import multi_search
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
STATUS_MARK = {"covered": "β", "partial": "~", "missing": "β"}
|
| 18 |
|
| 19 |
|
| 20 |
def parse_args() -> argparse.Namespace:
|
| 21 |
parser = argparse.ArgumentParser(
|
| 22 |
prog="paperrank",
|
| 23 |
+
description="Find papers for a research question, check aspect coverage, and spot possible gaps",
|
| 24 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
| 25 |
)
|
| 26 |
+
parser.add_argument("question", type=str, help="Research question in plain language")
|
| 27 |
+
parser.add_argument("--model", default=DEFAULT_MODEL, choices=AVAILABLE_MODELS, help="Reranker model")
|
| 28 |
+
parser.add_argument("--top", type=int, default=5, help="Papers to analyze and display")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return parser.parse_args()
|
| 30 |
|
| 31 |
|
| 32 |
def main() -> None:
|
| 33 |
+
sys.stdout.reconfigure(encoding="utf-8") # Windows consoles default to cp1252
|
| 34 |
args = parse_args()
|
| 35 |
|
| 36 |
+
print("[1/4] Breaking the question into aspects ...")
|
| 37 |
+
plan = decompose(args.question)
|
| 38 |
+
print(" Aspects :", ", ".join(a["name"] for a in plan["aspects"]) or "(none: LLM unavailable)")
|
| 39 |
+
print(" Sub-queries:", *plan["sub_queries"], sep="\n - ")
|
| 40 |
+
|
| 41 |
+
print("[2/4] Searching OpenAlex ...")
|
| 42 |
+
papers = multi_search(plan["sub_queries"])
|
| 43 |
+
if not papers:
|
| 44 |
+
print("No papers with abstracts found. Try rephrasing.", file=sys.stderr)
|
| 45 |
sys.exit(1)
|
| 46 |
+
|
| 47 |
+
print(f"[3/4] Reranking {len(papers)} candidates with {args.model} ...")
|
| 48 |
+
top = rerank(args.question, papers, load_model(args.model))[:args.top]
|
| 49 |
+
|
| 50 |
+
print("[4/4] Checking coverage ...")
|
| 51 |
+
try:
|
| 52 |
+
assessments = assess_coverage(args.question, plan["aspects"], top)
|
| 53 |
+
except LLMError as e:
|
| 54 |
+
print(f" Coverage unavailable: {e}")
|
| 55 |
+
assessments = [None] * len(top)
|
| 56 |
+
|
| 57 |
+
for paper, r in zip(top, assessments):
|
| 58 |
+
coverage = f"{r['coverage']}% coverage" if r and r["coverage"] is not None else "not analyzed"
|
| 59 |
+
print(f"\n#{paper['ce_rank']} [{coverage}, match {paper['ce_score']:.2f}] {paper['title']}")
|
| 60 |
+
print(f" {paper.get('year') or 'n.d.'} Β· {paper.get('citationCount') or 0} citations Β· {paper.get('url', '')}")
|
| 61 |
+
for a in (r or {}).get("aspects", []):
|
| 62 |
+
print(f" {STATUS_MARK[a['status']]} {a['name']}" + (f' "{a["evidence"]}"' if a["evidence"] else ""))
|
| 63 |
+
if r and r["note"]:
|
| 64 |
+
print(f" not addressed: {r['note']}")
|
| 65 |
+
|
| 66 |
+
if all(assessments):
|
| 67 |
+
gaps = find_gaps(plan["aspects"], assessments)
|
| 68 |
+
print("\nPossible gaps (leads to verify, not proof)")
|
| 69 |
+
print(" Not fully covered by any top paper:", ", ".join(gaps["uncovered"]) or "none")
|
| 70 |
+
if gaps["full_count"] is not None:
|
| 71 |
+
print(f" All aspects together ({gaps['full_query']!r}): {gaps['full_count']} papers")
|
| 72 |
+
for b in gaps["bottlenecks"]:
|
| 73 |
+
ratio = b["count_without"] / max(gaps["full_count"], 1)
|
| 74 |
+
print(f" without '{b['aspect']}': {b['count_without']} papers ({ratio:.0f}x more)")
|
| 75 |
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|