Spaces:
Runtime error
Runtime error
File size: 5,397 Bytes
7cb11f5 de9b734 7cb11f5 030f336 7cb11f5 030f336 7cb11f5 030f336 7cb11f5 de9b734 7cb11f5 de9b734 7cb11f5 de9b734 7cb11f5 |
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 |
/**
* Hugging Face Dataset Integration
*
* Uploads benchmark results to a Hugging Face Dataset repository.
* - Preserves file path structure: {task}/{org}/{model}/{params}.json
* - Uses JSON format (not JSONL)
* - Overwrites existing files instead of appending
*/
import { uploadFile, listFiles } from "@huggingface/hub";
import { generateBenchmarkPath, type BenchmarkSettings } from "../core/benchmark-id.js";
import type { QueuedBenchmark } from "./queue.js";
import { logger } from "../core/logger.js";
export interface HFDatasetConfig {
repo: string;
token: string;
}
export class HFDatasetUploader {
private config: HFDatasetConfig | null = null;
constructor(config?: HFDatasetConfig) {
if (config && config.repo && config.token) {
this.config = config;
}
}
/**
* Check if HF Dataset upload is enabled
*/
isEnabled(): boolean {
return this.config !== null;
}
/**
* Get the HF Dataset file path for a benchmark
*/
private getHFFilePath(benchmark: QueuedBenchmark): string {
const settings: BenchmarkSettings = {
platform: benchmark.platform,
modelId: benchmark.modelId,
task: benchmark.task,
mode: benchmark.mode,
device: benchmark.device,
dtype: benchmark.dtype,
batchSize: benchmark.batchSize,
browser: benchmark.browser,
headed: benchmark.headed,
environment: benchmark.result?.environment ? {
cpu: benchmark.result.environment.cpu,
memory: benchmark.result.environment.memory,
gpu: benchmark.result.environment.gpu,
platform: benchmark.result.environment.platform,
arch: benchmark.result.environment.arch,
cpuCores: benchmark.result.environment.cpuCores, // Web browser format
} : undefined,
};
const { fullPath } = generateBenchmarkPath(settings);
// Replace .jsonl extension with .json
return fullPath.replace(/\.jsonl$/, ".json");
}
/**
* Transform benchmark data for HF Dataset upload
* Lifts frequently-accessed fields to top level for easier browsing
*/
private transformForUpload(benchmark: QueuedBenchmark): any {
const result = benchmark.result || {};
return {
// Top-level metadata
id: benchmark.id,
status: benchmark.status,
timestamp: benchmark.timestamp,
startedAt: benchmark.startedAt,
completedAt: benchmark.completedAt,
// Configuration (lifted to top level)
platform: benchmark.platform,
modelId: benchmark.modelId,
task: benchmark.task,
mode: benchmark.mode,
device: benchmark.device,
dtype: benchmark.dtype,
batchSize: benchmark.batchSize,
repeats: benchmark.repeats,
// Browser-specific (only if web platform)
...(benchmark.platform === "web" && {
browser: benchmark.browser,
headed: benchmark.headed,
}),
// Runtime info (lifted from result)
runtime: result.runtime,
// Metrics (lifted to top level for easy access)
metrics: result.metrics,
// Environment (lifted to top level for easy access)
environment: result.environment,
// Error info (if present)
...(result.error && { error: result.error }),
// Additional metadata
...(result.cacheDir && { cacheDir: result.cacheDir }),
...(result.notes && { notes: result.notes }),
};
}
/**
* Upload a benchmark result to HF Dataset
* Overwrites the file if it already exists
*/
async uploadResult(benchmark: QueuedBenchmark): Promise<void> {
if (!this.config) {
throw new Error("HF Dataset upload is not configured");
}
const filePath = this.getHFFilePath(benchmark);
// Transform and convert benchmark to JSON string
const transformed = this.transformForUpload(benchmark);
const content = JSON.stringify(transformed, null, 2);
const blob = new Blob([content], { type: "application/json" });
try {
// Upload file to HF Dataset (overwrites if exists)
await uploadFile({
repo: {
type: "dataset",
name: this.config.repo,
},
credentials: { accessToken: this.config.token },
file: {
path: filePath,
content: blob,
},
commitTitle: `Update benchmark: ${benchmark.modelId} (${benchmark.platform}/${benchmark.task})`,
commitDescription: `Benchmark ID: ${benchmark.id}\nStatus: ${benchmark.status}\nTimestamp: ${new Date(benchmark.timestamp).toISOString()}`,
});
logger.log(`β Uploaded to HF Dataset: ${filePath}`);
} catch (error: any) {
logger.error(`β Failed to upload to HF Dataset: ${filePath}`, error.message);
throw error;
}
}
/**
* List all files in the HF Dataset
*/
async listAllFiles(): Promise<string[]> {
if (!this.config) {
throw new Error("HF Dataset upload is not configured");
}
try {
const files = [];
for await (const file of listFiles({
repo: {
type: "dataset",
name: this.config.repo,
},
credentials: { accessToken: this.config.token },
})) {
if (file.path.endsWith(".json")) {
files.push(file.path);
}
}
return files;
} catch (error: any) {
logger.error("β Failed to list files from HF Dataset", error.message);
throw error;
}
}
}
|