File size: 6,820 Bytes
04f9918
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Parses the leaderboard tables of the LocalLLaMA/typed-decisions dataset card (README.md).
// Works in the browser (window.TDParse) and in Node (module.exports) so it can be tested offline.
(function (root) {
  'use strict';

  // Sizes the card does not state in a parseable way.
  const SIZE_OVERRIDES = {
    'Jeff-Gemma4-E2B': { total: 2e9, active: null, note: 'E2B, effective size' },
  };

  const DASH = /^[\s–—-]*$/;

  function splitRow(line) {
    let s = line.trim().replace(/\\\|/g, '\u0000');
    if (s.startsWith('|')) s = s.slice(1);
    if (s.endsWith('|')) s = s.slice(0, -1);
    return s.split('|').map((c) => c.replace(/\u0000/g, '|').trim());
  }

  function findTables(md) {
    const lines = md.split(/\r?\n/);
    const tables = [];
    let heading = '';
    for (let i = 0; i < lines.length; i++) {
      const l = lines[i];
      if (/^#{1,6}\s/.test(l)) heading = l.replace(/^#+\s*/, '').trim();
      if (l.trim().startsWith('|') && i + 1 < lines.length && /^\s*\|?\s*:?-{2,}/.test(lines[i + 1])) {
        const header = splitRow(l);
        const rows = [];
        i += 2;
        while (i < lines.length && lines[i].trim().startsWith('|')) rows.push(splitRow(lines[i++]));
        i--;
        tables.push({ heading, header, rows });
      }
    }
    return tables;
  }

  const LINK = /\[([^\]]+)\]\(([^)\s]+)\)/g;

  function links(cell) {
    return [...cell.matchAll(LINK)].map((m) => ({ text: m[1].replace(/\*\*|`/g, ''), url: m[2] }));
  }

  function text(cell) {
    return cell.replace(LINK, '$1').replace(/\*\*|__/g, '').replace(/`/g, '').replace(/\s+/g, ' ').trim();
  }

  function num(cell) {
    const t = text(cell).replace(/,/g, '');
    if (DASH.test(t)) return null;
    const m = t.match(/-?\d+(?:\.\d+)?/);
    return m ? parseFloat(m[0]) : null;
  }

  const SIZE_RE = /(?<![\w.])(\d+(?:\.\d+)?)\s?([BbMm])(?![a-zA-Z])(?:-A(\d+(?:\.\d+)?)B)?/;

  function parseSize(cellText, name) {
    if (SIZE_OVERRIDES[name]) return { ...SIZE_OVERRIDES[name] };
    const parens = [...cellText.matchAll(/\(([^()]*)\)/g)].map((m) => m[1]);
    for (const c of [...parens, name]) {
      const m = c.match(SIZE_RE);
      if (m) {
        const total = parseFloat(m[1]) * (/[Bb]/.test(m[2]) ? 1e9 : 1e6);
        return { total, active: m[3] ? parseFloat(m[3]) * 1e9 : null, note: '' };
      }
    }
    return null;
  }

  function parseLatency(cell) {
    const t = text(cell).replace(/,/g, '');
    if (DASH.test(t)) return null;
    const m = t.match(/(\d+(?:\.\d+)?)\s*(ms|s)\b/);
    if (!m) return null;
    const ms = parseFloat(m[1]) * (m[2] === 's' ? 1000 : 1);
    const note = (t.match(/\(([^)]*)\)/) || [])[1] || '';
    return { ms, note, raw: text(cell) };
  }

  function parsePrice(cell) {
    const t = text(cell);
    const m = t.match(/\$\s*(\d+(?:\.\d+)?)/);
    return { value: m ? parseFloat(m[1]) : null, raw: DASH.test(t) ? '' : t };
  }

  function modelName(cell) {
    const raw = cell.replace(/^\s*\*\*/, '');
    const first = links(cell).find((l) => !/^#\d+$/.test(l.text));
    if (first && raw.trimStart().startsWith('[' )) return { name: first.text, url: first.url };
    const t = text(cell);
    const cut = t.split(/\s\(|,\s|\svia\s/)[0].trim();
    return { name: cut, url: null };
  }

  function colIndex(header) {
    const find = (re) => header.findIndex((h) => re.test(h));
    return {
      rank: find(/^#$/),
      model: find(/^model/i),
      kind: find(/^kind/i),
      acc: find(/accuracy/i),
      kl: find(/^kl/i),
      brier: find(/brier/i),
      ece: find(/^ece/i),
      lat: find(/latency/i),
      price: find(/price/i),
    };
  }

  function parseRow(cells, ci, table) {
    const cell = (k) => (ci[k] >= 0 ? cells[ci[k]] || '' : '');
    const modelCell = cell('model');
    const full = text(modelCell);
    const { name, url } = modelName(modelCell);
    const kind = text(cell('kind'));
    const lat = parseLatency(cell('lat'));
    const price = parsePrice(cell('price'));
    const size = parseSize(full, name);
    const discussions = links(modelCell)
      .filter((l) => /^#\d+$/.test(l.text))
      .map((l) => ({ num: +l.text.slice(1), url: l.url }));
    const via = (full.match(/\bvia\s+(.+?)(?:\s\(|$)/) || [])[1] || '';
    const hfModel = url && /^https:\/\/huggingface\.co\/(?!datasets\/|spaces\/|docs\/)/.test(url);
    const open =
      /open weights/i.test(kind + ' ' + price.raw) || !!hfModel || /local/i.test(price.raw) || /\bours\b/.test(full);
    const apiServed = !open || !!via || /API/i.test(lat ? lat.note : '');
    const rankText = text(cell('rank'));
    return {
      table,
      rank: /^\d+$/.test(rankText) ? +rankText : null,
      name,
      url,
      full,
      kind,
      via,
      open,
      apiServed,
      params: size ? size.total : null,
      active: size ? size.active || size.total : null,
      sizeNote: size ? size.note : '',
      acc: num(cell('acc')),
      kl: num(cell('kl')),
      brier: num(cell('brier')),
      ece: num(cell('ece')),
      lat: lat ? lat.ms : null,
      latNote: lat ? lat.note : '',
      latRaw: lat ? lat.raw : '',
      price: price.value,
      priceRaw: price.raw,
      discussions,
      isRef: rankText !== '' && !/^\d+$/.test(rankText) && /reference/i.test(kind),
    };
  }

  function parseReadme(md) {
    const tables = findTables(md);
    const rows = [];
    const refs = [];
    for (const t of tables) {
      const ci = colIndex(t.header);
      if (ci.model >= 0 && ci.acc >= 0) {
        const fitted = /fitted|fine-tuned/i.test(t.heading);
        for (const cells of t.rows) {
          const r = parseRow(cells, ci, fitted ? 'fitted' : 'zero');
          if (r.isRef) refs.push({ name: r.name, acc: r.acc, kl: r.kl, brier: r.brier, ece: r.ece, what: r.full });
          else if (r.acc != null) rows.push(r);
        }
      } else if (/^reference$/i.test(t.header[0]) && t.header.some((h) => /accuracy/i.test(h))) {
        const ia = t.header.findIndex((h) => /accuracy/i.test(h));
        const iw = t.header.findIndex((h) => /what/i.test(h));
        for (const cells of t.rows) {
          const name = text(cells[0]);
          if (refs.some((r) => r.name.toLowerCase().startsWith(name.toLowerCase()))) continue;
          refs.push({ name, acc: num(cells[ia]), what: iw >= 0 ? text(cells[iw]) : '' });
        }
      }
    }
    rows.forEach((r, i) => (r.id = `${r.table}-${i}`));
    const ceilingMatch = md.match(/Scores well above\s+(\d\.\d+)/i);
    return { rows, refs, teacherCeiling: ceilingMatch ? parseFloat(ceilingMatch[1]) : null };
  }

  const api = { parseReadme, findTables, parseSize, parseLatency, text, links };
  if (typeof module !== 'undefined' && module.exports) module.exports = api;
  else root.TDParse = api;
})(typeof window !== 'undefined' ? window : globalThis);