mirror of
https://github.com/mudler/LocalAI.git
synced 2026-04-30 20:15:35 -04:00
* feat(face-recognition): add insightface backend for 1:1 verify, 1:N identify, embedding, detection, analysis
Adds face recognition as a new first-class capability in LocalAI via the
`insightface` Python backend, with a pluggable two-engine design so
non-commercial (insightface model packs) and commercial-safe
(OpenCV Zoo YuNet + SFace) models share the same gRPC/HTTP surface.
New gRPC RPCs (backend/backend.proto):
* FaceVerify(FaceVerifyRequest) returns FaceVerifyResponse
* FaceAnalyze(FaceAnalyzeRequest) returns FaceAnalyzeResponse
Existing Embedding and Detect RPCs are reused (face image in
PredictOptions.Images / DetectOptions.src) for face embedding and
face detection respectively.
New HTTP endpoints under /v1/face/:
* verify — 1:1 image pair same-person decision
* analyze — per-face age + gender (emotion/race reserved)
* register — 1:N enrollment; stores embedding in vector store
* identify — 1:N recognition; detect → embed → StoresFind
* forget — remove a registered face by opaque ID
Service layer (core/services/facerecognition/) introduces a
`Registry` interface with one in-memory `storeRegistry` impl backed
by LocalAI's existing local-store gRPC vector backend. HTTP handlers
depend on the interface, not on StoresSet/StoresFind directly, so a
persistent PostgreSQL/pgvector implementation can be slotted in via a
single constructor change in core/application (TODO marker in the
package doc).
New usecase flag FLAG_FACE_RECOGNITION; insightface is also wired
into FLAG_DETECTION so /v1/detection works for face bounding boxes.
Gallery (backend/index.yaml) ships three entries:
* insightface-buffalo-l — SCRFD-10GF + ArcFace R50 + genderage
(~326MB pre-baked; non-commercial research use only)
* insightface-opencv — YuNet + SFace (~40MB pre-baked; Apache 2.0)
* insightface-buffalo-s — SCRFD-500MF + MBF (runtime download; non-commercial)
Python backend (backend/python/insightface/):
* engines.py — FaceEngine protocol with InsightFaceEngine and
OnnxDirectEngine; resolves model paths relative to the backend
directory so the same gallery config works in docker-scratch and
in the e2e-backends rootfs-extraction harness.
* backend.py — gRPC servicer implementing Health, LoadModel, Status,
Embedding, Detect, FaceVerify, FaceAnalyze.
* install.sh — pre-bakes buffalo_l + OpenCV YuNet/SFace inside the
backend directory so first-run is offline-clean (the final scratch
image only preserves files under /<backend>/).
* test.py — parametrized unit tests over both engines.
Tests:
* Registry unit tests (go test -race ./core/services/facerecognition/...)
— in-memory fake grpc.Backend, table-driven, covers register/
identify/forget/error paths + concurrent access.
* tests/e2e-backends/backend_test.go extended with face caps
(face_detect, face_embed, face_verify, face_analyze); relative
ordering + configurable verifyCeiling per engine.
* Makefile targets: test-extra-backend-insightface-buffalo-l,
-opencv, and the -all aggregate.
* CI: .github/workflows/test-extra.yml gains tests-insightface-grpc,
auto-triggered by changes under backend/python/insightface/.
Docs:
* docs/content/features/face-recognition.md — feature page with
license table, quickstart (defaults to the commercial-safe model),
models matrix, API reference, 1:N workflow, storage caveats.
* Cross-refs in object-detection.md, stores.md, embeddings.md, and
whats-new.md.
* Contributor README at backend/python/insightface/README.md.
Verified end-to-end:
* buffalo_l: 6/6 specs (health, load, face_detect, face_embed,
face_verify, face_analyze).
* opencv: 5/5 specs (same minus face_analyze — SFace has no
demographic head; correctly skipped via BACKEND_TEST_CAPS).
Assisted-by: Claude:claude-opus-4-7
* fix(face-recognition): move engine selection to model gallery, collapse backend entries
The previous commit put engine/model_pack options on backend gallery
entries (`backend/index.yaml`). That was wrong — `GalleryBackend`
(core/gallery/backend_types.go:32) has no `options` field, so the
YAML decoder silently dropped those keys and all three "different
insightface-*" backend entries resolved to the same container image
with no distinguishing configuration.
Correct split:
* `backend/index.yaml` now has ONE `insightface` backend entry
shipping the CPU + CUDA 12 container images. The Python backend
bundles both the non-commercial insightface model packs
(buffalo_l / buffalo_s) and the commercial-safe OpenCV Zoo
weights (YuNet + SFace); the active engine is selected at
LoadModel time via `options: ["engine:..."]`.
* `gallery/index.yaml` gains three model entries —
`insightface-buffalo-l`, `insightface-opencv`,
`insightface-buffalo-s` — each setting the appropriate
`overrides.backend` + `overrides.options` so installing one
actually gives the user the intended engine. This matches how
`rfdetr-base` lives in the model gallery against the `rfdetr`
backend.
The earlier e2e tests passed despite this bug because the Makefile
targets pass `BACKEND_TEST_OPTIONS` directly to LoadModel via gRPC,
bypassing any gallery resolution entirely. No code changes needed.
Assisted-by: Claude:claude-opus-4-7
* feat(face-recognition): cover all supported models in the gallery + drop weight baking
Follows up on the model-gallery split: adds entries for every model
configuration either engine actually supports, and switches weight
delivery from image-baked to LocalAI's standard gallery mechanism.
Gallery now has seven `insightface-*` model entries (gallery/index.yaml):
insightface (family) — non-commercial research use
• buffalo-l (326MB) — SCRFD-10GF + ResNet50 + genderage, default
• buffalo-m (313MB) — SCRFD-2.5GF + ResNet50 + genderage
• buffalo-s (159MB) — SCRFD-500MF + MBF + genderage
• buffalo-sc (16MB) — SCRFD-500MF + MBF, recognition only
(no landmarks, no demographics — analyze
returns empty attributes)
• antelopev2 (407MB) — SCRFD-10GF + ResNet100@Glint360K + genderage
OpenCV Zoo family — Apache 2.0 commercial-safe
• opencv — YuNet + SFace fp32 (~40MB)
• opencv-int8 — YuNet + SFace int8 (~12MB, ~3x smaller, faster on CPU)
Model weights are no longer baked into the backend image. The image
now ships only the Python runtime + libraries (~275MB content size,
~1.18GB disk vs ~1.21GB when weights were baked). Weights flow through
LocalAI's gallery mechanism:
* OpenCV variants list `files:` with ONNX URIs + SHA-256, so
`local-ai models install insightface-opencv` pulls them into the
models directory exactly like any other gallery-managed model.
* insightface packs (upstream distributes .zip archives only, not
individual ONNX files) auto-download on first LoadModel via
FaceAnalysis' built-in machinery, rooted at the LocalAI models
directory so they live alongside everything else — same pattern
`rfdetr` uses with `inference.get_model()`.
Backend changes (backend/python/insightface/):
* backend.py — LoadModel propagates `ModelOptions.ModelPath` (the
LocalAI models directory) to engines via a `_model_dir` hint.
This replaces the earlier ModelFile-dirname approach; ModelPath
is the canonical "models directory" variable set by the Go loader
(pkg/model/initializers.go:144) and is always populated.
* engines.py::_resolve_model_path — picks up `model_dir` and searches
it (plus basename-in-model-dir) before falling back to the dev
script-dir. This is how OnnxDirectEngine finds gallery-downloaded
YuNet/SFace files by filename only.
* engines.py::_flatten_insightface_pack — new helper that works
around an upstream packaging inconsistency: buffalo_l/s/sc zips
expand flat, but buffalo_m and antelopev2 zips wrap their ONNX
files in a redundant `<name>/` directory. insightface's own
loader looks one level too shallow and fails. We call
`ensure_available()` explicitly, flatten if nested, then hand to
FaceAnalysis.
* engines.py::InsightFaceEngine.prepare — root-resolution order now
includes the `_model_dir` hint so packs download into the LocalAI
models directory by default.
* install.sh — no longer pre-downloads any weights. Everything is
gallery-managed now.
* smoke.py (new) — parametrized smoke test that iterates over every
gallery configuration, simulating the LocalAI install flow
(creates a models dir, fetches OpenCV files with checksum
verification, lets insightface auto-download its packs), then
runs detect + embed + verify (+ analyze where supported) through
the in-process BackendServicer.
* test.py — OnnxDirectEngineTest no longer hardcodes `/models/opencv/`
paths; downloads ONNX files to a temp dir at setUpClass time and
passes ModelPath accordingly.
Registry change (core/services/facerecognition/store_registry.go):
* `dim=0` in NewStoreRegistry now means "accept whatever dimension
arrives" — needed because the backend supports 512-d ArcFace/MBF
and 128-d SFace via the same Registry. A non-zero dim still fails
fast with ErrDimensionMismatch.
* core/application plumbs `faceEmbeddingDim = 0`, explaining the
rationale in the comment.
Backend gallery description updated to reflect that the image carries
no weights — it's just Python + engines.
Smoke-tested all 7 configurations against the rebuilt image (with the
flatten fix applied), exit 0:
PASS: insightface-buffalo-l faces=6 dim=512 same-dist=0.000
PASS: insightface-buffalo-sc faces=6 dim=512 same-dist=0.000
PASS: insightface-buffalo-s faces=6 dim=512 same-dist=0.000
PASS: insightface-buffalo-m faces=6 dim=512 same-dist=0.000
PASS: insightface-antelopev2 faces=6 dim=512 same-dist=0.000
PASS: insightface-opencv faces=6 dim=128 same-dist=0.000
PASS: insightface-opencv-int8 faces=6 dim=128 same-dist=0.000
7/7 passed
Assisted-by: Claude:claude-opus-4-7
* fix(face-recognition): pre-fetch OpenCV ONNX for e2e target; drop stale pre-baked claim
CI regression from the previous commit: I moved OpenCV Zoo weight
delivery to LocalAI's gallery `files:` mechanism, but the
test-extra-backend-insightface-opencv target was still passing
relative paths `detector_onnx:models/opencv/yunet.onnx` in
BACKEND_TEST_OPTIONS. The e2e suite drives LoadModel directly over
gRPC without going through the gallery, so those relative paths
resolved to nothing and OpenCV's ONNXImporter failed:
LoadModel failed: Failed to load face engine:
OpenCV(4.13.0) ... Can't read ONNX file: models/opencv/yunet.onnx
Fix: add an `insightface-opencv-models` prerequisite target that
fetches the two ONNX files (YuNet + SFace) to a deterministic host
cache at /tmp/localai-insightface-opencv-cache/, verifies SHA-256,
and skips the download on re-runs. The opencv test target depends on
it and passes absolute paths in BACKEND_TEST_OPTIONS, so the backend
finds the files via its normal absolute-path resolution branch.
Also refresh the buffalo_l comment: it no longer says "pre-baked"
(nothing is — the pack auto-downloads from upstream's GitHub release
on first LoadModel, same as in CI).
Locally verified: `make test-extra-backend-insightface-opencv` passes
5/5 specs (health, load, face_detect, face_embed, face_verify).
Assisted-by: Claude:claude-opus-4-7
* feat(face-recognition): add POST /v1/face/embed + correct /v1/embeddings docs
The docs promised that /v1/embeddings returns face vectors when you
send an image data-URI. That was never true: /v1/embeddings is
OpenAI-compatible and text-only by contract — its handler goes
through `core/backend/embeddings.go::ModelEmbedding`, which sets
`predictOptions.Embeddings = s` (a string of TEXT to embed) and never
populates `predictOptions.Images[]`. The Python backend's Embedding
gRPC method does handle Images[] (that's how /v1/face/register reaches
it internally via `backend.FaceEmbed`), but the HTTP embeddings
endpoint wasn't wired to populate it.
Rather than overload /v1/embeddings with image-vs-text detection —
messy, and the endpoint is OpenAI-compatible by design — add a
dedicated /v1/face/embed endpoint that wraps `backend.FaceEmbed`
(already used internally by /v1/face/register and /v1/face/identify).
Matches LocalAI's convention of a dedicated path per non-standard flow
(/v1/rerank, /v1/detection, /v1/face/verify etc.).
Response:
{
"embedding": [<dim> floats, L2-normed],
"dim": int, // 512 for ArcFace R50 / MBF, 128 for SFace
"model": "<name>"
}
Live-tested on the opencv engine: returns a 128-d L2-normalized vector
(sum(x^2) = 1.0000). Sentinel in docs updated to note /v1/embeddings
is text-only and point image users at /v1/face/embed instead.
Assisted-by: Claude:claude-opus-4-7
* fix(http): map malformed image input + gRPC status codes to proper 4xx
Image-input failures on LocalAI's single-image endpoints (/v1/detection,
/v1/face/{verify,analyze,embed,register,identify}) have historically
returned 500 — even when the client was the one who sent garbage.
Classic example: you POST an "image" that isn't a URL, isn't a
data-URI, and isn't a valid JPEG/PNG — the server shouldn't claim
that's its fault.
Two helpers land in core/http/endpoints/localai/images.go and every
single-image handler is switched over:
* decodeImageInput(s)
Wraps utils.GetContentURIAsBase64 and turns any failure
(invalid URL, not a data-URI, download error, etc.) into
echo.NewHTTPError(400, "invalid image input: ...").
* mapBackendError(err)
Inspects the gRPC status on a backend call error and maps:
INVALID_ARGUMENT → 400 Bad Request
NOT_FOUND → 404 Not Found
FAILED_PRECONDITION → 412 Precondition Failed
Unimplemented → 501 Not Implemented
All other codes fall through unchanged (still 500).
Before, my 1×1 PNG error-path test returned:
HTTP 500 "rpc error: code = InvalidArgument desc = failed to decode one or both images"
After:
HTTP 400 "failed to decode one or both images"
Scope-limited to the LocalAI single-image endpoints. The multi-modal
paths (middleware/request.go, openresponses/responses.go,
openai/realtime.go) intentionally log-and-skip individual media parts
when decoding fails — different design intent (graceful degradation
of a multi-part message), not a 400-worthy failure. Left untouched.
Live-verified: every error case in /tmp/face_errors.py now returns
4xx with a meaningful message; the "image with no face (1x1 PNG)"
case specifically went from 500 → 400.
Assisted-by: Claude:claude-opus-4-7
* refactor(face-recognition): insightface packs go through gallery files:, drop FaceAnalysis
Follows up on the discovery that LocalAI's gallery `files:` mechanism
handles archives (zip, tar.gz, …) via mholt/archiver/v3 — the rhasspy
piper voices use exactly this pattern. Insightface packs are zip
archives, so we can now deliver them the same way every other
gallery-managed model gets delivered: declaratively, checksum-verified,
through LocalAI's standard download+extract pipeline.
Two changes:
1. Gallery (gallery/index.yaml) — every insightface-* entry gains a
`files:` list with the pack zip's URI + SHA-256. `local-ai models
install insightface-buffalo-l` now fetches the zip, verifies the
hash, and extracts it into the models directory. No more reliance
on insightface's library-internal `ensure_available()` auto-download
or its hardcoded `BASE_REPO_URL`.
2. InsightFaceEngine (backend/python/insightface/engines.py) — drops
the FaceAnalysis wrapper and drives insightface's `model_zoo`
directly. The ~50 lines FaceAnalysis provides — glob ONNX files,
route each through `model_zoo.get_model()`, build a
`{taskname: model}` dict, loop per-face at inference — are
reimplemented in `InsightFaceEngine`. The actual inference classes
(RetinaFace, ArcFaceONNX, Attribute, Landmark) are still
insightface's — we only replicate the glue, so drift risk against
upstream is minimal.
Why drop FaceAnalysis: it hard-codes a `<root>/models/<name>/*.onnx`
layout that doesn't match what LocalAI's zip extraction produces.
LocalAI unpacks archives flat into `<models_dir>`. Upstream packs
are inconsistent — buffalo_l/s/sc ship ONNX at the zip root (lands
at `<models_dir>/*.onnx`), buffalo_m/antelopev2 wrap in a redundant
`<name>/` dir (lands at `<models_dir>/<name>/*.onnx`). The new
`_locate_insightface_pack` helper searches both locations plus
legacy paths and returns whichever has ONNX files. Replaces the
earlier `_flatten_insightface_pack` helper (which tried to fight
FaceAnalysis's layout expectations; now we just find the files
wherever they are).
Net effect for users: install once via LocalAI's managed flow,
weights live alongside every other model, progress shows in the
jobs endpoint, no first-load network call. Same API surface,
cleaner plumbing.
Assisted-by: Claude:claude-opus-4-7
* fix(face-recognition): CI's insightface e2e path needs the pack pre-fetched
The e2e suite drives LoadModel over gRPC without going through LocalAI's
gallery flow, so the engine's `_model_dir` option (normally populated
from ModelPath) is empty. Previously the insightface target relied on
FaceAnalysis auto-download to paper over this, but we dropped
FaceAnalysis in favor of direct model_zoo calls — so the buffalo_l
target started failing at LoadModel with "no insightface pack found".
Mirror the opencv target's pre-fetch pattern: download buffalo_sc.zip
(same SHA as the gallery entry), extract it on the host, and pass
`root:<dir>` so the engine locates the pack without needing
ModelPath. Switched to buffalo_sc (smallest pack, ~16MB) to keep CI
fast; it covers the same insightface engine code path as buffalo_l.
Face analyze cap dropped since buffalo_sc has no age/gender head.
Assisted-by: Claude:claude-opus-4-7[1m]
* feat(face-recognition): surface face-recognition in advertised feature maps
The six /v1/face/* endpoints were missing from every place LocalAI
advertises its feature surface to clients:
* api_instructions — the machine-readable capability index at
GET /api/instructions. Added `face-recognition` as a dedicated
instruction area with an intro that calls out the in-memory
registry caveat and the /v1/face/embed vs /v1/embeddings split.
* auth/permissions — added FeatureFaceRecognition constant, routed
all six face endpoints through it so admins can gate them per-user
like any other API feature. Default ON (matches the other API
features).
* React UI capabilities — CAP_FACE_RECOGNITION symbol mapped to
FLAG_FACE_RECOGNITION. Declared only for now; the Face page is a
follow-up (noted in the plan).
Instruction count bumped 9 → 10; test updated.
Assisted-by: Claude:claude-opus-4-7[1m]
* docs(agents): capture advertising-surface steps in the endpoint guide
Before this change, adding a new /v1/* endpoint reliably missed one or
more of: the swagger @Tags annotation, the /api/instructions registry,
the auth RouteFeatureRegistry, and the React UI CAP_* symbol. The
endpoint would work but be invisible to API consumers, admins, and the
UI — and nothing in the existing docs said to look in those places.
Extend .agents/api-endpoints-and-auth.md with a new "Advertising
surfaces" section covering all four surfaces (swagger tags, /api/
instructions, capabilities.js, docs/), and expand the closing checklist
so it's impossible to ship a feature without visiting each one. Hoist a
one-liner reminder into AGENTS.md's Quick Reference so agents skim it
before diving in.
Assisted-by: Claude:claude-opus-4-7[1m]
597 lines
15 KiB
Go
597 lines
15 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"crypto/subtle"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
|
|
pb "github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// A GRPC Server that allows to run LLM inference.
|
|
// It is used by the LLMServices to expose the LLM functionalities that are called by the client.
|
|
// The GRPC Service is general, trying to encompass all the possible LLM options models.
|
|
// It depends on the real implementer then what can be done or not.
|
|
//
|
|
// The server is implemented as a GRPC service, with the following methods:
|
|
// - Predict: to run the inference with options
|
|
// - PredictStream: to run the inference with options and stream the results
|
|
|
|
// server is used to implement helloworld.GreeterServer.
|
|
type server struct {
|
|
pb.UnimplementedBackendServer
|
|
llm AIModel
|
|
}
|
|
|
|
func (s *server) Health(ctx context.Context, in *pb.HealthMessage) (*pb.Reply, error) {
|
|
return newReply("OK"), nil
|
|
}
|
|
|
|
func (s *server) Embedding(ctx context.Context, in *pb.PredictOptions) (*pb.EmbeddingResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
embeds, err := s.llm.Embeddings(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &pb.EmbeddingResult{Embeddings: embeds}, nil
|
|
}
|
|
|
|
func (s *server) LoadModel(ctx context.Context, in *pb.ModelOptions) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
|
|
err := s.llm.Load(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error loading model: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Loading succeeded", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) Predict(ctx context.Context, in *pb.PredictOptions) (*pb.Reply, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
result, err := s.llm.Predict(in)
|
|
return newReply(result), err
|
|
}
|
|
|
|
func (s *server) GenerateImage(ctx context.Context, in *pb.GenerateImageRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.GenerateImage(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error generating image: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Image generated", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) GenerateVideo(ctx context.Context, in *pb.GenerateVideoRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.GenerateVideo(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error generating video: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Video generated", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) TTS(ctx context.Context, in *pb.TTSRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.TTS(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error generating audio: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "TTS audio generated", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) TTSStream(in *pb.TTSRequest, stream pb.Backend_TTSStreamServer) error {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
audioChan := make(chan []byte)
|
|
|
|
done := make(chan bool)
|
|
go func() {
|
|
for audioChunk := range audioChan {
|
|
stream.Send(&pb.Reply{Audio: audioChunk})
|
|
}
|
|
done <- true
|
|
}()
|
|
|
|
err := s.llm.TTSStream(in, audioChan)
|
|
<-done
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *server) SoundGeneration(ctx context.Context, in *pb.SoundGenerationRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.SoundGeneration(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error generating audio: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Sound Generation audio generated", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) Detect(ctx context.Context, in *pb.DetectOptions) (*pb.DetectResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.Detect(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) FaceVerify(ctx context.Context, in *pb.FaceVerifyRequest) (*pb.FaceVerifyResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.FaceVerify(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) FaceAnalyze(ctx context.Context, in *pb.FaceAnalyzeRequest) (*pb.FaceAnalyzeResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.FaceAnalyze(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) AudioTranscription(ctx context.Context, in *pb.TranscriptRequest) (*pb.TranscriptResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
result, err := s.llm.AudioTranscription(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tresult := &pb.TranscriptResult{}
|
|
for _, s := range result.Segments {
|
|
tks := []int32{}
|
|
for _, t := range s.Tokens {
|
|
tks = append(tks, int32(t))
|
|
}
|
|
tresult.Segments = append(tresult.Segments,
|
|
&pb.TranscriptSegment{
|
|
Text: s.Text,
|
|
Id: int32(s.Id),
|
|
Start: int64(s.Start),
|
|
End: int64(s.End),
|
|
Tokens: tks,
|
|
Speaker: s.Speaker,
|
|
})
|
|
}
|
|
|
|
tresult.Text = result.Text
|
|
tresult.Language = result.Language
|
|
tresult.Duration = result.Duration
|
|
return tresult, nil
|
|
}
|
|
|
|
func (s *server) AudioTranscriptionStream(in *pb.TranscriptRequest, stream pb.Backend_AudioTranscriptionStreamServer) error {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
resultChan := make(chan *pb.TranscriptStreamResponse)
|
|
|
|
done := make(chan bool)
|
|
go func() {
|
|
for chunk := range resultChan {
|
|
stream.Send(chunk)
|
|
}
|
|
done <- true
|
|
}()
|
|
|
|
err := s.llm.AudioTranscriptionStream(in, resultChan)
|
|
<-done
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *server) PredictStream(in *pb.PredictOptions, stream pb.Backend_PredictStreamServer) error {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
resultChan := make(chan string)
|
|
|
|
done := make(chan bool)
|
|
go func() {
|
|
for result := range resultChan {
|
|
stream.Send(newReply(result))
|
|
}
|
|
done <- true
|
|
}()
|
|
|
|
err := s.llm.PredictStream(in, resultChan)
|
|
<-done
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *server) TokenizeString(ctx context.Context, in *pb.PredictOptions) (*pb.TokenizationResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.TokenizeString(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
castTokens := make([]int32, len(res.Tokens))
|
|
for i, v := range res.Tokens {
|
|
castTokens[i] = int32(v)
|
|
}
|
|
|
|
return &pb.TokenizationResponse{
|
|
Length: int32(res.Length),
|
|
Tokens: castTokens,
|
|
}, err
|
|
}
|
|
|
|
func (s *server) Status(ctx context.Context, in *pb.HealthMessage) (*pb.StatusResponse, error) {
|
|
res, err := s.llm.Status()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) StoresSet(ctx context.Context, in *pb.StoresSetOptions) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.StoresSet(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error setting entry: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Set key", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) StoresDelete(ctx context.Context, in *pb.StoresDeleteOptions) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.StoresDelete(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error deleting entry: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Deleted key", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) StoresGet(ctx context.Context, in *pb.StoresGetOptions) (*pb.StoresGetResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.StoresGet(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) StoresFind(ctx context.Context, in *pb.StoresFindOptions) (*pb.StoresFindResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.StoresFind(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) VAD(ctx context.Context, in *pb.VADRequest) (*pb.VADResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.VAD(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &res, nil
|
|
}
|
|
|
|
func (s *server) AudioEncode(ctx context.Context, in *pb.AudioEncodeRequest) (*pb.AudioEncodeResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.AudioEncode(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (s *server) AudioDecode(ctx context.Context, in *pb.AudioDecodeRequest) (*pb.AudioDecodeResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.AudioDecode(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (s *server) StartFineTune(ctx context.Context, in *pb.FineTuneRequest) (*pb.FineTuneJobResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.StartFineTune(in)
|
|
if err != nil {
|
|
return &pb.FineTuneJobResult{Success: false, Message: fmt.Sprintf("Error starting fine-tune: %s", err.Error())}, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (s *server) FineTuneProgress(in *pb.FineTuneProgressRequest, stream pb.Backend_FineTuneProgressServer) error {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
updateChan := make(chan *pb.FineTuneProgressUpdate)
|
|
|
|
done := make(chan bool)
|
|
go func() {
|
|
for update := range updateChan {
|
|
stream.Send(update)
|
|
}
|
|
done <- true
|
|
}()
|
|
|
|
err := s.llm.FineTuneProgress(in, updateChan)
|
|
<-done
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *server) StopFineTune(ctx context.Context, in *pb.FineTuneStopRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.StopFineTune(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error stopping fine-tune: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Fine-tune stopped", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) ListCheckpoints(ctx context.Context, in *pb.ListCheckpointsRequest) (*pb.ListCheckpointsResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.ListCheckpoints(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (s *server) ExportModel(ctx context.Context, in *pb.ExportModelRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.ExportModel(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error exporting model: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Model exported", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) StartQuantization(ctx context.Context, in *pb.QuantizationRequest) (*pb.QuantizationJobResult, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.StartQuantization(in)
|
|
if err != nil {
|
|
return &pb.QuantizationJobResult{Success: false, Message: fmt.Sprintf("Error starting quantization: %s", err.Error())}, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (s *server) QuantizationProgress(in *pb.QuantizationProgressRequest, stream pb.Backend_QuantizationProgressServer) error {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
updateChan := make(chan *pb.QuantizationProgressUpdate)
|
|
|
|
done := make(chan bool)
|
|
go func() {
|
|
for update := range updateChan {
|
|
stream.Send(update)
|
|
}
|
|
done <- true
|
|
}()
|
|
|
|
err := s.llm.QuantizationProgress(in, updateChan)
|
|
<-done
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *server) StopQuantization(ctx context.Context, in *pb.QuantizationStopRequest) (*pb.Result, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
err := s.llm.StopQuantization(in)
|
|
if err != nil {
|
|
return &pb.Result{Message: fmt.Sprintf("Error stopping quantization: %s", err.Error()), Success: false}, err
|
|
}
|
|
return &pb.Result{Message: "Quantization stopped", Success: true}, nil
|
|
}
|
|
|
|
func (s *server) ModelMetadata(ctx context.Context, in *pb.ModelOptions) (*pb.ModelMetadataResponse, error) {
|
|
if s.llm.Locking() {
|
|
s.llm.Lock()
|
|
defer s.llm.Unlock()
|
|
}
|
|
res, err := s.llm.ModelMetadata(in)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (s *server) Free(ctx context.Context, in *pb.HealthMessage) (*pb.Result, error) {
|
|
if err := s.llm.Free(); err != nil {
|
|
return &pb.Result{Success: false, Message: err.Error()}, nil
|
|
}
|
|
return &pb.Result{Success: true}, nil
|
|
}
|
|
|
|
// NewBackendServer creates a pb.BackendServer.
|
|
func NewBackendServer(model AIModel) pb.BackendServer {
|
|
return &server{llm: model}
|
|
}
|
|
|
|
// AuthTokenEnvVar is the environment variable used to configure gRPC bearer token auth.
|
|
const AuthTokenEnvVar = "LOCALAI_GRPC_AUTH_TOKEN"
|
|
|
|
// validateToken extracts the bearer token from gRPC metadata and validates it.
|
|
func validateToken(ctx context.Context, expected string) error {
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
if !ok {
|
|
return status.Error(codes.Unauthenticated, "missing metadata")
|
|
}
|
|
values := md.Get("authorization")
|
|
if len(values) == 0 {
|
|
return status.Error(codes.Unauthenticated, "missing authorization header")
|
|
}
|
|
raw := values[0]
|
|
if !strings.HasPrefix(raw, "Bearer ") {
|
|
return status.Error(codes.Unauthenticated, "authorization must use Bearer scheme")
|
|
}
|
|
token := strings.TrimPrefix(raw, "Bearer ")
|
|
if subtle.ConstantTimeCompare([]byte(token), []byte(expected)) != 1 {
|
|
return status.Error(codes.Unauthenticated, "invalid token")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func tokenUnaryInterceptor(token string) grpc.UnaryServerInterceptor {
|
|
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
|
|
if err := validateToken(ctx, token); err != nil {
|
|
return nil, err
|
|
}
|
|
return handler(ctx, req)
|
|
}
|
|
}
|
|
|
|
func tokenStreamInterceptor(token string) grpc.StreamServerInterceptor {
|
|
return func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
if err := validateToken(ss.Context(), token); err != nil {
|
|
return err
|
|
}
|
|
return handler(srv, ss)
|
|
}
|
|
}
|
|
|
|
// serverOpts returns the common gRPC server options, including auth interceptors
|
|
// when LOCALAI_GRPC_AUTH_TOKEN is set.
|
|
func serverOpts() []grpc.ServerOption {
|
|
opts := []grpc.ServerOption{
|
|
grpc.MaxRecvMsgSize(maxGRPCMessageSize),
|
|
grpc.MaxSendMsgSize(maxGRPCMessageSize),
|
|
}
|
|
if token := os.Getenv(AuthTokenEnvVar); token != "" {
|
|
opts = append(opts,
|
|
grpc.UnaryInterceptor(tokenUnaryInterceptor(token)),
|
|
grpc.StreamInterceptor(tokenStreamInterceptor(token)),
|
|
)
|
|
log.Printf("gRPC auth enabled via %s", AuthTokenEnvVar)
|
|
}
|
|
return opts
|
|
}
|
|
|
|
func StartServer(address string, model AIModel) error {
|
|
lis, err := net.Listen("tcp", address)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s := grpc.NewServer(serverOpts()...)
|
|
pb.RegisterBackendServer(s, &server{llm: model})
|
|
log.Printf("gRPC Server listening at %v", lis.Addr())
|
|
if err := s.Serve(lis); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func RunServer(address string, model AIModel) (func() error, error) {
|
|
lis, err := net.Listen("tcp", address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s := grpc.NewServer(serverOpts()...)
|
|
pb.RegisterBackendServer(s, &server{llm: model})
|
|
log.Printf("gRPC Server listening at %v", lis.Addr())
|
|
if err = s.Serve(lis); err != nil {
|
|
return func() error {
|
|
return lis.Close()
|
|
}, err
|
|
}
|
|
|
|
return func() error {
|
|
s.GracefulStop()
|
|
return nil
|
|
}, nil
|
|
}
|