Show a Hugging Face repository's model card on the mirror page
Opening a repository shows its model card, rendered from markdown, with its base model, license and languages above it; the listing names each repository's base model and license under its name. The card is a stranger's text: raw HTML and dangerous links are dropped, images are shown as links rather than loaded, and relative links point into the repository. Adds github.com/yuin/goldmark for the markdown. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
02fa639657
commit
3fea39feb9
10 files changed
+375
-27
No files matched your search
@@ -122,7 +122,11 @@ type adminMirrorData struct {
|
||||
Query string
|
||||
Explore *explorer
|
||||
// Back is the listing an open repository was reached from.
|
||||
Back template.URL
|
||||
Back template.URL
|
||||
// Card is the open repository's model card, rendered.
|
||||
Card template.HTML
|
||||
// RepoURL is the open repository on the registry's own site.
|
||||
RepoURL string
|
||||
Repo *hf.Repo
|
||||
RepoSets []repoSet
|
||||
RegistryErr string
|
||||
@@ -466,6 +470,8 @@ func (p *AccountPages) explore(r *http.Request, d *adminMirrorData) {
|
||||
return
|
||||
}
|
||||
d.Repo = &got
|
||||
d.RepoURL = strings.TrimSuffix(got.DownloadURL("", ""), "/resolve/"+revOrMain(got.Revision)+"/")
|
||||
d.Card = renderCard(got.Card, d.RepoURL+"/blob/"+revOrMain(got.Revision)+"/")
|
||||
var weights []hf.File
|
||||
for _, f := range got.Files {
|
||||
if !f.IsWeights() {
|
||||
@@ -532,3 +538,10 @@ func (p *AccountPages) checkSource(ctx context.Context, source string) gguf.Verd
|
||||
}
|
||||
return gguf.Check(ctx, url, p.llamaVersion)
|
||||
}
|
||||
|
||||
func revOrMain(rev string) string {
|
||||
if rev == "" {
|
||||
return "main"
|
||||
}
|
||||
return rev
|
||||
}
|
||||
@@ -97,3 +97,20 @@ func TestAnAdapterIsNotListedAsAModel(t *testing.T) {
|
||||
t.Errorf("adapters should be left out and small models kept: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestARepositoryShowsItsCard(t *testing.T) {
|
||||
reg := &fakeRegistry{repo: hf.Repo{
|
||||
ID: "Qwen/X-GGUF", Revision: "abc123def456", License: "apache-2.0",
|
||||
BaseModel: []string{"Qwen/X"}, Card: "# X\n\nA **small** model. See [the license](LICENSE).",
|
||||
Files: []hf.File{{Path: "x.gguf", Bytes: 10}},
|
||||
}}
|
||||
body := mirrorGet(t, mirrorPageLlama(t, MirrorDeps{Mirror: &fakeMirror{}, Registry: reg}),
|
||||
"/admin/mirror?repo=Qwen/X-GGUF").Body.String()
|
||||
for _, want := range []string{"<strong>small</strong>", "license apache-2.0", "<code>Qwen/X</code>",
|
||||
`href="https://huggingface.co/Qwen/X-GGUF/blob/abc123def456/LICENSE"`,
|
||||
`href="https://huggingface.co/Qwen/X-GGUF"`} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("missing %s", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer"
|
||||
"github.com/yuin/goldmark/renderer/html"
|
||||
"github.com/yuin/goldmark/text"
|
||||
"github.com/yuin/goldmark/util"
|
||||
)
|
||||
|
||||
// renderCard turns a model card into HTML for the mirror page.
|
||||
//
|
||||
// The card is a stranger's text, so what it may do is narrow:
|
||||
// - raw HTML is dropped, which goldmark does unless told otherwise, and
|
||||
// links to javascript: and the like are emptied, which it also does;
|
||||
// - images are not loaded but shown as links, because an admin page that
|
||||
// fetches whatever a README points at tells a stranger who is looking;
|
||||
// - relative links, to the repository's own LICENSE and files, are made to
|
||||
// point at the repository rather than at this page.
|
||||
func renderCard(card, repoBase string) template.HTML {
|
||||
if strings.TrimSpace(card) == "" {
|
||||
return ""
|
||||
}
|
||||
md := goldmark.New(
|
||||
goldmark.WithExtensions(extension.GFM),
|
||||
goldmark.WithParserOptions(parser.WithASTTransformers(
|
||||
util.Prioritized(cardLinks{base: repoBase}, 100))),
|
||||
goldmark.WithRendererOptions(
|
||||
renderer.WithNodeRenderers(util.Prioritized(imageAsLink{}, 100))),
|
||||
)
|
||||
var out bytes.Buffer
|
||||
if err := md.Convert([]byte(card), &out); err != nil {
|
||||
return ""
|
||||
}
|
||||
// Safe by construction: goldmark in its default, safe mode, with the two
|
||||
// changes above. Not something a template may escape again.
|
||||
return template.HTML(out.String()) //nolint:gosec
|
||||
}
|
||||
|
||||
// cardLinks resolves a card's relative links against its repository.
|
||||
type cardLinks struct{ base string }
|
||||
|
||||
func (c cardLinks) Transform(doc *ast.Document, _ text.Reader, _ parser.Context) {
|
||||
base, err := url.Parse(c.base)
|
||||
if err != nil || c.base == "" {
|
||||
return
|
||||
}
|
||||
_ = ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||
if !entering {
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
var dest *[]byte
|
||||
switch l := n.(type) {
|
||||
case *ast.Link:
|
||||
dest = &l.Destination
|
||||
case *ast.Image:
|
||||
dest = &l.Destination
|
||||
default:
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
u, err := url.Parse(string(*dest))
|
||||
if err != nil || u.IsAbs() || strings.HasPrefix(string(*dest), "#") {
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
*dest = []byte(base.ResolveReference(u).String())
|
||||
return ast.WalkContinue, nil
|
||||
})
|
||||
}
|
||||
|
||||
// imageAsLink renders an image as a link to it, named by its alt text.
|
||||
type imageAsLink struct{}
|
||||
|
||||
func (imageAsLink) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
||||
reg.Register(ast.KindImage, func(w util.BufWriter, src []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||
if !entering {
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
img := n.(*ast.Image)
|
||||
alt := strings.TrimSpace(string(nodeText(img, src)))
|
||||
if alt == "" {
|
||||
alt = "image"
|
||||
}
|
||||
dest := img.Destination
|
||||
if html.IsDangerousURL(dest) {
|
||||
_, _ = w.WriteString(`<span class="img">[` + template.HTMLEscapeString(alt) + `]</span>`)
|
||||
return ast.WalkSkipChildren, nil
|
||||
}
|
||||
_, _ = w.WriteString(`<a class="img" rel="noopener noreferrer" href="` +
|
||||
template.HTMLEscapeString(string(util.URLEscape(dest, true))) + `">[` +
|
||||
template.HTMLEscapeString(alt) + `]</a>`)
|
||||
return ast.WalkSkipChildren, nil
|
||||
})
|
||||
}
|
||||
|
||||
// nodeText is the plain text beneath a node.
|
||||
func nodeText(n ast.Node, src []byte) []byte {
|
||||
var b bytes.Buffer
|
||||
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
|
||||
if t, ok := c.(*ast.Text); ok {
|
||||
b.Write(t.Segment.Value(src))
|
||||
continue
|
||||
}
|
||||
b.Write(nodeText(c, src))
|
||||
}
|
||||
return b.Bytes()
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAModelCardCannotRunOrLoadAnything(t *testing.T) {
|
||||
card := "# Qwen\n\nA **small** model.\n\n<script>alert(1)</script>\n\n" +
|
||||
"<img src=x onerror=alert(1)>\n\n[click](javascript:alert(1))\n\n" +
|
||||
"\n\n[license](LICENSE)\n\n" +
|
||||
"| a | b |\n|---|---|\n| 1 | 2 |\n"
|
||||
got := string(renderCard(card, "https://huggingface.co/Qwen/X/blob/abc/"))
|
||||
|
||||
for _, bad := range []string{"<script", "onerror", "javascript:", "<img"} {
|
||||
if strings.Contains(got, bad) {
|
||||
t.Errorf("the card rendered %q: %s", bad, got)
|
||||
}
|
||||
}
|
||||
// Shown as a link to the picture, not fetched by the page.
|
||||
if !strings.Contains(got, `href="https://tracker.example/pixel.png">[benchmark]</a>`) {
|
||||
t.Errorf("an image should become a link: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, `href="https://huggingface.co/Qwen/X/blob/abc/LICENSE"`) {
|
||||
t.Errorf("a relative link should point into the repository: %s", got)
|
||||
}
|
||||
if !strings.Contains(got, "<strong>small</strong>") || !strings.Contains(got, "<table>") {
|
||||
t.Errorf("the markdown should render, tables included: %s", got)
|
||||
}
|
||||
}
|
||||
@@ -476,3 +476,15 @@ table.explore .cannot { color: var(--bad); }
|
||||
.tag { display: inline-block; font-size: .7rem; font-weight: 600; color: var(--muted);
|
||||
border: 1px solid var(--line); border-radius: 999px; padding: 0 .4rem; margin-left: .3rem; vertical-align: 1px; }
|
||||
.tag.new { color: var(--accent); border-color: var(--accent); }
|
||||
table.explore .from { font-size: .75rem; color: var(--muted); margin-top: .15rem; overflow-wrap: anywhere; }
|
||||
.repo-meta { display: flex; flex-wrap: wrap; gap: .25rem 1rem; font-size: .85rem; color: var(--muted); margin: .25rem 0 .75rem; }
|
||||
/* A README is written for its own site. Held to a box so the file list, which
|
||||
is what this page is for, stays within reach below it. */
|
||||
details.model-card { border: 1px solid var(--line); border-radius: 8px; background: var(--card); margin: 0 0 1rem; }
|
||||
details.model-card > summary { cursor: pointer; padding: .5rem .8rem; font-weight: 600; font-size: .9rem; }
|
||||
details.model-card .card-body { max-height: 28rem; overflow: auto; padding: 0 1rem 1rem; font-size: .88rem; line-height: 1.55; }
|
||||
.card-body h1 { font-size: 1.25rem; } .card-body h2 { font-size: 1.1rem; } .card-body h3, .card-body h4 { font-size: .95rem; }
|
||||
.card-body pre { overflow-x: auto; padding: .6rem .75rem; background: var(--bg); border-radius: 6px; font-size: .8rem; }
|
||||
.card-body table { width: auto; max-width: 100%; display: block; overflow-x: auto; font-size: .8rem; }
|
||||
.card-body td, .card-body th { padding: .25rem .5rem; }
|
||||
.card-body a.img { font-size: .8rem; }
|
||||
@@ -216,7 +216,8 @@ document.addEventListener('click', function (e) {
|
||||
<th class="num">Downloads</th><th class="num">Likes</th><th class="num">Updated</th></tr>
|
||||
{{range .Rows}}<tr>
|
||||
<td><a href="{{.Href}}"><code>{{.ID}}</code></a>
|
||||
{{if .New}}<span class="tag new">new</span>{{end}}{{if .Gated.Is}}<span class="tag">gated</span>{{end}}</td>
|
||||
{{if .New}}<span class="tag new">new</span>{{end}}{{if .Gated.Is}}<span class="tag">gated</span>{{end}}
|
||||
{{if or .BaseModel .License}}<div class="from">{{with .BaseModel}}from {{.}}{{end}}{{if and .BaseModel .License}} · {{end}}{{.License}}</div>{{end}}</td>
|
||||
<td class="num">{{.ParamsLabel}}</td>
|
||||
<td>{{if .Architecture}}<span{{if not .Verdict.Servable}} class="cannot"{{end}} title="{{.Verdict.Reason}}">{{.Architecture}}{{if .Verdict.Checked}}{{if .Verdict.Servable}} ✓{{else}} ✗{{end}}{{end}}</span>{{else}}—{{end}}</td>
|
||||
<td class="num">{{.ContextLabel}}</td>
|
||||
@@ -239,6 +240,18 @@ directory, or give the mirror a token.</p>
|
||||
{{if .Repo}}
|
||||
<p><a href="{{.Back}}">← back to the list</a></p>
|
||||
<h3>{{.Repo.ID}} <span class="opt">at {{.Repo.ShortRevision}}</span></h3>
|
||||
<p class="repo-meta">
|
||||
{{with .Repo.BaseModel}}<span>from {{range $i, $b := .}}{{if $i}}, {{end}}<code>{{$b}}</code>{{end}}</span>{{end}}
|
||||
{{with .Repo.License}}<span>license {{.}}</span>{{end}}
|
||||
{{with .Repo.Languages}}<span>{{join .}}</span>{{end}}
|
||||
<span><a href="{{.RepoURL}}" rel="noopener noreferrer">on Hugging Face ↗</a></span>
|
||||
</p>
|
||||
{{if .Card}}
|
||||
<details class="model-card" open>
|
||||
<summary>Model card</summary>
|
||||
<div class="card-body">{{.Card}}</div>
|
||||
</details>
|
||||
{{end}}
|
||||
{{if .RepoSets}}
|
||||
<table>
|
||||
<tr><th>What</th><th>Size</th><th>Published checksum</th><th></th></tr>
|
||||
|
||||
Reference in new issue
Block a user