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:
Claude committed 2026-11-01 21:04:05 +00:00
1 parent d58bff3437
commit 6f4932e04d
7 files changed
+189 -34

No files matched your search

+54
View File
@@ -1,7 +1,12 @@
import "server-only";
import { ExternalLinkIcon } from "lucide-react";
import ReactMarkdown, { defaultUrlTransform } from "react-markdown";
import rehypeRaw from "rehype-raw";
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
import remarkGfm from "remark-gfm";
import { Skeleton } from "@/components/ui/skeleton";
import { shortSha } from "@/lib/format";
import { fetchModelCard } from "@/lib/hub/client";
// Model cards mix Markdown and raw HTML. Parse the HTML (rehype-raw), then sanitize it: no scripts,
// styles or event handlers, only the tags/attributes GitHub-style READMEs normally use.
@@ -17,6 +22,55 @@ const schema = {
},
};
/**
* The Model card tab: the README at the pinned commit, fetched from the Hub (cached per commit).
* Rendered inside a Suspense boundary, so the rest of the page doesn't wait for the Hub.
*/
export async function ModelCardSection({ repoId, sha }: { repoId: string; sha: string }) {
const hub = `https://huggingface.co/${repoId}`;
const card = await fetchModelCard(repoId, sha).catch(() => undefined);
if (!card) {
return (
<p className="text-sm text-muted-foreground">
{card === null ? "This repository has no model card." : "The model card could not be loaded."}{" "}
<a href={hub} target="_blank" rel="noreferrer" className="underline">
Open on Hugging Face
</a>
</p>
);
}
return (
<>
<ModelCard markdown={card} repoId={repoId} sha={sha} />
<div className="mt-6 flex justify-end">
<a
href={`${hub}/blob/${sha}/README.md`}
target="_blank"
rel="noreferrer"
className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
>
README.md @ {shortSha(sha)}
<ExternalLinkIcon className="size-3" />
</a>
</div>
</>
);
}
export function ModelCardSkeleton() {
return (
<div className="flex flex-col gap-3" aria-busy="true" aria-label="Loading the model card">
<Skeleton className="h-7 w-2/5" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-11/12" />
<Skeleton className="h-4 w-4/5" />
<Skeleton className="mt-3 h-5 w-1/4" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-3/4" />
</div>
);
}
/** Renders a repo's README like the Hub: relative links/images point at the pinned commit. */
export function ModelCard({ markdown, repoId, sha }: { markdown: string; repoId: string; sha: string }) {
const base = `https://huggingface.co/${repoId}`;
+7 -28
View File
@@ -2,7 +2,6 @@ import {
ArrowDownIcon,
BoxIcon,
CpuIcon,
ExternalLinkIcon,
FileTextIcon,
FolderIcon,
HistoryIcon,
@@ -17,6 +16,7 @@ import {
import type { Metadata } from "next";
import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { Suspense } from "react";
import { CopyButton } from "@/components/copy-button";
import { BenchmarkStateBadge } from "@/components/benchmark-state";
import { PageHero } from "@/components/hero/page-hero";
@@ -27,7 +27,6 @@ import { cn } from "@/lib/utils";
import { formatBytes, shortSha } from "@/lib/format";
import { listBenchmarks } from "@/lib/benchmarks";
import { targetName } from "@/lib/benchmark-view";
import { fetchModelCard } from "@/lib/hub/client";
import { modelFamilies } from "@/lib/hub/metadata";
import { listModelJobs } from "@/lib/jobs";
import { getModelByRepo, relatedModels } from "@/lib/library";
@@ -39,7 +38,7 @@ import { DownloadCard } from "./download-card";
import { FilesTab } from "./files-tab";
import { HubCheckButton } from "./hub-check";
import { JobsTab } from "./jobs-tab";
import { ModelCard } from "./model-card";
import { ModelCardSection, ModelCardSkeleton } from "./model-card";
import { DeleteModelButton } from "./delete-model-button";
import { TagEditor } from "./tag-editor";
import { UpdateBanner } from "./update-banner";
@@ -101,10 +100,10 @@ export default async function ModelPage({ params, searchParams }: PageProps<"/[o
const hubTags = model.hubTags ?? [];
const hubMissing = model.hubState === "repo_missing" || model.hubState === "commit_missing";
const [tags, related, card, worker, recentJobs, runs] = await Promise.all([
// The model card (a Hub request) isn't awaited here: it streams in behind its Suspense boundary.
const [tags, related, worker, recentJobs, runs] = await Promise.all([
modelTagList(model.id),
relatedModels(model.id, baseModels),
tab === "card" ? fetchModelCard(model.repoId, model.pinnedSha).catch(() => undefined) : Promise.resolve(undefined),
getWorkerStatus(),
listModelJobs(model.id, 1),
listBenchmarks({ modelId: model.id }, { limit: 5 }), // the sidebar shows 5 and links to all
@@ -253,29 +252,9 @@ export default async function ModelPage({ params, searchParams }: PageProps<"/[o
{tab === "card" && (
<div className="p-5 sm:p-6">
{card ? (
<>
<ModelCard markdown={card} repoId={model.repoId} sha={model.pinnedSha} />
<div className="mt-6 flex justify-end">
<a
href={`${hub}/blob/${model.pinnedSha}/README.md`}
target="_blank"
rel="noreferrer"
className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
>
README.md @ {shortSha(model.pinnedSha)}
<ExternalLinkIcon className="size-3" />
</a>
</div>
</>
) : (
<p className="text-sm text-muted-foreground">
{card === null ? "This repository has no model card." : "The model card could not be loaded."}{" "}
<a href={hub} target="_blank" rel="noreferrer" className="underline">
Open on Hugging Face
</a>
</p>
)}
<Suspense fallback={<ModelCardSkeleton />}>
<ModelCardSection repoId={model.repoId} sha={model.pinnedSha} />
</Suspense>
</div>
)}
{tab === "files" && (