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
@@ -78,6 +78,10 @@ export const models = sqliteTable(
|
||||
// Hub metadata (for search/filters), refreshed by the worker. Null until fetched.
|
||||
/** e.g. ["Qwen/Qwen3-8B"] (cardData.base_model, or base_model:* tags). */
|
||||
baseModels: text("base_models", { mode: "json" }).$type<string[]>(),
|
||||
/** How this model derives from its base: "quantized", "finetune", "adapter", "merge". */
|
||||
baseRelation: text("base_relation"),
|
||||
/** Avatar of the repo's org/user on the Hub. */
|
||||
authorAvatarUrl: text("author_avatar_url"),
|
||||
/** e.g. "qwen3" (gguf.architecture or config.model_type). */
|
||||
architecture: text("architecture"),
|
||||
pipelineTag: text("pipeline_tag"),
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -82,3 +82,24 @@ function groupBy<T>(items: T[], key: (t: T) => string, value: (t: T) => string |
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Look up a model by "org/name" (case-insensitive, like the Hub). */
|
||||
export async function getModelByRepo(repoId: string) {
|
||||
const row = await db()
|
||||
.select({ id: models.id })
|
||||
.from(models)
|
||||
.where(sql`lower(${models.repoId}) = lower(${repoId})`)
|
||||
.limit(1);
|
||||
return row[0] ? getModel(row[0].id) : null;
|
||||
}
|
||||
|
||||
export type RelatedModel = { repoId: string; quants: string[]; status: string };
|
||||
|
||||
/** Other models in the library that share a base model (e.g. another quant of the same Qwen). */
|
||||
export async function relatedModels(modelId: string, baseModels: string[]): Promise<RelatedModel[]> {
|
||||
if (baseModels.length === 0) return [];
|
||||
const all = await listLibrary();
|
||||
return all
|
||||
.filter((m) => m.id !== modelId && m.baseModels.some((b) => baseModels.includes(b)))
|
||||
.map((m) => ({ repoId: m.repoId, quants: m.quants, status: m.status }));
|
||||
}
|
||||
+2
-2
@@ -3,7 +3,7 @@ import { and, eq, sql } from "drizzle-orm";
|
||||
import { ulid } from "ulid";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { jobs, modelFiles, modelRevisions, models, modelTags } from "@/lib/db/schema";
|
||||
import { checkFileAccess, getRepoInfo, listFiles } from "@/lib/hub/client";
|
||||
import { checkFileAccess, getAuthorAvatar, getRepoInfo, listFiles } from "@/lib/hub/client";
|
||||
import { metadataColumns } from "@/lib/hub/refresh";
|
||||
import { isSelected, type SelectionRule } from "@/lib/selection";
|
||||
import { deleteUnusedTags } from "@/lib/tags";
|
||||
@@ -69,7 +69,7 @@ export async function addModel(input: {
|
||||
localPath: repoId,
|
||||
includePatterns: input.rule.include,
|
||||
excludePatterns: input.rule.exclude,
|
||||
...metadataColumns(info.metadata),
|
||||
...metadataColumns(info.metadata, await getAuthorAvatar(repoId.split("/")[0])),
|
||||
}),
|
||||
d.insert(modelRevisions).values({ modelId, sha: info.sha }),
|
||||
...chunk(fileRows, INSERT_CHUNK).map((c) => d.insert(modelFiles).values(c)),
|
||||
|
||||
Reference in new issue
Block a user