|
| 1 | +"""Render OpenMed model cards from manifest rows.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | + |
| 9 | +DEFAULT_ARXIV = "2508.01630" |
| 10 | + |
| 11 | + |
| 12 | +def render_model_card(row: dict[str, Any]) -> str: |
| 13 | + """Return a README.md model card for one manifest row.""" |
| 14 | + |
| 15 | + repo_id = _string(row.get("repo_id"), "OpenMed/model") |
| 16 | + title = repo_id.rsplit("/", 1)[-1] |
| 17 | + benchmark = row.get("benchmark") if isinstance(row.get("benchmark"), dict) else {} |
| 18 | + formats = _list(row.get("formats")) |
| 19 | + languages = _list(row.get("languages")) |
| 20 | + labels = _list(row.get("canonical_labels")) |
| 21 | + arxiv = _string(row.get("arxiv"), DEFAULT_ARXIV) |
| 22 | + license_name = _string(row.get("license"), "Not specified") |
| 23 | + task = _string(row.get("task"), "unknown") |
| 24 | + |
| 25 | + lines = [ |
| 26 | + "---", |
| 27 | + f"license: {license_name}", |
| 28 | + f"pipeline_tag: {task}", |
| 29 | + "library_name: openmed", |
| 30 | + "tags:", |
| 31 | + "- openmed", |
| 32 | + "- medical-nlp", |
| 33 | + "---", |
| 34 | + "", |
| 35 | + f"# {title}", |
| 36 | + "", |
| 37 | + "This model card is rendered from the OpenMed model manifest. Update `models.jsonl` and rerun the publish step instead of editing this file directly.", |
| 38 | + "", |
| 39 | + "## Manifest Summary", |
| 40 | + "", |
| 41 | + "| Field | Value |", |
| 42 | + "|---|---|", |
| 43 | + f"| Repository | `{repo_id}` |", |
| 44 | + f"| Family | {_string(row.get('family'), 'Not specified')} |", |
| 45 | + f"| Task | {task} |", |
| 46 | + f"| Languages | {_comma_or_unspecified(languages)} |", |
| 47 | + f"| Tier | {_string(row.get('tier'), 'Not specified')} |", |
| 48 | + f"| Parameters | {_format_param_count(row.get('param_count'))} |", |
| 49 | + f"| Architecture | {_string(row.get('architecture'), 'Not specified')} |", |
| 50 | + f"| Base model | {_string(row.get('base_model'), 'Not specified')} |", |
| 51 | + f"| Formats | {_comma_or_unspecified(formats)} |", |
| 52 | + f"| License | {license_name} |", |
| 53 | + f"| arXiv | {_arxiv_link(arxiv)} |", |
| 54 | + f"| Reproducibility hash | `{_string(row.get('reproducibility_hash'), 'Not specified')}` |", |
| 55 | + f"| Released | {_string(row.get('released'), 'Not specified')} |", |
| 56 | + "", |
| 57 | + "## Benchmark", |
| 58 | + "", |
| 59 | + "| Dataset | Micro F1 | Recall |", |
| 60 | + "|---|---:|---:|", |
| 61 | + f"| {_string(benchmark.get('dataset'), 'Not reported')} | {_metric(benchmark.get('micro_f1'))} | {_metric(benchmark.get('recall'))} |", |
| 62 | + "", |
| 63 | + "## Canonical Labels", |
| 64 | + "", |
| 65 | + _labels_block(labels), |
| 66 | + ] |
| 67 | + return "\n".join(lines) + "\n" |
| 68 | + |
| 69 | + |
| 70 | +def write_model_card(path: str | Path, row: dict[str, Any]) -> Path: |
| 71 | + """Write a rendered model card to *path* and return the path.""" |
| 72 | + |
| 73 | + path = Path(path) |
| 74 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 75 | + path.write_text(render_model_card(row), encoding="utf-8") |
| 76 | + return path |
| 77 | + |
| 78 | + |
| 79 | +def _string(value: Any, default: str) -> str: |
| 80 | + if value is None: |
| 81 | + return default |
| 82 | + text = str(value).strip() |
| 83 | + return text or default |
| 84 | + |
| 85 | + |
| 86 | +def _list(value: Any) -> list[str]: |
| 87 | + if not isinstance(value, list): |
| 88 | + return [] |
| 89 | + return [str(item) for item in value if str(item)] |
| 90 | + |
| 91 | + |
| 92 | +def _comma_or_unspecified(values: list[str]) -> str: |
| 93 | + return ", ".join(values) if values else "Not specified" |
| 94 | + |
| 95 | + |
| 96 | +def _format_param_count(value: Any) -> str: |
| 97 | + if not isinstance(value, int) or value <= 0: |
| 98 | + return "Not specified" |
| 99 | + if value >= 1_000_000_000: |
| 100 | + compact = f"{value / 1_000_000_000:g}B" |
| 101 | + elif value >= 1_000_000: |
| 102 | + compact = f"{value / 1_000_000:g}M" |
| 103 | + elif value >= 1_000: |
| 104 | + compact = f"{value / 1_000:g}K" |
| 105 | + else: |
| 106 | + compact = str(value) |
| 107 | + return f"{compact} ({value:,})" |
| 108 | + |
| 109 | + |
| 110 | +def _metric(value: Any) -> str: |
| 111 | + if isinstance(value, (int, float)): |
| 112 | + return f"{float(value):.4f}" |
| 113 | + return "Not reported" |
| 114 | + |
| 115 | + |
| 116 | +def _arxiv_link(value: str) -> str: |
| 117 | + if value == "Not specified": |
| 118 | + return value |
| 119 | + return f"[arXiv:{value}](https://arxiv.org/abs/{value})" |
| 120 | + |
| 121 | + |
| 122 | +def _labels_block(labels: list[str]) -> str: |
| 123 | + if not labels: |
| 124 | + return "Not specified." |
| 125 | + return ", ".join(f"`{label}`" for label in labels) |
0 commit comments