File size: 15,112 Bytes
c1f2130
9d14977
 
c1f2130
9d14977
 
 
c1f2130
 
 
 
9d14977
c1f2130
9d14977
c1f2130
9d14977
c1f2130
 
 
 
fe99ffa
 
 
 
 
 
 
154e1d8
 
9d14977
 
 
154e1d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436ada0
154e1d8
 
 
 
 
 
 
9d14977
436ada0
9d14977
436ada0
9d14977
 
 
154e1d8
 
 
 
436ada0
 
 
bca888a
436ada0
 
154e1d8
 
 
 
 
 
9d14977
dd0b4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d14977
 
 
 
 
c1f2130
 
9d14977
 
 
c1f2130
9d14977
 
 
 
c1f2130
9d14977
 
 
 
 
 
 
 
 
 
 
 
154e1d8
 
 
 
 
 
9d14977
 
c1f2130
 
154e1d8
9d14977
154e1d8
 
c1f2130
 
9d14977
 
 
 
 
 
 
 
 
 
 
c1f2130
 
9d14977
 
 
c1f2130
9d14977
 
c1f2130
9d14977
 
 
c1f2130
9d14977
 
 
 
 
 
 
c1f2130
9d14977
 
 
c1f2130
 
9d14977
 
 
dd0b4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d14977
 
 
 
 
 
 
 
 
 
c1f2130
9d14977
c1f2130
 
9d14977
 
 
 
 
 
154e1d8
 
 
 
 
9d14977
 
 
 
 
c1f2130
9d14977
 
 
 
 
 
 
 
 
436ada0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d14977
c1f2130
9d14977
 
 
 
 
 
 
 
154e1d8
 
 
 
 
dd0b4fc
9d14977
 
 
 
 
 
 
 
 
c1f2130
9d14977
 
 
 
c1f2130
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/**
 * Downloads the HF dataset snapshot to local cache during build.
 * This avoids hitting HF rate limits at runtime and during cache refresh.
 *
 * Phase 1: Clone the dataset snapshot once via Git
 * Phase 2: Copy index files (model-cards, eval-list, developers, benchmark-metadata, peer-ranks)
 * Phase 3: Copy detail directories (developers/*.json, evals/*.json, models/*.json)
 *
 * Run with:  node scripts/cache-hf-data.mjs
 */

import { execFile } from "child_process"
import fs from "fs/promises"
import os from "os"
import path from "path"
import { promisify } from "util"

const root = path.resolve(new URL(import.meta.url).pathname, "..", "..")
const cacheDir = path.join(root, ".cache", "hf-data")
const publicDir = path.join(root, "public")
const dataBackend = process.env.DATA_BACKEND?.trim().toLowerCase()
if (dataBackend === "v2" || dataBackend === "stage-j") {
  await fs.mkdir(cacheDir, { recursive: true })
  console.log("[cache-hf-data] DATA_BACKEND=v2: skipping legacy HF cache; runtime reads SNAPSHOT_URL")
  process.exit(0)
}

const HF_DATASET_REPO = process.env.HF_DATASET_REPO?.trim()
  || "https://huggingface.co/datasets/evaleval/card_backend"
const HF_RESOLVE_BASE = `${HF_DATASET_REPO}/resolve/main`
const execFileAsync = promisify(execFile)

// Lean DuckDB mode: when DATA_BACKEND=duckdb, the runtime reads model/eval/
// developer/summary data exclusively from `duckdb/v1/*.parquet` (see
// lib/duckdb-data.ts:48,82). The legacy JSON-fallback artifacts
// (model-cards*, eval-list*, developers*, plus the per-slug detail
// directories developers/, evals/, models/) become dead weight β€” skipping
// them avoids OOMing the HF Space on a 2.8 GB JSON snapshot.
//
// Always keep:
//   - manifest.json           (lib/data-backend.ts:96 β†’ /api/backend-manifest)
//   - eval-hierarchy.json     (lib/data-backend.ts:98 β†’ /api/eval-hierarchy)
//   - benchmark-metadata.json (lib/benchmark-metadata.ts:5 β†’ /api/benchmark-metadata)
//   - comparison-index.json   (app/api/comparison-index/route.ts:7 β†’ app/models/[id]/page.tsx:157)
//   - corpus-aggregates.json  (app/corpus/page.tsx via lib/hf-data:fetchCorpusAggregates)
//   - duckdb/v1/*.parquet     (lib/duckdb-data.ts read path)
//   - public/peer-ranks.json  (always written outside cacheDir; component fetches HF directly)
const isDuckDBLean = process.env.DATA_BACKEND?.trim().toLowerCase() === "duckdb"

