File size: 3,564 Bytes
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { HF_RELS, REL_NAME, type Lineage, type Rel } from "@/lib/types"
import { num as fmt, full } from "@/lib/format"
import { RelDot, relColor } from "@/components/rel"

/**
 * Community models by type next to what Hugging Face's model tree shows. HF has only four types,
 * so those come first with their own subtotal; the types HF has no place for follow, then the
 * total. Types with nothing in them are left out.
 */
export function RelationTable({ L, order }: { L: Lineage; order: Rel[] }) {
  const b = L.by_relation
  const hfRels = HF_RELS.filter((r) => b[r].n > 0 || (L.hf_tree[r] ?? 0) > 0)
  const extra = order.filter((r) => !HF_RELS.includes(r) && b[r].n > 0)
  const max = Math.max(...order.map((r) => b[r].n))
  const sum = (rs: Rel[], k: "n" | "dl30" | "dlall") => rs.reduce((a, r) => a + b[r][k], 0)
  const hfSum = HF_RELS.reduce((a, r) => a + (L.hf_tree[r] ?? 0), 0)

  const row = (r: Rel) => (
    <TableRow key={r} className="border-border/60 hover:bg-transparent">
      <TableCell className="py-3.5">
        <span className="inline-flex items-center gap-2.5"><RelDot rel={r} />{REL_NAME[r]}</span>
      </TableCell>
      <TableCell className="py-3.5">
        <div className="flex items-center gap-3">
          <span className="w-14 text-right font-display font-semibold tabular-nums">{full(b[r].n)}</span>
          <span className="hidden h-2 rounded-full sm:block" style={{ width: `${(b[r].n / max) * 160}px`, background: relColor(r) }} />
        </div>
      </TableCell>
      <TableCell className="py-3.5 text-right tabular-nums">{HF_RELS.includes(r) ? full(L.hf_tree[r] ?? 0) : "—"}</TableCell>
      <TableCell className="py-3.5 text-right tabular-nums">{fmt(b[r].dl30)}</TableCell>
      <TableCell className="py-3.5 text-right tabular-nums">{fmt(b[r].dlall)}</TableCell>
    </TableRow>
  )
  const total = (label: string, n: number, hf: number | null, d30: number, da: number) => (
    <TableRow className="border-border/60 font-semibold hover:bg-transparent">
      <TableCell className="py-3.5">{label}</TableCell>
      <TableCell className="py-3.5"><span className="inline-block w-14 text-right font-display tabular-nums">{full(n)}</span></TableCell>
      <TableCell className="py-3.5 text-right tabular-nums">{hf == null ? "—" : full(hf)}</TableCell>
      <TableCell className="py-3.5 text-right tabular-nums">{fmt(d30)}</TableCell>
      <TableCell className="py-3.5 text-right tabular-nums">{fmt(da)}</TableCell>
    </TableRow>
  )

  return (
    <Table className="text-[15px]">
      <TableHeader>
        <TableRow className="border-border/60 hover:bg-transparent">
          <TableHead className="text-[15px] text-muted-foreground">Type</TableHead>
          <TableHead className="text-[15px] text-muted-foreground">Models</TableHead>
          <TableHead className="text-right text-[15px] text-muted-foreground">On HF model tree</TableHead>
          <TableHead className="text-right text-[15px] text-muted-foreground">Downloads, 30 days</TableHead>
          <TableHead className="text-right text-[15px] text-muted-foreground">All-time</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {hfRels.map(row)}
        {extra.length > 0 && total("Subtotal", sum(HF_RELS, "n"), hfSum, sum(HF_RELS, "dl30"), sum(HF_RELS, "dlall"))}
        {extra.map(row)}
        {total("Total", L.n_models, extra.length ? null : hfSum, L.dl30, L.dlall)}
      </TableBody>
    </Table>
  )
}