Tablinum-3: apps, web, scicode, homerepair tables; code, recipes, packs, routes, sources, license, art
9728219 verified Download code/s2_authors.py from tablinum/tablinum-3: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/datasets/tablinum/tablinum-3/resolve/main/code/s2_authors.py
- Command line
-
hf download hf://datasets/tablinum/tablinum-3/code/s2_authors.py
-
curl -L -o s2_authors.py https://huggingface.co/datasets/tablinum/tablinum-3/resolve/main/code/s2_authors.py
3 kB
| """s2_authors.py OUT_DIR (11-02, tablinum-3 Humanities rank 1, research/humanities_round2.md) - author / venue enrichment for papers_hum: | |
| every passage's page_id is a Semantic Scholar CorpusId; POST /graph/v1/paper/batch (500 ids per call, fields title,authors,year,venue, | |
| journal) and store {corpusId, authors, venue, journal} as jsonl shards. Resumable (ids already fetched are skipped). Polite: on 429 waits | |
| with backoff; uses S2_API_KEY from the environment if set (1 req/s), else the shared anonymous pool. Local use only (S2 API terms).""" | |
| import glob, json, os, sys, time, urllib.request | |
| import pyarrow.parquet as pq | |
| OUT = sys.argv[1]; os.makedirs(OUT, exist_ok=True); KEY = os.environ.get("S2_API_KEY") | |
| ids = sorted({int(x) for f in sorted(glob.glob(os.environ.get("ENGRAM_ROOT", ".") + "/tables/papers_hum/passages/*.parquet")) for x in pq.read_table(f, columns=["page_id"]).column("page_id").to_pylist()}) | |
| done = set() | |
| for f in glob.glob(f"{OUT}/*.jsonl"): | |
| for l in open(f): | |
| try: done.add(json.loads(l)["id"]) | |
| except Exception: pass | |
| todo = [i for i in ids if i not in done]; print(f"{len(ids)} ids, {len(done)} done, {len(todo)} to fetch", flush=True) | |
| out = open(f"{OUT}/s2_{int(time.time())}.jsonl", "a"); wait = 3.0; n = 0 | |
| for b in range(0, len(todo), 500): | |
| chunk = todo[b:b + 500] | |
| body = json.dumps({"ids": [f"CorpusId:{i}" for i in chunk]}).encode() | |
| for attempt in range(40): | |
| req = urllib.request.Request("https://api.semanticscholar.org/graph/v1/paper/batch?fields=authors,year,venue,journal,externalIds", | |
| body, {"Content-Type": "application/json", **({"x-api-key": KEY} if KEY else {})}) | |
| try: | |
| res = json.load(urllib.request.urlopen(req, timeout=120)); wait = max(1.5 if KEY else 3.0, wait * 0.5); break # 11-03: fast recovery after a 429 | |
| except urllib.error.HTTPError as e: | |
| ra = e.headers.get("Retry-After") if e.headers else None | |
| wait = max(float(ra), 1.1) if ra and ra.replace('.', '', 1).isdigit() else (min(60, wait * 2) if e.code == 429 else min(120, wait * 1.5)) | |
| print(f"{time.strftime('%H:%M:%S')} HTTP {e.code} batch {b} attempt {attempt} retry-after {ra} -> wait {wait:.1f}s {e.read()[:160]!r}", flush=True) | |
| except Exception: | |
| wait = min(300, wait * 1.5) | |
| time.sleep(wait) | |
| else: | |
| print("giving up on batch", b, flush=True); continue | |
| for i, r in zip(chunk, res): | |
| if not r: out.write(json.dumps({"id": i, "missing": True}) + "\n"); continue | |
| out.write(json.dumps({"id": i, "authors": [a.get("name") for a in (r.get("authors") or [])][:12], "year": r.get("year"), | |
| "venue": r.get("venue"), "journal": (r.get("journal") or {}).get("name")}, ensure_ascii=False) + "\n") | |
| out.flush(); n += 1 | |
| if n % 20 == 0 or n <= 5: print(f"{b + len(chunk)}/{len(todo)} fetched, wait {wait:.1f}s", flush=True) | |
| time.sleep(wait) | |
| print("done", flush=True) | |