const ESSENTIAL_CACHE_ROOT_FILES = [
  "manifest.json",
  "benchmark-metadata.json",
  "eval-hierarchy.json",
  "comparison-index.json",
  "corpus-aggregates.json",
]

const JSON_FALLBACK_CACHE_ROOT_FILES = [
  "model-cards.json",
  "model-cards-lite.json",
  "eval-list.json",
  "eval-list-lite.json",
  "developers.json",
]

const CACHE_ROOT_FILES = isDuckDBLean
  ? ESSENTIAL_CACHE_ROOT_FILES
  : [...ESSENTIAL_CACHE_ROOT_FILES, ...JSON_FALLBACK_CACHE_ROOT_FILES]

const OPTIONAL_CACHE_ROOT_FILES = new Set([
  "model-cards-lite.json",
  "eval-list-lite.json",
  "corpus-aggregates.json",
])

const ESSENTIAL_CACHE_DIRECTORIES = ["duckdb"]
const JSON_FALLBACK_CACHE_DIRECTORIES = ["developers", "evals", "models"]

const CACHE_DIRECTORIES = isDuckDBLean
  ? ESSENTIAL_CACHE_DIRECTORIES
  : [...JSON_FALLBACK_CACHE_DIRECTORIES, ...ESSENTIAL_CACHE_DIRECTORIES]

const TOKEN_CASE_MAP = {
  ai: "AI",
  claude: "Claude",
  fc: "FC",
  gemini: "Gemini",
  gemma: "Gemma",
  gpt: "GPT",
  haiku: "Haiku",
  mini: "Mini",
  opus: "Opus",
  preview: "Preview",
  pro: "Pro",
  prompt: "Prompt",
  reasoning: "Reasoning",
  sonnet: "Sonnet",
  thinking: "Thinking",
}

async function runGit(args, cwd) {
  await execFileAsync("git", args, {
    cwd,
    maxBuffer: 1024 * 1024 * 32,
  })
}

async function cloneDatasetSnapshot(targetDir) {
  await runGit(["clone", "--depth", "1", "--single-branch", HF_DATASET_REPO, targetDir], root)
}

async function ensureCleanDirectory(dirPath) {
  await fs.rm(dirPath, { recursive: true, force: true })
  await fs.mkdir(dirPath, { recursive: true })
}

async function copyFile(sourcePath, destinationPath) {
  await fs.mkdir(path.dirname(destinationPath), { recursive: true })
  await fs.copyFile(sourcePath, destinationPath)
  const stat = await fs.stat(destinationPath)
  return stat.size
}

function isGitLfsPointer(contents) {
  return contents.startsWith("version https://git-lfs.github.com/spec/v1\n")
}

async function writeRemoteFile(relativePath, destinationPath) {
  const headers = {}
  const hfToken = process.env.HF_TOKEN?.trim()
  if (hfToken) {
    headers.Authorization = `Bearer ${hfToken}`
  }
  const response = await fetch(`${HF_RESOLVE_BASE}/${relativePath}`, { headers })
  if (!response.ok) {
    throw new Error(`Failed to download ${relativePath}: ${response.status} ${response.statusText}`)
  }

  const buffer = Buffer.from(await response.arrayBuffer())
  await fs.mkdir(path.dirname(destinationPath), { recursive: true })
  await fs.writeFile(destinationPath, buffer)
  return buffer.length
}

async function copySnapshotFile(snapshotRoot, relativePath, destinationPath) {
  const sourcePath = path.join(snapshotRoot, relativePath)
  const contents = await fs.readFile(sourcePath, "utf8")

  if (isGitLfsPointer(contents)) {
    const size = await writeRemoteFile(relativePath, destinationPath)
    return { size, source: "remote" }
  }

  const size = await copyFile(sourcePath, destinationPath)
  return { size, source: "snapshot" }
}

async function copySnapshotDirectory(snapshotRoot, relativeDir, destinationDir) {
  const sourceDir = path.join(snapshotRoot, relativeDir)
  const entries = await fs.readdir(sourceDir, { withFileTypes: true })

  let fileCount = 0
  let remoteCount = 0

  for (const entry of entries) {
    const nestedRelativePath = path.posix.join(relativeDir, entry.name)
    const destinationPath = path.join(destinationDir, entry.name)

    if (entry.isDirectory()) {
      await ensureCleanDirectory(destinationPath)
      const nested = await copySnapshotDirectory(snapshotRoot, nestedRelativePath, destinationPath)
      fileCount += nested.fileCount
      remoteCount += nested.remoteCount
      continue
    }

    const result = await copySnapshotFile(snapshotRoot, nestedRelativePath, destinationPath)
    fileCount += 1
    remoteCount += result.source === "remote" ? 1 : 0
  }

  return { fileCount, remoteCount }
}

