File size: 2,025 Bytes
04f9918
 
 
 
 
 
e025309
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
// Prints the Hugging Face model ids linked from the dataset card's leaderboard tables (one per line),
// so the Space README can list them under `models:` and show up in "Spaces using this model".
// Usage: node sync_models.js [--card README.md] [--write SPACE_README.md]
//   --card  read the card from a file instead of the live dataset
//   --write replace the `models:` list in the Space README front matter (prints "changed" or "unchanged")
const P = require('./parse.js');
const EXCLUDE = [];
(async () => {
  const arg = (k) => { const i = process.argv.indexOf(k); return i > 0 ? process.argv[i + 1] : null; };
  const src = arg('--card');
  const md = src ? require('fs').readFileSync(src, 'utf8')
    : await (await fetch('https://huggingface.co/datasets/LocalLLaMA/typed-decisions/resolve/main/README.md')).text();
  const ids = new Set();
  for (const r of P.parseReadme(md).rows) {
    const m = r.url && r.url.match(/^https:\/\/huggingface\.co\/(?!datasets\/|spaces\/|docs\/)([\w.-]+\/[\w.-]+)\/?$/);
    if (m && !EXCLUDE.some((re) => re.test(m[1]))) ids.add(m[1]);
  }
  // renamed repos redirect; keep the canonical id and drop ids that no longer exist
  const out = new Set();
  for (const id of ids) {
    const res = await fetch(`https://huggingface.co/api/models/${id}`);
    if (res.ok) out.add((await res.json()).id);
    else console.error(`skipped ${id}: HTTP ${res.status}`);
  }
  const list = [...out].sort((a, b) => a.localeCompare(b));
  const target = arg('--write');
  if (!target) { console.log(list.join('\n')); return; }
  const fs = require('fs');
  const readme = fs.readFileSync(target, 'utf8');
  const block = `models:\n${list.map((id) => `- ${id}`).join('\n')}\n`;
  const next = /^models:\n(?:- .*\n)*/m.test(readme)
    ? readme.replace(/^models:\n(?:- .*\n)*/m, block)
    : readme.replace(/^datasets:\n/m, `${block}datasets:\n`);
  if (next === readme) { console.log('unchanged'); return; }
  fs.writeFileSync(target, next);
  console.log(`changed: ${list.length} models`);
})();