"""The part of the dataset meant for people: three tidy tables, the page's own data, a card. tables/models.parquet one row per repo (official and community), as of the day tables/releases.parquet one row per official release tables/downloads.parquet every repo's downloads over time (hub-stats weekly, then daily) site/*.json exactly what the public page reads, copied from the same run README.md the dataset card (dataset/README.md in the code) Rebuilt by every daily run from the same whitelisted data the page shows (no names of adult repos, nothing private), so the files can be passed around. Column names are the same across tables: repo_id, release, modality, day, downloads_30d, downloads_all_time, likes. """ from pathlib import Path import pandas as pd from . import hubstats, org, store from .hub import api CARD = Path(__file__).resolve().parent.parent / "dataset" / "README.md" def tables(site, repos): """(models, releases, downloads) DataFrames.""" rel = {r["key"]: r for r in site["releases"]} modality = {c["key"]: c["name"] for c in site["categories"]} day = site["today"] rows = [] for r in site["releases"]: for m in r["models"]: rows.append({"repo_id": m["id"], "author": m["id"].split("/")[0], "release": r["key"], "modality": modality[r["cat"]], "official": True, "relation": "official", "parent": None, "created": m["created"], "downloads_30d": m["dl30"], "downloads_all_time": m["dlall"], "downloads_1d": None, "likes": m["likes"], "day": day}) for key, rs in repos.items(): r = rel[key] for x in rs: rows.append({"repo_id": x["id"], "author": x["id"].split("/")[0], "release": key, "modality": modality[r["cat"]], "official": False, "relation": x["rel"], "parent": x["up"], "created": x["created"] or None, "downloads_30d": x["dl30"], "downloads_all_time": x["dlall"], "downloads_1d": x.get("d1"), "likes": x["likes"], "day": day}) models = pd.DataFrame(rows).drop_duplicates("repo_id") for c in ["downloads_30d", "downloads_all_time", "downloads_1d", "likes"]: models[c] = models[c].astype("Int64") releases = pd.DataFrame([{ "release": r["key"], "name": r["name"], "modality": modality[r["cat"]], "date": r["date"], "base_repo": r["base"], "official_repos": [m["id"] for m in r["models"]], "community_models": r["n"], "builders": r["authors"], "community_downloads_30d": r["dl30"], "community_downloads_all_time": r["dlall"], "official_downloads_30d": r["official"].get("dl30"), "official_downloads_all_time": r["official"].get("dlall"), "has_page": r["page"], } for r in site["releases"]]) keep = set(models.repo_id) parts = [] hs = hubstats.read() if hs is not None: parts.append(hs.assign(source="hub-stats")) own = [org.read_snapshots()] for fam in {Path(p).parent.name for p in (store.LOCAL / "raw").glob("*/*.parquet")} - {"org", "hubstats"}: raw = store.read_raw(fam) own.append(raw[raw.kind == "model"]) snap = pd.concat(own, ignore_index=True)[["day", "repo_id", "downloads_30d", "downloads_all_time"]] parts.append(snap.assign(source="snapshot")) downloads = pd.concat(parts, ignore_index=True) downloads = downloads[downloads.repo_id.isin(keep)] # one row per repo and day; our own snapshot wins over hub-stats on a shared day downloads = (downloads.sort_values(["repo_id", "day", "source"], ascending=[True, True, False]) .drop_duplicates(["repo_id", "day"]) [["day", "repo_id", "downloads_all_time", "downloads_30d", "source"]].reset_index(drop=True)) for c in ["downloads_all_time", "downloads_30d"]: downloads[c] = downloads[c].astype("Int64") return models, releases, downloads def publish(site, repos, public_ids, site_files, push=False, log=print): """Writes tables/ and site/ under the local mirror; with `push`, one commit to the dataset.""" from huggingface_hub import CommitOperationAdd models, releases, downloads = tables(site, repos) org.check_public({"ids": list(models.repo_id) + list(models.parent.dropna()) + list(downloads.repo_id.unique()) + [i for v in releases.official_repos for i in v] + list(releases.base_repo)}, public_ids) out = store.LOCAL / "tables" out.mkdir(parents=True, exist_ok=True) paths = {} for name, df in [("models", models), ("releases", releases), ("downloads", downloads)]: p = out / f"{name}.parquet" df.to_parquet(p, index=False) paths[f"tables/{name}.parquet"] = p for p in site_files: paths[f"site/{Path(p).name}"] = Path(p) if CARD.exists(): paths["README.md"] = CARD log(f"export: {len(models)} models, {len(releases)} releases, {len(downloads)} download rows") if push: ops = [CommitOperationAdd(path_in_repo=k, path_or_fileobj=str(v)) for k, v in paths.items()] api.create_commit(store.DATASET_REPO, ops, repo_type="dataset", commit_message=f"tables and site data {site['today']}") return paths