function normalizeHandle(rawHandle) {
  return rawHandle
    .trim()
    .toLowerCase()
    .replace(/[_\s/]+/g, "-")
    .replace(/(\d)-(?=\d(?:-|$))/g, "$1.")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "")
}

function formatVersionDate(dateToken) {
  if (!/^(?:19|20)\d{6}$/.test(dateToken)) {
    return dateToken
  }

  return `${dateToken.slice(0, 4)}-${dateToken.slice(4, 6)}-${dateToken.slice(6, 8)}`
}

function titleCaseToken(token) {
  if (!token) {
    return token
  }

  if (TOKEN_CASE_MAP[token]) {
    return TOKEN_CASE_MAP[token]
  }

  if (/^\d+(\.\d+)?$/.test(token)) {
    return token
  }

  if (/^\d+(\.\d+)?[bkmt]$/i.test(token)) {
    return `${token.slice(0, -1)}${token.slice(-1).toUpperCase()}`
  }

  return token.charAt(0).toUpperCase() + token.slice(1)
}

function humanizeHandle(handle) {
  return handle
    .split("-")
    .filter(Boolean)
    .map((token) => (/^(?:19|20)\d{6}$/.test(token) ? formatVersionDate(token) : titleCaseToken(token)))
    .join(" ")
}

function getCanonicalFamilyInfo(modelFamilyId) {
  const [namespace = "unknown", rawHandle = ""] = String(modelFamilyId).split("/")
  const normalizedHandle = normalizeHandle(rawHandle)
  const match = normalizedHandle.match(/^(.*?)-((?:19|20)\d{6})(?:-(.+))?$/)
  const familySlug = match ? match[1] : normalizedHandle

  return {
    familyId: `${namespace}/${familySlug}`,
    familyName: humanizeHandle(familySlug),
  }
}

function normalizeSetupAliasQualifier(value) {
  return String(value ?? "").trim().toLowerCase().replace(/[_\s]+/g, "-")
}

function isSetupAliasQualifier(value) {
  const normalized = normalizeSetupAliasQualifier(value)
  return (
    normalized === "prompt" ||
    normalized === "fc" ||
    normalized === "function-calling" ||
    normalized.startsWith("thinking")
  )
}

function getNormalizedVariantMeta(familyId, variantKey, variantLabel) {
  if (variantKey === "default" || variantKey === "base") {
    return {
      variantKey: "default",
      variantLabel: "Default",
    }
  }

  const familyHandle = familyId.split("/")[1] ?? ""
  const compositeHandle = normalizeHandle(`${familyHandle}-${variantKey}`)
  const match = compositeHandle.match(/^(.*?)-((?:19|20)\d{6})(?:-(.+))?$/)

  if (!match) {
    return {
      variantKey,
      variantLabel,
    }
  }

  const [, , dateToken, qualifier] = match
  if (qualifier && isSetupAliasQualifier(humanizeHandle(qualifier))) {
    const formattedDate = formatVersionDate(dateToken)
    return {
      variantKey: formattedDate,
      variantLabel: formattedDate,
    }
  }

  const formattedDate = formatVersionDate(dateToken)
  return {
    variantKey,
    variantLabel: qualifier ? `${formattedDate} Β· ${humanizeHandle(qualifier)}` : formattedDate,
  }
}

