engine

package
v0.16.521 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package engine is the speech recognition the runtime brings with it: a pinned whisper.cpp server run as a child of the runtime, and a Unix socket speaking the protocol internal/voice already knows, so that a machine with nothing set up dictates out of the box. The model is fetched once, on the first dictation, checked against its SHA-256 and kept in the runtime's data folder; nothing is sent anywhere but the download requests, and neither audio nor text is written to disk or logged.

Index

Constants

View Source
const (
	// ModelBase is where the model files are; the file name is appended.
	ModelBase = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/"
	// BinaryBase is where the prebuilt server binaries are (the whisper-server workflow of this repository publishes
	// whisper-server-<os>-<arch> and whisper-server.sha256 under the tag); the file name is appended.
	BinaryBase = "https://github.com/ly-solutions/harnessly/releases/download/whisper-server-" + WhisperVersion + "-r1/"
	// BinarySums names the checksum list published beside the binaries.
	BinarySums = "whisper-server.sha256"
)
View Source
const WhisperVersion = "v1.9.5"

WhisperVersion is the whisper.cpp release the server binary is built from (MIT, github.com/ggml-org/whisper.cpp).

Variables

View Source
var ErrChecksum = errors.New("checksum mismatch")

ErrChecksum is a download that is not the file that was pinned.

View Source
var Models = map[string]Model{
	"tiny":   {"tiny", "ggml-tiny-q5_1.bin", 32152673, "818710568da3ca15689e31a743197b520007872ff9576237bda97bd1b469c3d7"},
	"base":   {"base", "ggml-base-q5_1.bin", 59707625, "422f1ae452ade6f30a004d7e5c6a43195e4433bc370bf23fac9cc591f01a8898"},
	"small":  {"small", "ggml-small-q5_1.bin", 190085487, "ae85e4a935d7a567bd102fe55afc16bb595bdb618e11b2fc7591bc08120411bb"},
	"medium": {"medium", "ggml-medium-q5_0.bin", 539212467, "19fea4b380c3a618ec4723c3eef2eb785ffba0d0538cf43f8f235e7b3b34220f"},
	"large-v3-turbo": {"large-v3-turbo", "ggml-large-v3-turbo-q5_0.bin", 574041195,
		"394221709cd5ad1f40c46e6031ca61bce88931e6e088c188294c6d5a55ffa7e2"},
}

Models are the sizes the engine knows, quantized to 5 bits: "tiny" is the fastest and the least exact, "base" is what a 2-vCPU machine answers a 30-second recording with in a few seconds, "small" is clearly better in Russian and takes about three times as long. "medium" and "large-v3-turbo" are for a machine with many processors (or patience) and half a gigabyte of disk and memory to spare: the turbo is the more exact of the two and about as fast.

Functions

func MaskKey added in v0.16.16

func MaskKey(key string) string

MaskKey is how a key is shown back: its last four characters, the rest dots; "" for no key.

func SameOrigin added in v0.16.16

func SameOrigin(a, b string) bool

SameOrigin says two server addresses are the same origin: scheme, host name (any case) and port, the scheme's default port when none is written. A saved key goes only to the origin it was saved for.

Types

type Config

type Config struct {
	// Dir is the engine's folder (the runtime's data folder, voice/): bin/ and models/ are in it.
	Dir string
	// Bin is a whisper-server of the operator's own; ModelFile a ggml model of their own (no pin, no download).
	Bin, ModelFile string
	// Model is "tiny", "base" or "small"; "" picks by the processor count.
	Model string
	// Threads is what the recognition may use; 0 is the processor count up to four.
	Threads int
	// NoDownload keeps the engine from touching the network: it uses only the files that are already there.
	NoDownload bool
	// Idle is how long the server stays loaded after the last recognition (5 minutes).
	Idle time.Duration

	Client                *http.Client
	ModelBase, BinaryBase string
	GOOS, GOARCH          string
	CPUFlags              func() string // the processor's feature flags; /proc/cpuinfo's by default
	LookPath              func(string) (string, error)
	Log                   func(level, msg string, kv ...any)
}

Config is what the deployment names; every field has a default.

type Engine

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

Engine is the recognition of one runtime.

func New

func New(cfg Config) *Engine

New is an engine; nothing is started or fetched until Prepare.

func (*Engine) BinaryURL added in v0.16.16

func (e *Engine) BinaryURL() string

BinaryURL is where the server binary of this machine is downloaded from.

func (*Engine) Close

