Download src/lib/data/exports.ts from hivetrace/GuardRateLeaderboard: direct link, hf CLI and curl.
- Browser
- Download file 5.42 kB
-
https://huggingface.co/spaces/hivetrace/GuardRateLeaderboard/resolve/main/src/lib/data/exports.ts
- Command line
-
hf download hf://spaces/hivetrace/GuardRateLeaderboard/src/lib/data/exports.ts
-
curl -L -o exports.ts https://huggingface.co/spaces/hivetrace/GuardRateLeaderboard/resolve/main/src/lib/data/exports.ts
5.42 kB
| import { | |
| buildRankingRows, | |
| compareRankingRows, | |
| rankingRowsToCsv, | |
| rankingScopeGroups | |
| } from '$lib/data/ranking'; | |
| import { toolsDataGroups, toolsRowsToCsv } from '$lib/data/tools'; | |
| import { | |
| buildMatrixGroups, | |
| buildMatrixRows, | |
| detailMatrixRowsToCsv, | |
| detailMetricValue, | |
| visibleDetailMatrixDatasets | |
| } from '$lib/data/details'; | |
| import type { DetailsDataset, DetailsDatasetMatrixCell } from '$lib/types/details-data'; | |
| import type { DetailMetric, MatrixDataset } from '$lib/types/details'; | |
| import type { LocalizedString } from '$lib/types/i18n'; | |
| import type { GuardModel } from '$lib/types/leaderboard'; | |
| import type { RankingDataset } from '$lib/types/ranking-data'; | |
| import type { ToolsDataset } from '$lib/types/tools-data'; | |
| const rankingScope = 'all'; | |
| const rankingSortKey = 'integral'; | |
| const rankingSortDirection = 'desc'; | |
| const detailsMatrixMetric: DetailMetric = 'score'; | |
| const detailsMatrixSortDataset = null; | |
| const detailsMatrixSortDirection = -1; | |
| function localizedEn(value: LocalizedString | null | undefined, fallback: string) { | |
| return value?.en ?? fallback; | |
| } | |
| function modelKey(model: GuardModel) { | |
| return model.modelId ?? model.name; | |
| } | |
| function csvNumberValue(value: number | null | undefined, digits = 4) { | |
| return typeof value === 'number' && Number.isFinite(value) ? Number(value.toFixed(digits)) : null; | |
| } | |
| function exportMatrixDatasets(dataset: DetailsDataset): MatrixDataset[] { | |
| return dataset.datasetMatrix.datasets.map((item) => ({ | |
| id: item.id, | |
| name: item.id, | |
| group: localizedEn(item.groupLabel, item.groupId), | |
| sourceGroup: localizedEn(item.groupLabel, item.groupId), | |
| label: localizedEn(item.label, item.id), | |
| language: item.language | |
| })); | |
| } | |
| function exportMatrixGroups(dataset: DetailsDataset) { | |
| const matrixDatasets = exportMatrixDatasets(dataset); | |
| const visibleDatasets = visibleDetailMatrixDatasets(matrixDatasets, dataset.allGroups, 'any'); | |
| return buildMatrixGroups(visibleDatasets); | |
| } | |
| function exportMatrixRows(dataset: DetailsDataset) { | |
| return buildMatrixRows( | |
| '', | |
| detailsMatrixMetric, | |
| detailsMatrixSortDataset, | |
| detailsMatrixSortDirection, | |
| dataset.models, | |
| exportMatrixDatasets(dataset), | |
| dataset.datasetMatrix.cellsByModelId | |
| ); | |
| } | |
| export function rankingExportRows(dataset: RankingDataset) { | |
| const groups = rankingScopeGroups(rankingScope, dataset); | |
| const rows = buildRankingRows(groups, true, dataset.models, dataset.allGroups.length); | |
| return [...rows].sort((a, b) => compareRankingRows(a, b, rankingSortKey, rankingSortDirection)); | |
| } | |
| export function rankingExportCsv(dataset: RankingDataset) { | |
| return rankingRowsToCsv(rankingExportRows(dataset)); | |
| } | |
| export function rankingExportJson(dataset: RankingDataset) { | |
| return { | |
| snapshotId: dataset.snapshotId, | |
| snapshotDate: dataset.snapshotDate, | |
| generatedAt: dataset.generatedAt, | |
| scope: rankingScope, | |
| sort: { | |
| key: rankingSortKey, | |
| direction: rankingSortDirection | |
| }, | |
| rows: rankingExportRows(dataset).map((row, index) => ({ | |
| rank: index + 1, | |
| model: row.model.name, | |
| size: row.size, | |
| integral: csvNumberValue(row.integral), | |
| min_group: csvNumberValue(row.minGroup), | |
| overall_fpr: csvNumberValue(row.fpr), | |
| overall_fnr: csvNumberValue(row.fnr), | |
| overall_f1: csvNumberValue(row.f1), | |
| p95_ms: csvNumberValue(row.p95, 1), | |
| run_date: row.model.runDate | |
| })) | |
| }; | |
| } | |
| export function detailsMatrixExportCsv(dataset: DetailsDataset) { | |
| return detailMatrixRowsToCsv( | |
| exportMatrixRows(dataset), | |
| exportMatrixGroups(dataset), | |
| detailsMatrixMetric, | |
| dataset.datasetMatrix.cellsByModelId | |
| ); | |
| } | |
| export function detailsMatrixExportJson(dataset: DetailsDataset) { | |
| const groups = exportMatrixGroups(dataset); | |
| const matrixRows = exportMatrixRows(dataset); | |
| const matrixDatasets = groups.flatMap((group) => group.datasets); | |
| const cellsByModelId: Record<string, Record<string, DetailsDatasetMatrixCell>> = dataset | |
| .datasetMatrix.cellsByModelId; | |
| return { | |
| snapshotId: dataset.snapshotId, | |
| snapshotDate: dataset.snapshotDate, | |
| generatedAt: dataset.generatedAt, | |
| metric: detailsMatrixMetric, | |
| datasets: matrixDatasets.map((item) => ({ | |
| id: item.id, | |
| group: item.group, | |
| label: item.label, | |
| language: item.language | |
| })), | |
| rows: matrixRows.map((model, index) => ({ | |
| rank: index + 1, | |
| model: model.name, | |
| organization: model.org, | |
| size: model.params, | |
| integral: csvNumberValue(model.integral), | |
| metric: detailsMatrixMetric, | |
| values: Object.fromEntries( | |
| matrixDatasets.map((item) => [ | |
| item.id, | |
| csvNumberValue( | |
| detailMetricValue(cellsByModelId[modelKey(model)]?.[item.id], detailsMatrixMetric) | |
| ) | |
| ]) | |
| ) | |
| })) | |
| }; | |
| } | |
| function toolsExportGroups(dataset: ToolsDataset) { | |
| return toolsDataGroups(dataset.models, dataset.allGroups); | |
| } | |
| export function toolsExportCsv(dataset: ToolsDataset) { | |
| return toolsRowsToCsv(dataset.models, toolsExportGroups(dataset)); | |
| } | |
| export function toolsExportJson(dataset: ToolsDataset) { | |
| const groups = toolsExportGroups(dataset); | |
| return { | |
| snapshotId: dataset.snapshotId, | |
| snapshotDate: dataset.snapshotDate, | |
| generatedAt: dataset.generatedAt, | |
| groups, | |
| rows: dataset.models.flatMap((model) => | |
| groups.map((group) => { | |
| const cell = model.groups[group] ?? null; | |
| return { | |
| model: model.name, | |
| group, | |
| score: csvNumberValue(cell?.score), | |
| fnr: csvNumberValue(cell?.fnr), | |
| fpr: csvNumberValue(cell?.fpr), | |
| f1: csvNumberValue(cell?.f1) | |
| }; | |
| }) | |
| ) | |
| }; | |
| } | |