Documentation
¶
Overview ¶
Package download fetches speech.cpp's shared library and its models.
Library installs the libspeech release archive for a target (OS, architecture, backend) after checking it against the release's SHA256SUMS and, for the pinned release, against the module's own copy of that file. Model fetches a catalog model's weight file, checks its size and SHA-256, reports progress, and resumes an interrupted fetch.
Only the targets speech.cpp publishes archives for are supported: linux/amd64 (cpu, vulkan), darwin/arm64 (metal) and windows/amd64 (vulkan). Any other target returns ErrUnsupportedTarget; a library built by the caller can still be opened by path.
Index ¶
- Constants
- Variables
- func ArchiveName(version string, t Target) (string, error)
- func DefaultUserAgent() string
- func LibraryName(goos string) (string, error)
- func Model(ctx context.Context, name, weightType, dir string) (string, error)
- func ModelPath(dir, name, weightType string) (string, error)
- func VerifyFile(path string, f catalog.File) error
- type Backend
- type Client
- type Install
- type ProgressFunc
- type Target
Constants ¶
const ( // DialTimeout bounds opening a connection. DialTimeout = 30 * time.Second // TLSHandshakeTimeout bounds the TLS handshake. TLSHandshakeTimeout = 10 * time.Second // ResponseHeaderTimeout bounds the wait for a response's headers once the // request is sent. ResponseHeaderTimeout = 60 * time.Second // StallTimeout ends a download, with ErrStalled, when its body brings no // byte for this long. StallTimeout = 60 * time.Second )
Timeouts of the default HTTP client. None of them bounds a download's total time, so a large model on a slow link is not cut off; a context deadline does that.
const PartialSuffix = ".partial"
PartialSuffix is appended to a model file's name while it downloads.
const PinnedVersion = "0.11.3"
PinnedVersion is the speech.cpp release this module is built against.
const RecordName = "speech-install.json"
RecordName is the file an install writes beside the library it extracted.
Variables ¶
var ( // ErrUnsupportedTarget means speech.cpp publishes no archive for the target. ErrUnsupportedTarget = errors.New("no published libspeech archive for this target") // ErrInvalidVersion means a version is not MAJOR.MINOR.PATCH. ErrInvalidVersion = errors.New("invalid speech.cpp version") // ErrIntegrity means bytes do not match their published size or SHA-256, // or the release's SHA256SUMS disagrees with the module's pinned copy. ErrIntegrity = errors.New("integrity check failed") // ErrNoChecksum means the release's SHA256SUMS does not list the archive. ErrNoChecksum = errors.New("no SHA-256 for the archive") // ErrUnsafeArchive means an archive entry escapes the destination, is too // large, or is a link or file type that is not allowed. ErrUnsafeArchive = errors.New("unsafe archive") // ErrBadArchive means the archive does not hold the shared library. ErrBadArchive = errors.New("archive does not hold libspeech") // ErrHTTPStatus means a server answered with an unexpected status. ErrHTTPStatus = errors.New("unexpected HTTP status") // ErrNotInstallDir means the destination holds files that are not a // libspeech install, so it is not replaced. ErrNotInstallDir = errors.New("destination is not a libspeech install directory") // ErrNotInstalled means a directory holds no complete libspeech install. ErrNotInstalled = errors.New("libspeech is not installed") // ErrInvalidFile means a model file description cannot be fetched safely. ErrInvalidFile = errors.New("invalid model file description") // ErrStalled means a download with the default HTTP client received no // byte for StallTimeout. A model's partial file is kept for the next call. ErrStalled = errors.New("download stalled") )
Set of errors the package returns; test them with errors.Is.
Functions ¶
func ArchiveName ¶
ArchiveName returns the release archive's file name for the target, e.g. "speech-0.11.3-macos-arm64-metal.zip"; an empty version means PinnedVersion.
func DefaultUserAgent ¶
func DefaultUserAgent() string
DefaultUserAgent returns "speech-cpp-go/<module version>", or "speech-cpp-go/devel" when the version is not known.
func LibraryName ¶
LibraryName returns the file name of the shared library to open on goos, for libspeech's API major 4: libspeech.4.dylib, libspeech.so.4 or speech.dll. An install records the name it holds; see Install.Library.
func Model ¶
Model downloads the catalog model name's file of weightType (the model's default type when empty) into dir and returns its path. See Client.File.
Types ¶
type Client ¶
type Client struct {
// HTTPClient sends the requests. nil means a client of this package with
// DialTimeout, TLSHandshakeTimeout and ResponseHeaderTimeout, and a body
// that brings no byte for StallTimeout ends the download with ErrStalled;
// none of them limits a download's total time. A client set here is used
// as it is, with none of these: its own timeouts and the context apply.
HTTPClient *http.Client
// UserAgent is sent with every request; empty means DefaultUserAgent().
UserAgent string
// Progress, if set, is called as a download advances, at least every MiB
// and once when it completes.
Progress ProgressFunc
}
Client downloads libraries and models. The zero value is ready to use.
func (*Client) File ¶
File downloads f into dir as dir/<f.Name> and returns that path.
A file already there with f's size and SHA-256 is kept; one that differs is replaced. The download is written to dir/<f.Name>.partial and resumed from there with an HTTP Range request; a server that ignores the range, refuses it, or answers another range, and a partial file larger than f, restart the download from the start. A cancelled or interrupted download keeps the partial file for the next call. Once the partial file has f's size, its bytes on disk are hashed before it is renamed into place: when they do not match f's SHA-256 the partial file is removed and ErrIntegrity returned.
type Install ¶
type Install struct {
// Dir is the install directory; it is not stored in the record.
Dir string `json:"-"`
Version string `json:"version"` // speech.cpp release
Target Target `json:"target"`
Archive string `json:"archive"`
ArchiveSHA256 string `json:"archive_sha256"`
Library string `json:"library"` // file name of the library to open
Files map[string]string `json:"files"` // extracted file → SHA-256
Links map[string]string `json:"links,omitzero"`
Installed time.Time `json:"installed,omitzero"`
}
Install describes a libspeech install, as recorded in its directory.
func Installed ¶
Installed reads the install record in dir and checks that the library file it names is present. A directory with no record, or whose library is missing, is ErrNotInstalled. It is cheap: it hashes nothing, so a file changed since the install goes unnoticed; Install.Verify checks that.
func Library ¶
Library downloads the release archive of version (PinnedVersion when empty) for target t, checks it, and installs its shared library files into dir.
dir belongs to the install: it must not exist, be empty, or hold an earlier install (it has a RecordName file); anything else is ErrNotInstallDir. The archive is checked against the release's SHA256SUMS (and, for PinnedVersion, the module's pinned copy) before anything is extracted. Extraction goes to a new directory beside dir, which then replaces dir; a failure at any step leaves dir as it was. Two installs into the same dir at once are not supported.
func (Install) LibraryPath ¶
LibraryPath returns the path of the library to open.
type ProgressFunc ¶
type ProgressFunc func(done, total int64)
ProgressFunc receives the bytes done and the total of one download; total is -1 when the server does not say.
type Target ¶
type Target struct {
OS string `json:"os"`
Arch string `json:"arch"`
Backend Backend `json:"backend"`
}
Target is a platform a libspeech build runs on. OS and Arch use Go's names (runtime.GOOS, runtime.GOARCH).
func HostTarget ¶
HostTarget returns the running platform with backend.
func Targets ¶
func Targets() []Target
Targets returns the targets speech.cpp publishes archives for.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package catalog is speech.cpp's model catalog for the pinned release, embedded in the module: each model's name, task, languages and weight files, and per file its Hugging Face URL pinned to a revision, size and SHA-256.
|
Package catalog is speech.cpp's model catalog for the pinned release, embedded in the module: each model's name, task, languages and weight files, and per file its Hugging Face URL pinned to a revision, size and SHA-256. |