func (e *Engine) Close()

Close stops the server.

func (*Engine) Eligible

func (e *Engine) Eligible() error

Eligible is nil when this machine can recognize with the engine (maybe after a download), else why not.

func (*Engine) Infer

func (e *Engine) Infer(ctx context.Context, pcm []byte, lang string) (string, error)

Infer recognizes PCM16 mono 16 kHz audio; lang is "ru", "en" or "auto". The server is started when it is not running and stopped after Idle without a request. Cancelling ctx stops the pass.

func (*Engine) Listen

func (e *Engine) Listen(path string) (net.Listener, error)

Listen serves the dictation socket at path (a folder of mode 0700 holds it, the socket is the owner's alone) until the listener is closed.

func (*Engine) ModelURL added in v0.16.16

func (e *Engine) ModelURL(m Model) string

ModelURL is where a model is downloaded from.

func (*Engine) Needs added in v0.16.16

func (e *Engine) Needs() Needs

Needs says what the engine would fetch at the next dictation; nothing for an engine that sends to a server of the person's own. The model is judged by its size only (the checksum is checked when it is used).

func (*Engine) Prepare

func (e *Engine) Prepare()

Prepare starts getting the binary and the model unless that is done, under way, or failed a moment ago. It returns at once; Status says how it goes and Wait blocks for it.

func (*Engine) Ready

func (e *Engine) Ready() bool

Ready says the binary and the model are in place and checked.

func (*Engine) SetModel added in v0.16.15

func (e *Engine) SetModel(name string) bool

SetModel changes the model ("tiny", "base", "small"; "" picks by the processor count). The next dictation gets it: a ready engine goes back to idle and its server is stopped, so the next one prepares (downloads) the new model. A preparation under way finishes with the model it started with. False when nothing changed.

func (*Engine) SetRemote added in v0.16.16

func (e *Engine) SetRemote(r *Remote)

SetRemote makes the engine send recordings to r (nil: recognize here again). A running whisper-server is stopped.

func (*Engine) Status

func (e *Engine) Status() Status

Status is the preparation's state now; an engine that sends to a server of the person's own is always ready.

func (*Engine) UsesRemote added in v0.16.16

func (e *Engine) UsesRemote() bool

UsesRemote says the recordings go to a server of the person's own.

func (*Engine) Wait

func (e *Engine) Wait(ctx context.Context) error

Wait blocks until the preparation that Prepare started ends, and says how.

type Model

type Model struct {
	ID     string
	File   string
	Size   int64
	SHA256 string
}

Model is one multilingual ggml model (MIT, Whisper by OpenAI, converted by the whisper.cpp project).

type Needs added in v0.16.16

type Needs struct {
	// Binary: no whisper-server is on the machine yet.
	Binary bool
	// Model is the model file that is not there yet (nil when it is, or a file of the operator's own is used).
	Model *Model
	// ModelPath is where that model goes.
	ModelPath string
	// Offline: downloads are off (RUNTIME_VOICE_DOWNLOAD=off).
	Offline bool
}

Needs is what a preparation of the built-in recognition would still download.

type Remote added in v0.16.16

type Remote struct {
	URL   string `json:"url"`
	Model string `json:"model"`
	Key   string `json:"key,omitempty"`
}

Remote is a recognition server of the person's own that speaks OpenAI's speech-to-text API (POST <base>/audio/transcriptions, multipart: file, model, language): OpenAI itself, a self-hosted faster-whisper-server, speaches, LocalAI, Groq and the like. With one set the engine downloads and runs nothing: every recording is sent there whole, once the person stops (no partial texts, so a paid service is asked once per recording). The key stays in the runtime: it goes only into the request's Authorization header.

func (Remote) Check added in v0.16.16

func (r Remote) Check() error

Check is nil when the server can be asked: an http(s) address with a host and no password in it, a model name and a key of printable characters.

func (Remote) Endpoint added in v0.16.16

func (r Remote) Endpoint() string

Endpoint is the address a recording is posted to: the URL as given when it already ends in /audio/transcriptions, else that path appended (https://api.openai.com/v1 → https://api.openai.com/v1/audio/transcriptions).

type Status

type Status struct {
	State   string
	Stage   string
	Percent int
	Error   string
}

Status is where the preparation stands: "idle" (nothing asked yet), "preparing" (Stage is "binary" or "model" and Percent how far), "ready" and "failed" (Error says why).

Jump to

Keyboard shortcuts

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