huggingface

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package huggingface is a read-only client for the Hugging Face Hub HTTP API: it searches the Hub for models, lists the files in a model repository, and reads a model's card metadata, all over the same hardened, public-only transport the file downloader uses. It is the upstream half of turning a hub reference into a verified catalog entry, and the discovery surface (search, then list and view a candidate) that finds the reference in the first place.

It reads metadata only. It never downloads weights or executes anything: a file's bytes are fetched and verified separately through the download path. The value it adds is trust-bearing structure: for a large weights file the Hub records an LFS object id that is the file's sha256, so a manifest listed here already carries the content digest a download can be pinned to, captured from the registry rather than typed by hand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client reads model metadata from the Hugging Face Hub over a hardened transport.

func New

func New(opts ...Option) *Client

New builds a Client. With nothing injected it talks to the public Hub over the public-only transport, which refuses any private, loopback, or metadata address.

func (*Client) FileURL

func (c *Client) FileURL(repo, path string) string

FileURL is the direct https location a file is downloaded and verified from. It resolves the main revision, the same revision Tree lists, so a digest from Tree pins the bytes this URL returns.

func (*Client) Info

func (c *Client) Info(ctx context.Context, repo string) (Info, error)

Info reads a model's card metadata. repo is "owner/name".

func (*Client) Search

func (c *Client) Search(ctx context.Context, q SearchQuery) ([]SearchResult, error)

Search lists models on the Hub ranked by the query's sort order, returning each candidate's id and the signals needed to judge it. It reads metadata only, over the same hardened transport the rest of the client uses, and never fetches a tree or weights: a returned id is what bless and run consume next.

func (*Client) Tree

func (c *Client) Tree(ctx context.Context, repo string) ([]File, error)

Tree lists the files at the main revision of a model repository, with the content digest already attached to every LFS-tracked file. repo is "owner/name". A missing repo is reported as a terminal not-found rather than an empty list, so a typo never looks like an empty model.

type File

type File struct {
	// Path is the file's path within the repository, for example "model.safetensors"
	// or "tokenizer.json".
	Path string
	// Size is the file's size in bytes.
	Size int64
	// SHA256 is the content hash, present only for an LFS-tracked file (the large
	// weights), taken from the Hub's recorded LFS object id. It is empty for a small
	// git-tracked file, whose Hub object id is a git blob hash, not a content sha256;
	// such a file's digest is established by hashing it on download instead.
	SHA256 string
	// LFS reports whether the file is stored in LFS (the large binary objects). An LFS
	// file carries a usable SHA256; a non-LFS file does not.
	LFS bool
}

File is one file in a model repository's tree.

type Info

type Info struct {
	// ID is the canonical "owner/name" identifier the Hub returns.
	ID string
	// Author is the publishing namespace.
	Author string
	// License is the SPDX-style license id from the model card, when declared.
	License string
	// Tags are the model-card tags, which carry signals like the pipeline type and the
	// base library.
	Tags []string
	// Gated reports whether the repo requires accepting terms before download, so a
	// caller can warn that an automated fetch will not succeed unattended.
	Gated bool
}

Info is the subset of a model's card metadata used to describe a catalog entry.

type Option

type Option func(*Client)

Option configures a Client.

func WithBaseURL

func WithBaseURL(base string) Option

WithBaseURL overrides the Hub origin, for tests only.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient injects the HTTP client, so a test can supply one that reaches a local server. Production uses the default anti-SSRF, https-only transport.

type SearchQuery

type SearchQuery struct {
	// Text matches free-form against a repo id and its card. Empty matches everything.
	Text string
	// Filters are model-card tags that are all required (ANDed), for example
	// "text-generation" or "safetensors". A result carries every filter it is matched on.
	Filters []string
	// Author restricts results to a single publishing namespace, for example "Qwen".
	Author string
	// Sort orders the results: "downloads" (default), "likes", or "modified". An
	// unknown value falls back to downloads so a typo never returns an unordered page.
	Sort string
	// Limit caps the number of results. It is clamped to a sane range so a caller can
	// neither ask for zero nor pull an unbounded page.
	Limit int
}

SearchQuery selects and orders models on the Hub. The zero value is a valid query that returns the most-downloaded models; every field narrows or reorders it.

type SearchResult

type SearchResult struct {
	// ID is the canonical "owner/name" identifier, the reference bless and run accept.
	ID string
	// Downloads is the last-30-day download count, the Hub's popularity signal.
	Downloads int64
	// Likes is the number of users who have starred the repo.
	Likes int64
	// Pipeline is the model-card pipeline tag, for example "text-generation".
	Pipeline string
	// Library is the declared base library, for example "transformers" or "gguf".
	Library string
	// Tags are the model-card tags, which carry the weight-format signal (a repo with
	// safetensors weights is tagged "safetensors", a GGUF repo "gguf").
	Tags []string
}

SearchResult is one model returned by a Hub search: enough to judge a candidate and hand its id straight to bless, without yet fetching its tree or card.

func (SearchResult) SafeFormat

func (r SearchResult) SafeFormat() bool

SafeFormat reports whether a search result advertises a weight format a runtime can load without executing repository code: safetensors or GGUF. A repo without either (for example a PyTorch-pickle-only repo) is flagged so a caller does not chase a model that bless would refuse.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL