Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Sync from huggingface/lerobot-dataset-visualizer via hub-sync
Browse files
src/app/[org]/[dataset]/[episode]/fetch-data.ts
CHANGED
|
@@ -33,6 +33,84 @@ const HUB_ENDPOINT = (
|
|
| 33 |
process.env.DATASET_URL || "https://huggingface.co/datasets"
|
| 34 |
).replace(/\/datasets\/?$/, "");
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
const leRobotDatasets = new Map<string, LeRobotDataset>();
|
| 37 |
|
| 38 |
/**
|
|
@@ -47,6 +125,7 @@ function leRobotDataset(repoId: string): LeRobotDataset {
|
|
| 47 |
dataset = new LeRobotDataset(repoId, {
|
| 48 |
endpoint: HUB_ENDPOINT,
|
| 49 |
additionalFetchHeaders: authHeaders(),
|
|
|
|
| 50 |
});
|
| 51 |
leRobotDatasets.set(key, dataset);
|
| 52 |
}
|
|
@@ -242,6 +321,11 @@ const PROGRESS_PARQUET_CANDIDATES = [
|
|
| 242 |
"sarm_progress.parquet",
|
| 243 |
"srm_progress.parquet",
|
| 244 |
] as const;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
const PREFERRED_PROGRESS_COLUMNS = [
|
| 246 |
"progress_sparse",
|
| 247 |
"progress_dense",
|
|
@@ -335,6 +419,7 @@ async function loadEpisodeProgressGroup(
|
|
| 335 |
): Promise<((episodeDuration: number) => ChartRow[]) | null> {
|
| 336 |
for (const progressPath of PROGRESS_PARQUET_CANDIDATES) {
|
| 337 |
const progressUrl = buildVersionedUrl(repoId, version, progressPath);
|
|
|
|
| 338 |
try {
|
| 339 |
const progressBuffer = await fetchParquetFile(progressUrl);
|
| 340 |
const progressRows = await readParquetAsObjects(progressBuffer, []);
|
|
@@ -384,8 +469,12 @@ async function loadEpisodeProgressGroup(
|
|
| 384 |
[progressKey]: point.progress,
|
| 385 |
}));
|
| 386 |
};
|
| 387 |
-
} catch {
|
| 388 |
-
// Optional file: ignore and try next candidate.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
}
|
| 390 |
}
|
| 391 |
|
|
|
|
| 33 |
process.env.DATASET_URL || "https://huggingface.co/datasets"
|
| 34 |
).replace(/\/datasets\/?$/, "");
|
| 35 |
|
| 36 |
+
type RangeEntry =
|
| 37 |
+
| { ok: true; body: ArrayBuffer; status: number; contentRange: string | null }
|
| 38 |
+
| { ok: false; status: number; statusText: string };
|
| 39 |
+
|
| 40 |
+
const RANGE_CACHE_MAX_BYTES = 128 * 1024 * 1024;
|
| 41 |
+
const rangeCache = new Map<string, Promise<RangeEntry>>();
|
| 42 |
+
/** Sizes of the settled entries in `rangeCache`; in-flight ones have none yet. */
|
| 43 |
+
const rangeSizes = new Map<string, number>();
|
| 44 |
+
let rangeCacheBytes = 0;
|
| 45 |
+
|
| 46 |
+
/**
|
| 47 |
+
* Memoizes the package's range reads, least recently used out first past `RANGE_CACHE_MAX_BYTES`.
|
| 48 |
+
* Every package call opens its files afresh, and a v3 file is a single row group with no page index,
|
| 49 |
+
* so every episode in it requests byte-identical ranges. The browser cache never helps: the /resolve/
|
| 50 |
+
* 302 is `no-store` and points at a URL that is signed anew each time.
|
| 51 |
+
*/
|
| 52 |
+
const rangeCachedFetch = (async (input, init) => {
|
| 53 |
+
const headers = new Headers(init?.headers);
|
| 54 |
+
const range = headers.get("range");
|
| 55 |
+
if (!range) return fetch(input, init);
|
| 56 |
+
const url = input instanceof Request ? input.url : String(input);
|
| 57 |
+
// The request's own token, not getAuthToken(): a dataset instance keeps the headers it was built with.
|
| 58 |
+
const key = `${headers.get("authorization") ?? ""}\n${url}\n${range}`;
|
| 59 |
+
let pending = rangeCache.get(key);
|
| 60 |
+
if (pending) {
|
| 61 |
+
rangeCache.delete(key);
|
| 62 |
+
rangeCache.set(key, pending);
|
| 63 |
+
} else {
|
| 64 |
+
// Stored before it settles, so concurrent identical reads share one download.
|
| 65 |
+
pending = fetch(input, init)
|
| 66 |
+
.then(async (res): Promise<RangeEntry> => {
|
| 67 |
+
if (!res.ok) {
|
| 68 |
+
// Never cached: the package needs the 404 that ends its walk over the index shards.
|
| 69 |
+
rangeCache.delete(key);
|
| 70 |
+
return { ok: false, status: res.status, statusText: res.statusText };
|
| 71 |
+
}
|
| 72 |
+
const body = await res.arrayBuffer();
|
| 73 |
+
if (body.byteLength > RANGE_CACHE_MAX_BYTES / 2) {
|
| 74 |
+
rangeCache.delete(key);
|
| 75 |
+
} else {
|
| 76 |
+
rangeSizes.set(key, body.byteLength);
|
| 77 |
+
rangeCacheBytes += body.byteLength;
|
| 78 |
+
for (const k of rangeCache.keys()) {
|
| 79 |
+
if (rangeCacheBytes <= RANGE_CACHE_MAX_BYTES) break;
|
| 80 |
+
const size = rangeSizes.get(k);
|
| 81 |
+
if (size === undefined) continue;
|
| 82 |
+
rangeCache.delete(k);
|
| 83 |
+
rangeSizes.delete(k);
|
| 84 |
+
rangeCacheBytes -= size;
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
return {
|
| 88 |
+
ok: true,
|
| 89 |
+
body,
|
| 90 |
+
status: res.status,
|
| 91 |
+
contentRange: res.headers.get("content-range"),
|
| 92 |
+
};
|
| 93 |
+
})
|
| 94 |
+
.catch((err) => {
|
| 95 |
+
rangeCache.delete(key);
|
| 96 |
+
throw err;
|
| 97 |
+
});
|
| 98 |
+
rangeCache.set(key, pending);
|
| 99 |
+
}
|
| 100 |
+
const entry = await pending;
|
| 101 |
+
if (!entry.ok) {
|
| 102 |
+
return new Response(null, {
|
| 103 |
+
status: entry.status,
|
| 104 |
+
statusText: entry.statusText,
|
| 105 |
+
});
|
| 106 |
+
}
|
| 107 |
+
// The package reads the file size from content-range.
|
| 108 |
+
return new Response(entry.body, {
|
| 109 |
+
status: entry.status,
|
| 110 |
+
headers: entry.contentRange ? { "content-range": entry.contentRange } : {},
|
| 111 |
+
});
|
| 112 |
+
}) as typeof fetch; // Bun's types add `fetch.preconnect`, which the package never calls.
|
| 113 |
+
|
| 114 |
const leRobotDatasets = new Map<string, LeRobotDataset>();
|
| 115 |
|
| 116 |
/**
|
|
|
|
| 125 |
dataset = new LeRobotDataset(repoId, {
|
| 126 |
endpoint: HUB_ENDPOINT,
|
| 127 |
additionalFetchHeaders: authHeaders(),
|
| 128 |
+
fetch: rangeCachedFetch,
|
| 129 |
});
|
| 130 |
leRobotDatasets.set(key, dataset);
|
| 131 |
}
|
|
|
|
| 321 |
"sarm_progress.parquet",
|
| 322 |
"srm_progress.parquet",
|
| 323 |
] as const;
|
| 324 |
+
/**
|
| 325 |
+
* Progress files the Hub answered 404 for. With the range cache serving a same-file switch, probing
|
| 326 |
+
* them again would be most of what the switch costs.
|
| 327 |
+
*/
|
| 328 |
+
const missingProgressFiles = new Set<string>();
|
| 329 |
const PREFERRED_PROGRESS_COLUMNS = [
|
| 330 |
"progress_sparse",
|
| 331 |
"progress_dense",
|
|
|
|
| 419 |
): Promise<((episodeDuration: number) => ChartRow[]) | null> {
|
| 420 |
for (const progressPath of PROGRESS_PARQUET_CANDIDATES) {
|
| 421 |
const progressUrl = buildVersionedUrl(repoId, version, progressPath);
|
| 422 |
+
if (missingProgressFiles.has(progressUrl)) continue;
|
| 423 |
try {
|
| 424 |
const progressBuffer = await fetchParquetFile(progressUrl);
|
| 425 |
const progressRows = await readParquetAsObjects(progressBuffer, []);
|
|
|
|
| 469 |
[progressKey]: point.progress,
|
| 470 |
}));
|
| 471 |
};
|
| 472 |
+
} catch (err) {
|
| 473 |
+
// Optional file: ignore and try next candidate. hyparquet reports a missing file as
|
| 474 |
+
// "fetch head failed 404"; any other failure is retried next time.
|
| 475 |
+
if (err instanceof Error && /\b404$/.test(err.message)) {
|
| 476 |
+
missingProgressFiles.add(progressUrl);
|
| 477 |
+
}
|
| 478 |
}
|
| 479 |
}
|
| 480 |
|
src/components/urdf-viewer.tsx
CHANGED
|
@@ -853,9 +853,12 @@ export default function URDFViewer({
|
|
| 853 |
const chartDataCache = useRef<Record<number, Record<string, number>[]>>({
|
| 854 |
[data.episodeId]: data.flatChartData,
|
| 855 |
});
|
|
|
|
|
|
|
| 856 |
|
| 857 |
const handleEpisodeChange = useCallback(
|
| 858 |
(epId: number) => {
|
|
|
|
| 859 |
setSelectedEpisode(epId);
|
| 860 |
setFrame(0);
|
| 861 |
frameRef.current = 0;
|
|
@@ -863,6 +866,7 @@ export default function URDFViewer({
|
|
| 863 |
|
| 864 |
if (chartDataCache.current[epId]) {
|
| 865 |
setChartData(chartDataCache.current[epId]);
|
|
|
|
| 866 |
return;
|
| 867 |
}
|
| 868 |
|
|
@@ -881,10 +885,12 @@ export default function URDFViewer({
|
|
| 881 |
.then((result) => {
|
| 882 |
if (!result) return;
|
| 883 |
chartDataCache.current[epId] = result;
|
| 884 |
-
setChartData(result);
|
| 885 |
})
|
| 886 |
.catch((err) => console.error("Failed to load episode:", err))
|
| 887 |
-
.finally(() =>
|
|
|
|
|
|
|
| 888 |
},
|
| 889 |
[ensureDatasetInfo, repoId],
|
| 890 |
);
|
|
|
|
| 853 |
const chartDataCache = useRef<Record<number, Record<string, number>[]>>({
|
| 854 |
[data.episodeId]: data.flatChartData,
|
| 855 |
});
|
| 856 |
+
/// A slower load for an earlier pick must not overwrite the episode picked since.
|
| 857 |
+
const latestEpisodeRef = useRef(data.episodeId);
|
| 858 |
|
| 859 |
const handleEpisodeChange = useCallback(
|
| 860 |
(epId: number) => {
|
| 861 |
+
latestEpisodeRef.current = epId;
|
| 862 |
setSelectedEpisode(epId);
|
| 863 |
setFrame(0);
|
| 864 |
frameRef.current = 0;
|
|
|
|
| 866 |
|
| 867 |
if (chartDataCache.current[epId]) {
|
| 868 |
setChartData(chartDataCache.current[epId]);
|
| 869 |
+
setEpisodeLoading(false);
|
| 870 |
return;
|
| 871 |
}
|
| 872 |
|
|
|
|
| 885 |
.then((result) => {
|
| 886 |
if (!result) return;
|
| 887 |
chartDataCache.current[epId] = result;
|
| 888 |
+
if (latestEpisodeRef.current === epId) setChartData(result);
|
| 889 |
})
|
| 890 |
.catch((err) => console.error("Failed to load episode:", err))
|
| 891 |
+
.finally(() => {
|
| 892 |
+
if (latestEpisodeRef.current === epId) setEpisodeLoading(false);
|
| 893 |
+
});
|
| 894 |
},
|
| 895 |
[ensureDatasetInfo, repoId],
|
| 896 |
);
|