Skip to content

Commit aef9fd8

Browse files
committed
feat(web): sort and filter transcription models
1 parent 6577c6b commit aef9fd8

3 files changed

Lines changed: 224 additions & 21 deletions

File tree

‎apps/web/src/components/settings/VoiceSettingsPanel.tsx‎

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
MicIcon,
3232
RefreshCwIcon,
3333
StarIcon,
34+
SlidersHorizontalIcon,
3435
Trash2Icon,
3536
XIcon,
3637
} from "lucide-react";
@@ -65,13 +66,27 @@ import { Badge } from "../ui/badge";
6566
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
6667
import { Button } from "../ui/button";
6768
import { Input } from "../ui/input";
69+
import { InputGroup, InputGroupAddon, InputGroupInput } from "../ui/input-group";
70+
import {
71+
Menu,
72+
MenuCheckboxItem,
73+
MenuGroup,
74+
MenuGroupLabel,
75+
MenuPopup,
76+
MenuRadioGroup,
77+
MenuRadioItem,
78+
MenuRadioItemIndicator,
79+
MenuSeparator,
80+
MenuTrigger,
81+
} from "../ui/menu";
6882
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
6983
import { Switch } from "../ui/switch";
7084
import { Textarea } from "../ui/textarea";
7185
import { toastManager } from "../ui/toast";
7286
import { MicrophoneTest } from "./MicrophoneTest";
7387
import { LanguageRow, ModelBenchmarks, orderSpeechLanguages } from "./SpeechModelComparison";
7488
import { languageLabel, SpeechLanguageSelect } from "./SpeechLanguageSelect";
89+
import { filterSpeechModels, type SpeechModelSort } from "./speechModelBrowser";
7590
import { AdvancedSettings } from "./AdvancedSettings";
7691
import {
7792
SETTINGS_PICKER_TRIGGER_CLASSNAME,
@@ -110,12 +125,6 @@ const spellingsOf = (words: SpeechCustomWords) =>
110125
[term, ...aliases].map((spelling) => spelling.toLocaleLowerCase()),
111126
),
112127
);
113-
const modelSortOrder = (model: EnvironmentSpeechModel) => {
114-
if (model.active) return 0;
115-
if (model.state === "installed") return 1;
116-
if (model.state === "downloading" || model.state === "verifying") return 2;
117-
return model.recommended ? 3 : 4;
118-
};
119128

