Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
change the way we check the dataset version (#8)
Browse files4 unmodified lines5678910111213141516175 unmodified lines23242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091104 unmodified linesconst DATASET_URL = process.env.DATASET_URL || "https://huggingface.co/datasets";/*** Checks if a specific version/branch exists for a dataset*/async function checkVersionExists(repoId: string, version: string): Promise<boolean> {try {const testUrl = `${DATASET_URL}/${repoId}/resolve/${version}/meta/info.json`;// Try a simple GET request with a timeoutconst controller = new AbortController();const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout5 unmodified linesclearTimeout(timeoutId);// Check if it's a successful responseif (response.ok) {// Try to parse a bit of the JSON to make sure it's validtry {const text = await response.text();const data = JSON.parse(text);return !!data.features; // Only return true if it has features} catch (parseError) {return false;}}return false;} catch (error) {return false;}}/*** Checks if a dataset has v3.0 chunked structure*/async function checkV3ChunkedStructure(repoId: string): Promise<boolean> {try {const testUrl = `${DATASET_URL}/${repoId}/resolve/v3.0/meta/episodes/chunk-000/file-000.parquet`;const controller = new AbortController();const timeoutId = setTimeout(() => controller.abort(), 10000);const response = await fetch(testUrl, {method: "HEAD",cache: "no-store",signal: controller.signal});clearTimeout(timeoutId);return response.ok;} catch (error) {return false;}}/*** Determines the best available version for a dataset.* Prefers v3.0, falls back to v2.1, then v2.0, or throws an error if none exist.*/export async function getDatasetVersion(repoId: string): Promise<string> {// Check for v3.0 first - must have both info.json AND chunked episode structureconst hasV3Info = await checkVersionExists(repoId, "v3.0");if (hasV3Info) {const hasV3Structure = await checkV3ChunkedStructure(repoId);if (hasV3Structure) {return "v3.0";}}// Check for v2.1const hasV21 = await checkVersionExists(repoId, "v2.1");if (hasV21) {return "v2.1";}// Fall back to v2.0const hasV20 = await checkVersionExists(repoId, "v2.0");if (hasV20) {return "v2.0";}// If none of the supported versions exist, throw an errorthrow new Error(`Dataset ${repoId} is not compatible with this visualizer. ` +"This tool only works with dataset versions 3.0, 2.1, or 2.0. " +"Please use a compatible dataset version.");}/*** Constructs a versioned URL for dataset resources*/export function buildVersionedUrl(repoId: string, version: string, path: string): string {return `${DATASET_URL}/${repoId}/resolve/${version}/${path}`;}4 unmodified lines5678910111213141516171819202122232425262728293031323334355 unmodified lines41424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051064 unmodified linesconst DATASET_URL = process.env.DATASET_URL || "https://huggingface.co/datasets";/*** Dataset information structure from info.json*/interface DatasetInfo {codebase_version: string;robot_type: string | null;total_episodes: number;total_frames: number;total_tasks: number;chunks_size: number;data_files_size_in_mb: number;video_files_size_in_mb: number;fps: number;splits: Record<string, string>;data_path: string;video_path: string;features: Record<string, any>;}/*** Fetches dataset information from the main revision*/export async function getDatasetInfo(repoId: string): Promise<DatasetInfo> {try {const testUrl = `${DATASET_URL}/${repoId}/resolve/main/meta/info.json`;const controller = new AbortController();const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout5 unmodified linesclearTimeout(timeoutId);if (!response.ok) {throw new Error(`Failed to fetch dataset info: ${response.status}`);}const data = await response.json();// Check if it has the required structureif (!data.features) {throw new Error("Dataset info.json does not have the expected features structure");}return data as DatasetInfo;} catch (error) {if (error instanceof Error) {throw error;}throw new Error(`Dataset ${repoId} is not compatible with this visualizer. ` +"Failed to read dataset information from the main revision.");}}/*** Gets the dataset version by reading the codebase_version from the main revision's info.json*/export async function getDatasetVersion(repoId: string): Promise<string> {try {const datasetInfo = await getDatasetInfo(repoId);// Extract codebase_versionconst codebaseVersion = datasetInfo.codebase_version;if (!codebaseVersion) {throw new Error("Dataset info.json does not contain codebase_version");}// Validate that it's a supported versionconst supportedVersions = ["v3.0", "v2.1", "v2.0"];if (!supportedVersions.includes(codebaseVersion)) {throw new Error(`Dataset ${repoId} has codebase version ${codebaseVersion}, which is not supported. ` +"This tool only works with dataset versions 3.0, 2.1, or 2.0. " +"Please use a compatible dataset version.");}return codebaseVersion;} catch (error) {if (error instanceof Error) {throw error;}throw new Error(`Dataset ${repoId} is not compatible with this visualizer. ` +"Failed to read dataset information from the main revision.");}}export function buildVersionedUrl(repoId: string, version: string, path: string): string {return `${DATASET_URL}/${repoId}/resolve/main/${path}`;}
src/utils/versionUtils.ts+65-69