Hub: reuse commit trees, ask for SHAs only, stream the model card
- listFiles() keeps the last trees it fetched by repo@SHA (a commit's files never change; capped at 50,000 files): adding a model right after the lookup no longer downloads the whole tree again. Callers get copies. - addModel() fetches the author's avatar in parallel with the file list. - The Hub check resolves revisions with resolveRevision(): /revision/<rev> with expand=sha, as huggingface_hub's model_info(expand=...) does, instead of the full model info (file list, card data, the Spaces using the model). Falls back to the full request if an answer comes without a SHA. - The Model card tab renders inside <Suspense>: the page no longer waits for the README to arrive from the Hub (up to the 20 s timeout when it's slow). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
This commit is contained in:
7 files changed
+189
-34
No files matched your search
+45
-2
@@ -160,9 +160,50 @@ type TreeEntry = {
|
||||
xetHash?: string;
|
||||
};
|
||||
|
||||
/** All files (recursive) at a commit. Follows the Link header for pagination. */
|
||||
/**
|
||||
* The commit a revision points to. Asks the Hub for just the SHA (`expand=sha`, as huggingface_hub's
|
||||
* model_info does): the full model info can be large (file list, card data, the Spaces using it).
|
||||
* Falls back to the full info if an answer comes without a SHA.
|
||||
*/
|
||||
export async function resolveRevision(repoId: string, revision: string): Promise<string> {
|
||||
assertRepoId(repoId);
|
||||
const res = await hubFetch(`${HUB}/api/models/${repoPath(repoId)}/revision/${enc(revision)}?expand=sha`);
|
||||
if (res.ok) {
|
||||
const j = (await res.json().catch(() => null)) as { sha?: unknown } | null;
|
||||
if (typeof j?.sha === "string" && SHA_RE.test(j.sha)) return j.sha;
|
||||
} else if (res.status !== 400) {
|
||||
throw await toHubError(res, repoId, revision);
|
||||
}
|
||||
return (await getRepoInfo(repoId, revision)).sha;
|
||||
}
|
||||
|
||||
// A commit's file list never changes, so the last few are kept: adding a model right after looking
|
||||
// it up doesn't download its tree again. Capped by the number of files held.
|
||||
const treeCache = new Map<string, HubFile[]>();
|
||||
const TREE_CACHE_MAX_FILES = 50_000;
|
||||
let treeCacheFiles = 0;
|
||||
|
||||
function rememberTree(key: string, files: HubFile[]) {
|
||||
if (files.length > TREE_CACHE_MAX_FILES) return;
|
||||
treeCache.set(key, files);
|
||||
treeCacheFiles += files.length;
|
||||
for (const [k, v] of treeCache) {
|
||||
if (treeCacheFiles <= TREE_CACHE_MAX_FILES) break;
|
||||
treeCache.delete(k);
|
||||
treeCacheFiles -= v.length;
|
||||
}
|
||||
}
|
||||
|
||||
/** All files (recursive) at a commit, sorted by path. Follows the Link header for pagination. */
|
||||
export async function listFiles(repoId: string, sha: string): Promise<HubFile[]> {
|
||||
assertRepoId(repoId);
|
||||
const key = `${repoId}@${sha}`;
|
||||
const cached = treeCache.get(key);
|
||||
if (cached) {
|
||||
treeCache.delete(key); // most recently used last
|
||||
treeCache.set(key, cached);
|
||||
return [...cached];
|
||||
}
|
||||
const files: HubFile[] = [];
|
||||
let url: string | null =
|
||||
`${HUB}/api/models/${repoPath(repoId)}/tree/${enc(sha)}?recursive=true&expand=false&limit=1000`;
|
||||
@@ -181,7 +222,9 @@ export async function listFiles(repoId: string, sha: string): Promise<HubFile[]>
|
||||
}
|
||||
url = nextLink(res.headers.get("link"));
|
||||
}
|
||||
return files.sort((a, b) => a.path.localeCompare(b.path));
|
||||
files.sort((a, b) => a.path.localeCompare(b.path));
|
||||
if (SHA_RE.test(sha)) rememberTree(key, files);
|
||||
return [...files];
|
||||
}
|
||||
|
||||
function nextLink(link: string | null): string | null {
|
||||
|
||||
Reference in new issue
Block a user