Yayoi_no_yume on X asked for the privacy model's threshold to be adjustable: they read it as fixed at 0.5, and from their own use find 0.85-0.95 right for AstrLink. The 0.5 is Privacy Filter's (pf-serve's argv default); AstrLink Guard already masked at its publisher's 0.8. Both models now find at a floor of 0.5 (pii.Floor) and give each span its score: Guard's least token probability (rounded through float32, so a span read back from the cache file sits on the same side of a threshold), pf-serve's own. masking.model keeps the spans at pii.Threshold or over: settings.RedactModelScore[model] when it is in [0.5, 1), else the model's MinScore (Privacy Filter 0.5, AstrLink Guard 0.8). Settings -> Privacy shows a Confidence threshold row under the model (app menu, 0.5 to 0.95 and Default), /api/privacy-model takes do:"score". The setting is this computer's own, like the model. The defaults stay. TestMaskEval's cases, rules + model + magpie's checks, by threshold: Privacy Filter 0.5-0.8 masked 41/42 kept 112/113 Privacy Filter 0.85-0.95 masked 40/42 (a Chinese name lost) AstrLink Guard 0.5-0.7 masked 42/42 kept 112/113 AstrLink Guard 0.8-0.85 masked 41/42 (a phone in a tool result) AstrLink Guard 0.9 masked 40/42 AstrLink Guard 0.95 masked 40/42 kept 113/113 The model alone does raise fewer false alarms higher up (Privacy Filter's found-in-keep falls from 47 at 0.5 to 27 at 0.95, Guard's from 8 to 2), but the checks after it drop most of those already, so a higher threshold mostly costs values that should be masked. Cache file: found-<model>.bin is now "pfound02" with a float32 score per span. A "pfound01" file is still read, each span with score 1, so texts cached before mask as they did and aren't asked again. For those texts a higher threshold drops nothing and a lower one doesn't add a Guard span scored 0.5-0.8 until the cache turns over (20000 answers). merge() keeps the surer score of spans it joins. The eval recordings (eval-model.json, eval-astrlink.json) were made again with scores, from a scratch cache, the live found files untouched. Run in the worktree at11dcc743f+ this change: - go vet / gofmt -l on the touched packages: clean - go test -count=1 -timeout 45m ./internal/pii/... ./internal/masking ./internal/redact ./internal/settings ./internal/gui ./internal/gateway: all ok (gateway 546s, rerun after an earlier run hit "no space left on device" on this machine; every failure in that run was a TempDir mkdir). This run caught TestSettingsSaveKeepsWhatItDoesNotSend wanting RedactModelScore set; it is now - GUI: privacy-model.test.cjs 64/64 (chromium + webkit, 5 languages, 1100 and 640px; the new threshold menu test among them), i18n-keys 4/4, redact-kinds, redact-rule-edit, redact-rule-row, redact-shape, redact-words-any-case all pass. masking-records 82/82, routing-masking 10/10, privacy-usage-stats 4/4, mask-actions 22/22. privacy-hides-accounts was 3/4 once (webkit en, page.goto's 5s at load ~290, the class of #3525, commented there), then 4/4 three times. Not run: the rest of the GUI suite. Rebased on508911e43(which touched internal/pii fetch and mirror): at that head, go build ./..., Linux and Windows go vet of pii, masking, settings and redact, gofmt -l, and go test of pii/..., masking, redact, settings and gui all ok; gateway not run again after the rebase. Broken by hand: masking.model keeping every span (TestModelThreshold red); Threshold taking any value (TestModelThreshold red); the cache writing 1 for every score (TestRestartReadsWhatWasFound red); merge not keeping the surer score (TestMergeAcrossPieces red, after its joined span was made surer than the one it joins). Not tried: the real app's window, Windows, Privacy Filter on a GPU with the new score read (pf-serve itself is unchanged; it always sent the score).
154 lines
4.4 KiB
Go
154 lines
4.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// privacyModelJSON is Settings → Privacy's local masking model: whether it
|
|
// is on, the model masking uses (or would, turned on) with its own status
|
|
// (downloaded, downloading, where it runs) and threshold (the one the
|
|
// user set, else its default), and every model there is to pick, with
|
|
// theirs.
|
|
type privacyModelJSON struct {
|
|
On bool `json:"on"`
|
|
pii.Status
|
|
MinScore float64 `json:"minScore"`
|
|
DefaultScore float64 `json:"defaultScore"`
|
|
Models []pii.Status `json:"models"`
|
|
}
|
|
|
|
// modelOf and models are pii's; vars for tests.
|
|
var modelOf, models = pii.Of, pii.Models
|
|
|
|
func privacyModelState() privacyModelJSON {
|
|
st := settings.Load()
|
|
m := modelOf(pii.Choose(st.RedactModelName, st.RedactModel))
|
|
out := privacyModelJSON{On: st.RedactModel, Status: m.Now(), MinScore: pii.Threshold(st.RedactModelScore, m), DefaultScore: m.MinScore()}
|
|
for _, m := range models() {
|
|
out.Models = append(out.Models, m.Now())
|
|
}
|
|
return out
|
|
}
|
|
|
|
// privacyModelRoutes: GET /api/privacy-model is its state; POST with
|
|
// {"do": "on" | "off" | "cancel" | "remove" | "pick" | "score", "model": id,
|
|
// "score": s} turns
|
|
// it on (as model, or the one it would use: downloading it first, and
|
|
// saving it on once that is done), off (a download is stopped, the model
|
|
// let go and kept), stops a download, turns it off and deletes every
|
|
// model downloaded, or picks model: while it is on, that is turning it on
|
|
// as model (the one before masks until model is downloaded); while off,
|
|
// the pick is kept for turning it on; score keeps s as the threshold of
|
|
// model (the one masking uses, with none), 0 for its default.
|
|
func privacyModelRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/privacy-model", func(rw http.ResponseWriter, r *http.Request) {
|
|
writeJSON(rw, privacyModelState())
|
|
})
|
|
mux.HandleFunc("POST /api/privacy-model", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Do string `json:"do"`
|
|
Model string `json:"model"`
|
|
Score float64 `json:"score"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if in.Model != "" && in.Model != pii.PrivacyFilter && in.Model != pii.AstrLinkGuard {
|
|
fail(rw, fmt.Errorf("model is %s or %s, not %q", pii.PrivacyFilter, pii.AstrLinkGuard, in.Model))
|
|
return
|
|
}
|
|
st := settings.Load()
|
|
was := pii.Choose(st.RedactModelName, st.RedactModel)
|
|
var err error
|
|
switch in.Do {
|
|
case "pick":
|
|
switch {
|
|
case in.Model == "":
|
|
err = fmt.Errorf("pick needs a model")
|
|
case st.RedactModel:
|
|
err = turnOn(in.Model, was)
|
|
default:
|
|
err = settings.SetRedactModelName(in.Model)
|
|
}
|
|
case "score":
|
|
id := in.Model
|
|
if id == "" {
|
|
id = was
|
|
}
|
|
if in.Score != 0 && !(in.Score >= pii.Floor && in.Score < 1) {
|
|
err = fmt.Errorf("score is 0 (the model's own) or from %g to under 1, not %g", pii.Floor, in.Score)
|
|
} else {
|
|
err = settings.SetRedactModelScore(id, in.Score)
|
|
}
|
|
case "on":
|
|
id := in.Model
|
|
if id == "" {
|
|
id = was
|
|
}
|
|
err = turnOn(id, was)
|
|
case "off":
|
|
for _, m := range models() {
|
|
m.Cancel()
|
|
}
|
|
err = settings.SetRedactModel(false)
|
|
for _, m := range models() {
|
|
m.Stop()
|
|
}
|
|
case "cancel":
|
|
for _, m := range models() {
|
|
m.Cancel()
|
|
}
|
|
case "remove":
|
|
if err = settings.SetRedactModel(false); err == nil {
|
|
for _, m := range models() {
|
|
if e := m.Remove(); e != nil && err == nil {
|
|
err = e
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
err = fmt.Errorf("do is on, off, cancel, remove, pick or score, not %q", in.Do)
|
|
}
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
writeJSON(rw, privacyModelState())
|
|
})
|
|
}
|
|
|
|
// turnOn turns the local model on as id, once it is on disk: a request
|
|
// in between is masked as before (by the rules, or by was, the model on
|
|
// till then), and not held back. A download of another model under way
|
|
// is stopped; was is let go once id masks.
|
|
func turnOn(id, was string) error {
|
|
m := modelOf(id)
|
|
if !m.Supported() {
|
|
return fmt.Errorf("the local masking model doesn't run on this system yet")
|
|
}
|
|
for _, o := range models() {
|
|
if o.ID() != id {
|
|
o.Cancel()
|
|
}
|
|
}
|
|
on := func() {
|
|
if settings.UseRedactModel(id) == nil {
|
|
if was != id {
|
|
modelOf(was).Stop()
|
|
}
|
|
m.Warm()
|
|
}
|
|
}
|
|
if m.Installed() {
|
|
on()
|
|
return nil
|
|
}
|
|
return m.Download(on)
|
|
}
|