papers / build_data.py
nielsr's picture
nielsr HF Staff
Guard build and publish scripts against wiping the data
68095de verified
Raw History Blame Contribute Delete
4.77 kB
"""Build the Space's data in dataset/site/ from ../claims/*.json.
Usage (from the project root or this folder):
python3 space/build_data.py
python3 space/publish_dataset.py # then upload it to the dataset the Space reads from
Only papers with verdict "include" or "borderline" are published, minus
those listed in claims/site_exclude.json.
Compute estimates are kept internal (not written to papers.json).
Papers listed in pwc_indexed.json (refresh with check_pwc.py) get "pwc": true,
so the page links to Papers with Code for them and to arXiv otherwise.
"""
import csv
import glob
import json
import os
import shutil
import struct
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
OUT = os.path.join(HERE, "dataset") # uploaded by publish_dataset.py
SITE = os.path.join(OUT, "site")
CLAIMS = os.path.join(ROOT, "claims")
SHOW = {"include", "borderline"}
def png_size(path):
with open(path, "rb") as f:
head = f.read(24)
return struct.unpack(">II", head[16:24])
def main():
if not glob.glob(os.path.join(CLAIMS, "*.json")):
sys.exit(f"No claim files in {CLAIMS}; nothing written. Run this where the claims folder exists.")
manifest = {}
path = os.path.join(ROOT, "manifest.csv")
if os.path.exists(path):
manifest = {r["arxiv_id"]: r for r in csv.DictReader(open(path, newline=""))}
exclude_path = os.path.join(CLAIMS, "site_exclude.json")
exclude = json.load(open(exclude_path)) if os.path.exists(exclude_path) else {}
pwc_path = os.path.join(OUT, "pwc_indexed.json")
pwc = set(json.load(open(pwc_path))) if os.path.exists(pwc_path) else set()
os.makedirs(os.path.join(SITE, "img"), exist_ok=True)
papers, skipped = [], []
for f in sorted(glob.glob(os.path.join(CLAIMS, "*.json"))):
try:
d = json.load(open(f))
except Exception:
skipped.append((os.path.basename(f), "unparseable"))
continue
if not isinstance(d, dict) or d.get("verdict") not in SHOW or d.get("arxiv_id") in exclude:
continue
try:
papers.append(entry(d, manifest, pwc))
except Exception as e:
skipped.append((os.path.basename(f), repr(e)[:120]))
if not papers:
sys.exit(f"No publishable papers found in {CLAIMS}; nothing written.")
compact = dict(ensure_ascii=False, separators=(",", ":"))
# 1) tiny index for the first paint: just the IDs, enough to shuffle, count and show the first paper
with open(os.path.join(SITE, "index.json"), "w") as out:
json.dump([p["id"] for p in papers], out, **compact)
# 2) one small file per paper, fetched when that paper is shown
pdir = os.path.join(SITE, "p")
shutil.rmtree(pdir, ignore_errors=True)
os.makedirs(pdir)
for p in papers:
with open(os.path.join(pdir, p["id"] + ".json"), "w") as out:
json.dump(p, out, **compact)
# 3) search corpus, fetched only when someone types in the search box
with open(os.path.join(SITE, "search.json"), "w") as out:
json.dump([[p["id"], " ".join([p["title"], p["authors"], p["claim"], p["background"]]).lower()] for p in papers], out, **compact)
old = os.path.join(HERE, "papers.json")
if os.path.exists(old):
os.remove(old)
print(f"wrote {len(papers)} papers (index.json, p/<id>.json, search.json); skipped {len(skipped)} invalid")
for name, why in skipped:
print(f" skipped {name}: {why}")
def entry(d, manifest, pwc):
c = d["main_claim"]
img_src = os.path.join(CLAIMS, c["image"])
img_name = f"img/{d['arxiv_id']}.png"
img_w, img_h = png_size(img_src)
shutil.copyfile(img_src, os.path.join(SITE, img_name))
m = manifest.get(d["arxiv_id"], {})
return {
"id": d["arxiv_id"],
"title": " ".join(d["title"].split()),
"authors": m.get("authors", ""),
"openreview_id": d.get("openreview_id", ""),
"presentation": d.get("presentation") or m.get("presentation", "poster"),
"track": d.get("track") or m.get("track", ""),
"verdict": d["verdict"],
"background": d["background"],
"method": d["method"],
"claim": c["statement"],
"source": c["source"],
"metric": c.get("metric", ""),
"comparison": c.get("comparison", ""),
"falsification": c.get("falsification_criterion", ""),
"caption": c["caption"],
"image": img_name,
"kind": c.get("kind") or "table",
"image_w": img_w,
"image_h": img_h,
"pwc": d["arxiv_id"] in pwc,
}
if __name__ == "__main__":
main()