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;
|
||||
}
|
||||
@@ -3,6 +3,8 @@
|
||||
export type RepoMetadata = {
|
||||
author: string | null;
|
||||
baseModels: string[];
|
||||
/** "quantized", "finetune", "adapter", "merge" (from base_model:<relation>:<id> tags). */
|
||||
baseRelation: string | null;
|
||||
architecture: string | null;
|
||||
pipelineTag: string | null;
|
||||
license: string | null;
|
||||
@@ -37,11 +39,14 @@ export function parseRepoMetadata(info: HubModelInfo): RepoMetadata {
|
||||
.filter((id) => id.includes("/"));
|
||||
}
|
||||
|
||||
const relation = tags.map((t) => t.match(/^base_model:([a-z_]+):/)?.[1]).find(Boolean) ?? null;
|
||||
|
||||
const license = asArray(info.cardData?.license)[0] ?? tags.find((t) => t.startsWith("license:"))?.slice(8) ?? null;
|
||||
|
||||
return {
|
||||
author: info.author ?? info.id?.split("/")[0] ?? null,
|
||||
baseModels: [...new Set(baseModels)],
|
||||
baseRelation: baseModels.length > 0 ? relation : null,
|
||||
architecture: info.gguf?.architecture ?? info.config?.model_type ?? null,
|
||||
pipelineTag: info.pipeline_tag ?? null,
|
||||
license,
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { eq, isNull } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { models } from "@/lib/db/schema";
|
||||
import { getRepoInfo } from "./client";
|
||||
import { getAuthorAvatar, getRepoInfo } from "./client";
|
||||
import type { RepoMetadata } from "./metadata";
|
||||
|
||||
/** Columns to write on `models` for fetched Hub metadata. */
|
||||
export function metadataColumns(meta: RepoMetadata) {
|
||||
export function metadataColumns(meta: RepoMetadata, authorAvatarUrl: string | null) {
|
||||
return {
|
||||
baseModels: meta.baseModels,
|
||||
baseRelation: meta.baseRelation,
|
||||
authorAvatarUrl,
|
||||
architecture: meta.architecture,
|
||||
pipelineTag: meta.pipelineTag,
|
||||
license: meta.license,
|
||||
@@ -29,7 +31,8 @@ export async function backfillMetadata(onError: (repoId: string, err: unknown) =
|
||||
for (const m of missing) {
|
||||
try {
|
||||
const info = await getRepoInfo(m.repoId, m.pinnedSha);
|
||||
await db().update(models).set(metadataColumns(info.metadata)).where(eq(models.id, m.id));
|
||||
const avatar = await getAuthorAvatar(m.repoId.split("/")[0]);
|
||||
await db().update(models).set(metadataColumns(info.metadata, avatar)).where(eq(models.id, m.id));
|
||||
updated++;
|
||||
} catch (err) {
|
||||
onError(m.repoId, err);
|
||||
|
||||
Reference in new issue
Block a user