function normalizeModelCardEntry(entry) {
  const familyInfo = getCanonicalFamilyInfo(entry.model_family_id)
  const variantsByKey = new Map()

  for (const variant of entry.variants ?? []) {
    const meta = getNormalizedVariantMeta(familyInfo.familyId, variant.variant_key, variant.variant_label)
    const existing = variantsByKey.get(meta.variantKey)

    if (existing) {
      existing.evaluation_count += variant.evaluation_count
      existing.last_updated =
        !existing.last_updated || (variant.last_updated && new Date(variant.last_updated).getTime() > new Date(existing.last_updated).getTime())
          ? variant.last_updated
          : existing.last_updated
      existing.raw_model_ids = Array.from(
        new Set([...(existing.raw_model_ids ?? []), ...(variant.raw_model_ids ?? [])])
      ).sort((a, b) => a.localeCompare(b))
      continue
    }

    variantsByKey.set(meta.variantKey, {
      ...variant,
      variant_key: meta.variantKey,
      variant_label: meta.variantLabel,
      raw_model_ids: [...(variant.raw_model_ids ?? [])].sort((a, b) => a.localeCompare(b)),
    })
  }

  const variants = Array.from(variantsByKey.values()).sort((a, b) => {
    const aIsDefault = a.variant_key === "default"
    const bIsDefault = b.variant_key === "default"
    if (aIsDefault !== bIsDefault) {
      return aIsDefault ? -1 : 1
    }

    const aTime = a.last_updated ? new Date(a.last_updated).getTime() : Number.NEGATIVE_INFINITY
    const bTime = b.last_updated ? new Date(b.last_updated).getTime() : Number.NEGATIVE_INFINITY
    if (aTime !== bTime) {
      return bTime - aTime
    }

    return a.variant_label.localeCompare(b.variant_label)
  })

  return {
    ...entry,
    model_family_id: familyInfo.familyId,
    model_route_id: familyInfo.familyId.replace(/\//g, "__"),
    model_family_name: familyInfo.familyName,
    total_evaluations:
      variants.length > 0
        ? variants.reduce((sum, variant) => sum + (variant.evaluation_count ?? 0), 0)
        : entry.total_evaluations,
    variants,
  }
}

async function normalizeCachedModelCardFile(filePath) {
  try {
    const text = await fs.readFile(filePath, "utf8")
    const data = JSON.parse(text)
    if (!Array.isArray(data)) {
      return
    }

    const normalized = data.map(normalizeModelCardEntry)
    await fs.writeFile(filePath, `${JSON.stringify(normalized, null, 2)}\n`)
  } catch (error) {
    console.warn(`  β—‹ failed to normalize ${path.basename(filePath)}: ${error instanceof Error ? error.message : String(error)}`)
  }
}

async function countFiles(dirPath) {
  const entries = await fs.readdir(dirPath, { withFileTypes: true })
  let count = 0

  for (const entry of entries) {
    const fullPath = path.join(dirPath, entry.name)
    if (entry.isDirectory()) {
      count += await countFiles(fullPath)
      continue
    }

    count += 1
  }

  return count
}

async function main() {
  console.log("Caching HF dataset snapshot for build...\n")

  if (isDuckDBLean) {
    console.log("Lean DuckDB cache mode: skipping JSON-fallback artifacts (model-cards*, eval-list*, developers*, developers/, evals/, models/)")
    console.log("")
  }

  await fs.mkdir(cacheDir, { recursive: true })
  await fs.mkdir(publicDir, { recursive: true })

  const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "card-backend-"))

  try {
    // ── Phase 1: Clone snapshot ───────────────────────────────────────────
    console.log("Phase 1: Clone dataset snapshot")
    await cloneDatasetSnapshot(tempDir)
    console.log(`  βœ“ cloned ${HF_DATASET_REPO}`)

    // ── Phase 2: Root files ───────────────────────────────────────────────
    console.log("\nPhase 2: Copy index files")
    for (const fileName of CACHE_ROOT_FILES) {
      const destinationPath = path.join(cacheDir, fileName)
      try {
        const result = await copySnapshotFile(tempDir, fileName, destinationPath)
        const suffix = result.source === "remote" ? ", resolved from LFS" : ""
        console.log(`  βœ“ ${fileName} (${(result.size / 1024).toFixed(0)} KB${suffix})`)
      } catch (error) {
        if (
          OPTIONAL_CACHE_ROOT_FILES.has(fileName) &&
          typeof error === "object" &&
          error != null &&
          "code" in error &&
          error.code === "ENOENT"
        ) {
          console.log(`  β—‹ ${fileName} not published yet; skipping`)
          continue
        }

        throw error
      }
    }

    const peerRanksResult = await copySnapshotFile(
      tempDir,
      "peer-ranks.json",
      path.join(publicDir, "peer-ranks.json")
    )
    const peerRanksSuffix = peerRanksResult.source === "remote" ? ", resolved from LFS" : ""
    console.log(`  βœ“ peer-ranks.json (${(peerRanksResult.size / 1024).toFixed(0)} KB${peerRanksSuffix})`)

    if (!isDuckDBLean) {
      await normalizeCachedModelCardFile(path.join(cacheDir, "model-cards.json"))
      await normalizeCachedModelCardFile(path.join(cacheDir, "model-cards-lite.json"))
      console.log("  βœ“ normalized model card artifacts")
    }

    // ── Phase 3: Detail directories ─────────────────────────────────────
    console.log("\nPhase 3: Copy detail directories")
    for (const directoryName of CACHE_DIRECTORIES) {
      const destinationPath = path.join(cacheDir, directoryName)
      await ensureCleanDirectory(destinationPath)
      const result = await copySnapshotDirectory(tempDir, directoryName, destinationPath)
      const remoteSuffix = result.remoteCount > 0 ? `, ${result.remoteCount} resolved from LFS` : ""
      console.log(`  βœ“ ${directoryName}/ (${result.fileCount} files${remoteSuffix})`)
    }

    console.log("\nDone.")
  } finally {
    await fs.rm(tempDir, { recursive: true, force: true })
  }
}

main().catch((err) => {
  console.error(err)
  process.exit(1)
})