Open History to see the last commit
1import type { EnvironmentSpeechModel, SpeechBenchmarkTier } from "@t3tools/contracts";
2
3export type SpeechModelSort = "recommended" | "size" | "accuracy" | "speed";
4
5const 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
12export 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 readonly benchmarkTier: SpeechBenchmarkTier;
21 },
22) {
23 const query = options.search.trim().toLocaleLowerCase();
24 const score = (model: EnvironmentSpeechModel) => {
25 switch (options.sort) {
26 case "size":
27 return model.size;
28 case "accuracy":
29 return model.benchmark?.accuracy[options.accuracyLanguage]?.errorPercent ?? Infinity;
30 case "speed":
31 return model.benchmark?.secondsPer30s[options.benchmarkTier] ?? Infinity;
32 case "recommended":
33 return modelSortOrder(model);
34 }
35 };
36 return models
37 .filter(
38 (model) =>
39 options.languages.every((language) => model.languages.includes(language)) &&
40 (!options.downloadedOnly || model.state === "installed") &&
41 (!query || (model.name + " " + model.description).toLocaleLowerCase().includes(query)),
42 )
43 .toSorted(
44 (left, right) => score(left) - score(right) || modelSortOrder(left) - modelSortOrder(right),
45 );
46}