Hugging Face style model pages at /<org>/<name>
- Model URLs are the repo path (case-insensitive, canonical redirect); /models/<id> redirects - Header with avatar, org/name + copy, task/format/arch/language/license pills, user tags, cross-links to the Hub (files @ sha, commits, community, base model) and tabs: rendered model card at the pinned commit, files, jobs - Sidebar: download card, local copy, model tree with related local models - Metadata gains author avatar and base relation (refetched once) - Proxy matcher excludes only exact /login and /api/auth/ paths Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d16bd6dfee
commit
1527fcd830
29 files changed
+4335
-269
No files matched your search
@@ -193,6 +193,21 @@ function nextLink(link: string | null): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Avatar URL of a Hub org or user, or null. Best effort: errors are swallowed. */
|
||||
export async function getAuthorAvatar(name: string): Promise<string | null> {
|
||||
for (const path of [`organizations/${enc(name)}/avatar`, `users/${enc(name)}/overview`]) {
|
||||
try {
|
||||
const res = await hubFetch(`${HUB}/api/${path}`);
|
||||
if (!res.ok) continue;
|
||||
const j = (await res.json()) as { avatarUrl?: string };
|
||||
if (j.avatarUrl?.startsWith("https://")) return j.avatarUrl;
|
||||
} catch {
|
||||
// ignore, try next
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* For gated repos the file list is public, but downloads need accepted terms.
|
||||
* Probe one file with a HEAD request (no body is downloaded) to find out early.
|
||||
@@ -203,3 +218,30 @@ export async function checkFileAccess(repoId: string, sha: string, path: string)
|
||||
if (res.ok || (res.status >= 300 && res.status < 400)) return;
|
||||
throw await toHubError(res, repoId, sha);
|
||||
}
|
||||
|
||||
const cardCache = new Map<string, string | null>();
|
||||
const CARD_CACHE_MAX = 100;
|
||||
const CARD_MAX_BYTES = 512 * 1024;
|
||||
|
||||
/**
|
||||
* The repo's README.md at a commit, without YAML front matter. Null if there is none.
|
||||
* Cached in memory: a commit's content never changes.
|
||||
*/
|
||||
export async function fetchModelCard(repoId: string, sha: string): Promise<string | null> {
|
||||
assertRepoId(repoId);
|
||||
const key = `${repoId}@${sha}`;
|
||||
if (cardCache.has(key)) return cardCache.get(key)!;
|
||||
|
||||
const res = await hubFetch(`${HUB}/${repoPath(repoId)}/raw/${enc(sha)}/README.md`);
|
||||
if (res.status === 404) {
|
||||
cardCache.set(key, null);
|
||||
return null;
|
||||
}
|
||||
if (!res.ok) throw await toHubError(res, repoId, sha);
|
||||
let text = (await res.text()).slice(0, CARD_MAX_BYTES);
|
||||
text = text.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, "").trim();
|
||||
|
||||
if (cardCache.size >= CARD_CACHE_MAX) cardCache.delete(cardCache.keys().next().value!);
|
||||
cardCache.set(key, text || null);
|
||||
return text || null;
|
||||
}
|
||||
Reference in new issue
Block a user