"""Compact overview helpers for the General + Training sections. Two small functions instead of one big gather() so the UI can stream the fast bit (model_info) before the slow bit (derivative counts). """ from huggingface_hub import HfApi from transformers import AutoConfig from helpers import HF_TOKEN from checks_reach import count_derivatives, fmt_date __all__ = ["format_general", "format_community"] def _fmt(n) -> str: return f"{n:,}" if isinstance(n, int) else "—" def format_general(model_id: str) -> str: api = HfApi(token=HF_TOKEN) try: info = api.model_info(model_id) except Exception as e: return f"❌ Could not fetch model info: `{e}`" try: cfg = AutoConfig.from_pretrained(model_id, token=HF_TOKEN, trust_remote_code=True) arch = (cfg.architectures or ["unknown"])[0] except Exception: arch = "—" downloads = getattr(info, "downloads", None) likes = getattr(info, "likes", None) last_mod = fmt_date(getattr(info, "last_modified", None)) created = fmt_date(getattr(info, "created_at", None)) return ( f"### [`{model_id}`](https://huggingface.co/{model_id})\n\n" f"| Architecture | Downloads (30d) | Likes | Created | Modified |\n" f"|---|---:|---:|---|---|\n" f"| `{arch}` | {_fmt(downloads)} | {_fmt(likes)} | {created} | {last_mod} |" ) def format_community(model_id: str) -> str: """Derivative counts (finetune / adapter / quantized / merge). Finetune count is the headline signal for 'community trains this model'. """ api = HfApi(token=HF_TOKEN) try: deriv = count_derivatives(api, model_id) except Exception as e: return f"❌ Could not fetch derivatives: `{e}`" def _line(kind, emoji): d = deriv.get(kind, {}) count = d.get("count") if count is None: return f"{emoji} **—** {kind}" suffix = "+" if d.get("capped") else "" return f"{emoji} **{count:,}{suffix}** {kind}" return "   •   ".join([ _line("finetune", "🧬"), _line("adapter", "🧩"), _line("quantized", "📦"), _line("merge", "🔀"), ])