120129
type ModelCardProps = {
121130
readonly model: EnvironmentSpeechModel;
@@ -717,6 +726,8 @@ function SpeechModelBrowser(props: {
717726
catalog: { status, models, run, controlsDisabled, canCancel, requestedDownload },
718727
} = props;
719728
const [search, setSearch] = useState("");
729+
const [sort, setSort] = useState<SpeechModelSort>("recommended");
730+
const [downloadedOnly, setDownloadedOnly] = useState(false);
720731
const [languageSearch, setLanguageSearch] = useState("");
721732
const [selection, setSelection] = useState<string[] | null>(null);
722733
const preferred = navigator.languages.map((language) => language.toLowerCase().split("-")[0]!);
@@ -729,14 +740,20 @@ function SpeechModelBrowser(props: {
729740
(code) =>
730741
!needle || code.includes(needle) || languageLabel(code).toLocaleLowerCase().includes(needle),
731742
);
732-
const query = search.trim().toLocaleLowerCase();
733-
const sortedModels = models
734-
.filter(
735-
(model) =>
736-
selectedLanguages.every((language) => model.languages.includes(language)) &&
737-
(!query || (model.name + " " + model.description).toLocaleLowerCase().includes(query)),
738-
)
739-
.toSorted((left, right) => modelSortOrder(left) - modelSortOrder(right));
743+
const accuracyLanguage = selectedLanguages[0] ?? "en";
744+
const sortLabels: Record<SpeechModelSort, string> = {
745+
recommended: "Recommended",
746+
size: "Smallest download",
747+
accuracy: `Best ${languageLabel(accuracyLanguage)} accuracy`,
748+
speed: "Fastest transcription",
749+
};
750+
const sortedModels = filterSpeechModels(models, {
751+
search,
752+
languages: selectedLanguages,
753+
downloadedOnly,
754+
sort,
755+
accuracyLanguage,
756+
});
740757
const activeModel = models.find((model) => model.active);
741758
const renderedModels =
742759
activeModel && !sortedModels.includes(activeModel)
@@ -808,13 +825,54 @@ function SpeechModelBrowser(props: {
808825
? "Models for " + selectedLanguages.map(languageLabel).join(", ")
809826
: "All models"}
810827
</p>
811-
<Input
812-
size="compact"
813-
aria-label="Search transcription models"
814-
placeholder="Search models"
815-
value={search}
816-
onChange={(e) => setSearch(e.target.value)}
817-
/>
828+
<InputGroup>
829+
<InputGroupInput
830+
size="compact"
831+
aria-label="Search transcription models"
832+
placeholder="Search models"
833+
value={search}
834+
onChange={(e) => setSearch(e.target.value)}
835+
/>
836+
<InputGroupAddon align="inline-end">
837+
<Menu>
838+
<MenuTrigger
839+
render={<Button size="compact" variant="ghost-muted" />}
840+
aria-label={`Sort and filter models: ${sortLabels[sort]}${downloadedOnly ? ", downloaded only" : ""}`}
841+
>
842+
<SlidersHorizontalIcon />
843+
<span className="hidden @min-[40rem]/voice-models:inline">
844+
{sortLabels[sort]}
845+
</span>
846+
{downloadedOnly && <span>1</span>}
847+
</MenuTrigger>
848+
<MenuPopup align="end">
849+
<MenuGroup>
850+
<MenuGroupLabel>Sort by</MenuGroupLabel>
851+
<MenuRadioGroup
852+
value={sort}
853+
onValueChange={(value) => setSort(value as SpeechModelSort)}
854+
>
855+
{(Object.keys(sortLabels) as SpeechModelSort[]).map((value) => (
856+
<MenuRadioItem key={value} value={value} closeOnClick>
857+
<span className="flex items-center justify-between gap-3">
858+
{sortLabels[value]}
859+
<MenuRadioItemIndicator />
860+
</span>
861+
</MenuRadioItem>
862+
))}
863+
</MenuRadioGroup>
864+
</MenuGroup>
865+
<MenuSeparator />
866+
<MenuCheckboxItem
867+
checked={downloadedOnly}
868+
onCheckedChange={setDownloadedOnly}
869+
>
870+
Downloaded only
871+
</MenuCheckboxItem>
872+
</MenuPopup>
873+
</Menu>
874+
</InputGroupAddon>
875+
</InputGroup>
818876
</div>
819877
<div
820878
className="h-72 overflow-y-auto divide-y divide-border/60"
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import type { EnvironmentSpeechModel } from "@t3tools/contracts";
2+
import { expect, it } from "vite-plus/test";
3+
4+
import { filterSpeechModels } from "./speechModelBrowser";
5+
6+
const model: EnvironmentSpeechModel = {
7+
id: "active",
8+
name: "Active",
9+
description: "Local transcription",
10+
size: 300,
11+
languages: ["en", "fr"],
12+
accuracy: 1,
13+
speed: 1,
14+
recommended: false,
15+
supportsStreaming: false,
16+
supportsLanguageDetection: false,
17+
supportsTranslation: false,
18+
active: true,
19+
state: "installed",
20+
};
21+
const models: readonly EnvironmentSpeechModel[] = [
22+
model,
23+
{
24+
...model,
25+
id: "small",
26+
name: "Small",
27+
size: 100,
28+
active: false,
29+
state: "downloadable",
30+
recommended: true,
31+
benchmark: {
32+
accuracy: {
33+
en: { grade: "A", errorPercent: 3.8 },
34+
fr: { grade: "B", errorPercent: 8 },
35+
},
36+
secondsPer30s: 2,
37+
},
38+
},
39+
{
40+
...model,
41+
id: "fast",
42+
name: "Fast",
43+
size: 200,
44+
active: false,
45+
benchmark: {
46+
accuracy: {
47+
en: { grade: "A", errorPercent: 3.2 },
48+
fr: { grade: "B", errorPercent: 9 },
49+
},
50+
secondsPer30s: 1,
51+
},
52+
},
53+
{
54+
...model,
55+
id: "pending",
56+
name: "Pending",
57+
size: 400,
58+
active: false,
59+
state: "verifying",
60+
languages: ["en"],
61+
benchmark: { accuracy: {}, secondsPer30s: null },
62+
},
63+
];
64+
65+
it.each([
66+
["recommended", "en", ["active", "fast", "pending", "small"]],
67+
["size", "en", ["small", "fast", "active", "pending"]],
68+
["speed", "en", ["fast", "small", "active", "pending"]],
69+
["accuracy", "en", ["fast", "small", "active", "pending"]],
70+
["accuracy", "fr", ["small", "fast", "active", "pending"]],
71+
] as const)(
72+
"sorts by %s for %s with missing benchmarks last",
73+
(sort, accuracyLanguage, expected) => {
74+
expect(
75+
filterSpeechModels(models, {
76+
search: "",
77+
languages: [],
78+
downloadedOnly: false,
79+
sort,
80+
accuracyLanguage,
81+
}).map((entry) => entry.id),
82+
).toEqual(expected);
83+
expect(models[0]?.id).toBe("active");
84+
},
85+
);
86+
87+
it("combines text, all selected languages, and completed downloads without retaining the active model", () => {
88+
const options = {
89+
search: " LOCAL ",
90+
languages: ["en", "fr"],
91+
downloadedOnly: true,
92+
sort: "size",
93+
accuracyLanguage: "en",
94+
} as const;
95+
expect(filterSpeechModels(models, options).map((entry) => entry.id)).toEqual(["fast", "active"]);
96+
expect(
97+
filterSpeechModels(models, { ...options, search: "fast" }).map((entry) => entry.id),
98+
).toEqual(["fast"]);
99+
expect(filterSpeechModels(models, { ...options, search: "small" })).toEqual([]);
100+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { EnvironmentSpeechModel } from "@t3tools/contracts";
2+
3+
export type SpeechModelSort = "recommended" | "size" | "accuracy" | "speed";
4+
5+
const modelSortOrder = (model: EnvironmentSpeechModel) => {
6+
if (model.active) return 0;
7+
if (model.state === "installed") return 1;
8+
if (model.state === "downloading" || model.state === "verifying") return 2;
9+
return model.recommended ? 3 : 4;
10+
};
11+
12+
export function filterSpeechModels(
13+
models: readonly EnvironmentSpeechModel[],
14+
options: {
15+
readonly search: string;
16+
readonly languages: readonly string[];
17+
readonly downloadedOnly: boolean;
18+
readonly sort: SpeechModelSort;
19+
readonly accuracyLanguage: string;
20+
},
21+
) {
22+
const query = options.search.trim().toLocaleLowerCase();
23+
const score = (model: EnvironmentSpeechModel) => {
24+
switch (options.sort) {
25+
case "size":
26+
return model.size;
27+
case "accuracy":
28+
return model.benchmark?.accuracy[options.accuracyLanguage]?.errorPercent ?? Infinity;
29+
case "speed":
30+
return model.benchmark?.secondsPer30s ?? Infinity;
31+
case "recommended":
32+
return modelSortOrder(model);
33+
}
34+
};
35+
return models
36+
.filter(
37+
(model) =>
38+
options.languages.every((language) => model.languages.includes(language)) &&
39+
(!options.downloadedOnly || model.state === "installed") &&
40+
(!query || (model.name + " " + model.description).toLocaleLowerCase().includes(query)),
41+
)
42+
.toSorted(
43+
(left, right) => score(left) - score(right) || modelSortOrder(left) - modelSortOrder(right),
44+
);
45+
}

0 commit comments

Comments
 (0)