Files
LocalAI/core/backend/diarization.go
mudler's LocalAI [bot] 1cd7d63c7b fix(distributed): reject wrong-model requests on the remaining modalities (#10990)
#10970 gave the four PredictOptions RPCs a model-identity check so a
backend reached through a stale distributed route rejects the request
instead of answering from whatever model it holds (#10952). Every other
modality shares that exposure: the route is cached by host:port, a worker
can recycle a stopped backend's port for another model's backend, and a
liveness-only probe cannot tell a stale row from a valid one.

Extends the same mechanism to the 21 remaining request messages that reach
a backend through the router, using the pattern #10970 established rather
than a parallel one:

- proto: ModelIdentity on each modality request message.
- controller: populated from ModelConfig.Model at the call site that also
  builds ModelOptions, so load-time and request-time values are equal by
  construction.
- backends: one generic guard in pkg/grpc/server.go (27 Go backends), the
  method set in backend/python/common (36 Python backends), llama-cpp
  (AudioTranscription/Stream, Rerank, Score) and privacy-filter
  (TokenClassify).
- reconcile already drops the stale row on IsModelMismatch; no change.

TTSRequest and SoundGenerationRequest get a SEPARATE ModelIdentity field
rather than reusing their existing `model`: FileStagingClient rewrites
`model` to a worker-local path, so comparing it would reject valid
requests in exactly the configuration this guards.

AudioEncode/AudioDecode are deliberately left unguarded: the opus codec
backend is loaded from a literal rather than a ModelConfig, so no value
carries the equality guarantee the comparison depends on. The four
bidirectional stream RPCs are out of scope; they bypass reconcile.

Empty means skip on both sides, so an old controller, an old backend, and
the bare request structs in tests/e2e-backends all keep working.


Assisted-by: Claude Code:claude-opus-4-8 [Read] [Edit] [Bash]

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
2026-07-20 21:58:19 +02:00

161 lines
4.7 KiB
Go

package backend
import (
"context"
"fmt"
"sort"
"github.com/mudler/LocalAI/core/config"
"github.com/mudler/LocalAI/core/schema"
grpcPkg "github.com/mudler/LocalAI/pkg/grpc"
"github.com/mudler/LocalAI/pkg/grpc/proto"
"github.com/mudler/LocalAI/pkg/model"
)
// DiarizationRequest carries the diarization-specific knobs the HTTP
// layer collects. Speaker hints (NumSpeakers / MinSpeakers / MaxSpeakers)
// and clustering knobs are optional — backends ignore the ones they
// don't act on. IncludeText only matters for backends that emit
// per-segment transcripts as a by-product (e.g. vibevoice.cpp).
type DiarizationRequest struct {
Audio string
Language string
NumSpeakers int32
MinSpeakers int32
MaxSpeakers int32
ClusteringThreshold float32
MinDurationOn float32
MinDurationOff float32
IncludeText bool
}
// modelIdentity: see the note on TranscriptionRequest.toProto.
func (r *DiarizationRequest) toProto(threads uint32, modelIdentity string) *proto.DiarizeRequest {
return &proto.DiarizeRequest{
ModelIdentity: modelIdentity,
Dst: r.Audio,
Threads: threads,
Language: r.Language,
NumSpeakers: r.NumSpeakers,
MinSpeakers: r.MinSpeakers,
MaxSpeakers: r.MaxSpeakers,
ClusteringThreshold: r.ClusteringThreshold,
MinDurationOn: r.MinDurationOn,
MinDurationOff: r.MinDurationOff,
IncludeText: r.IncludeText,
}
}
func loadDiarizationModel(ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (grpcPkg.Backend, error) {
if modelConfig.Backend == "" {
return nil, fmt.Errorf("diarization: model %q has no backend set; supported backends include vibevoice-cpp and sherpa-onnx", modelConfig.Name)
}
opts := ModelOptions(modelConfig, appConfig)
m, err := ml.Load(opts...)
if err != nil {
recordModelLoadFailure(appConfig, modelConfig.Name, modelConfig.Backend, err, nil)
return nil, err
}
if m == nil {
return nil, fmt.Errorf("could not load diarization model")
}
return m, nil
}
// ModelDiarization runs the Diarize RPC against the configured backend
// and returns a normalized schema.DiarizationResult.
func ModelDiarization(ctx context.Context, req DiarizationRequest, ml *model.ModelLoader, modelConfig config.ModelConfig, appConfig *config.ApplicationConfig) (*schema.DiarizationResult, error) {
m, err := loadDiarizationModel(ml, modelConfig, appConfig)
if err != nil {
return nil, err
}
threads := uint32(0)
if modelConfig.Threads != nil {
threads = uint32(*modelConfig.Threads)
}
r, err := m.Diarize(ctx, req.toProto(threads, modelConfig.Model))
if err != nil {
return nil, err
}
return diarizationResultFromProto(r), nil
}
// diarizationResultFromProto normalizes backend speaker labels to
// "SPEAKER_NN" — the convention pyannote/RTTM tooling expects — while
// keeping the original label available via the Speaker field. Each
// distinct backend label gets its own normalized id, in first-seen order.
func diarizationResultFromProto(r *proto.DiarizeResponse) *schema.DiarizationResult {
if r == nil {
return &schema.DiarizationResult{Segments: []schema.DiarizationSegment{}}
}
out := &schema.DiarizationResult{
Task: "diarize",
Duration: float64(r.Duration),
Language: r.Language,
Segments: make([]schema.DiarizationSegment, 0, len(r.Segments)),
}
type speakerStats struct {
idx int
duration float64
segments int
}
stats := map[string]*speakerStats{}
order := []string{}
for i, s := range r.Segments {
if s == nil {
continue
}
raw := s.Speaker
if raw == "" {
raw = "0"
}
st, ok := stats[raw]
if !ok {
st = &speakerStats{idx: len(order)}
stats[raw] = st
order = append(order, raw)
}
dur := float64(s.End) - float64(s.Start)
if dur > 0 {
st.duration += dur
}
st.segments++
out.Segments = append(out.Segments, schema.DiarizationSegment{
Id: i,
Speaker: fmt.Sprintf("SPEAKER_%02d", st.idx),
Label: raw,
Start: float64(s.Start),
End: float64(s.End),
Text: s.Text,
})
}
out.NumSpeakers = len(order)
if out.NumSpeakers == 0 && r.NumSpeakers > 0 {
out.NumSpeakers = int(r.NumSpeakers)
}
out.Speakers = make([]schema.DiarizationSpeaker, 0, len(order))
for _, raw := range order {
st := stats[raw]
out.Speakers = append(out.Speakers, schema.DiarizationSpeaker{
Id: fmt.Sprintf("SPEAKER_%02d", st.idx),
Label: raw,
TotalSpeechDuration: st.duration,
SegmentCount: st.segments,
})
}
sort.SliceStable(out.Speakers, func(i, j int) bool {
return out.Speakers[i].Id < out.Speakers[j].Id
})
return out
}