Repository navigation
Expand file tree
/
Copy pathspeech.ts
More file actions
221 lines (211 loc) · 8.42 KB
/
Copy pathspeech.ts
File metadata and controls
221 lines (211 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {
SPEECH_MAX_OPTIONS_BYTES,
type SpeechCustomWords,
type SpeechBenchmarkTier,
SpeechTranscriptionOptions,
} from "@t3tools/contracts";
import * as Schema from "effect/Schema";
import recommendationPolicy from "./speechRecommendationPolicy.json" with { type: "json" };
const decodeOptions = Schema.decodeUnknownSync(Schema.fromJsonString(SpeechTranscriptionOptions));
const encodeOptions = Schema.encodeSync(Schema.fromJsonString(SpeechTranscriptionOptions));
export type SpeechRecommendationRole = "recommended" | "faster" | "accurate";
type RecommendationModel = {
readonly id: string;
readonly languages: readonly string[];
readonly benchmark?: {
readonly accuracy: Readonly<Record<string, { readonly errorPercent: number }>>;
readonly secondsPer30s: {
readonly cpu?: number | null;
readonly accelerated: number | null;
};
};
};
/** Reference accuracy/speed picks; no preference or incomplete measurements get no badge. */
export function recommendSpeechModels(
models: readonly RecommendationModel[],
languages: readonly string[],
tier: SpeechBenchmarkTier | undefined,
) {
const wanted = [...new Set(languages)];
const roles = new Map<string, SpeechRecommendationRole>();
if (wanted.length === 0 || tier === undefined) return roles;
const policy = recommendationPolicy;
const scored = models.flatMap((model) => {
if (!wanted.every((language) => model.languages.includes(language))) return [];
const errors = wanted.map((language) => model.benchmark?.accuracy[language]?.errorPercent);
const wait = model.benchmark?.secondsPer30s[tier];
if (
wait == null ||
!Number.isFinite(wait) ||
wait < 0 ||
errors.some((error) => error === undefined || !Number.isFinite(error) || error < 0)
)
return [];
const measured = errors as number[];
const error = Math.exp(
measured.reduce((sum, value) => sum + Math.log(value), 0) / measured.length,
);
return [
{
id: model.id,
error,
worst: Math.max(...measured),
wait,
cost: error + policy.waitWeight * Math.max(0, wait - policy.freeWaitSeconds),
},
];
});
if (scored.length === 0) return roles;
const useful = scored.filter((model) => model.worst < policy.maxLanguageErrorPercent);
const pool = useful.length ? useful : scored;
const lowest = (items: typeof scored, score: (item: (typeof scored)[number]) => number) =>
items.reduce<(typeof scored)[number] | undefined>(
(best, item) => (best === undefined || score(item) < score(best) ? item : best),
undefined,
);
const withinBudget = pool.filter((model) => model.wait <= policy.overallMaxWaitSeconds);
const best = (
useful.length
? lowest(withinBudget.length ? withinBudget : pool, (model) => model.cost)
: lowest(pool, (model) => model.worst + model.error / 1000)
)!;
const quicker = pool.filter((model) => model.wait < best.wait);
const fast = useful.length
? (lowest(
quicker.filter((model) => model.wait <= policy.fastMaxWaitSeconds),
(model) => model.error,
) ?? lowest(quicker, (model) => model.wait + model.error / 1000))
: undefined;
const accurate = useful.length
? lowest(
pool.filter(
(model) => model.error < best.error && model.wait <= policy.accurateMaxWaitSeconds,
),
(model) => model.error,
)
: undefined;
for (const [model, role] of [
[best, "recommended"],
[fast, "faster"],
[accurate, "accurate"],
] as const) {
if (model && !roles.has(model.id)) roles.set(model.id, role);
}
return roles;
}
// Only offer explicit hints understood by all cloud APIs; other languages can still be detected.
export const cloudSpeechLanguages = {
supportsLanguageDetection: true,
languages:
"ar bg ca cs da de el en es fa fi fr hi hr hu id it ja ko mk ms nl pl pt ro ru sk sv th tr uk ur vi zh".split(
" ",
),
};
/** Resolve the language the model can produce from the requested language. */
export function effectiveSpeechLanguage(
model: { readonly languages: readonly string[]; readonly supportsLanguageDetection: boolean },
intent: string,
): string {
if (model.languages.length === 1) return model.languages[0]!;
if (intent !== "auto" && model.languages.includes(intent)) return intent;
if (model.supportsLanguageDetection) return "auto";
return model.languages.find((language) => language === "en") ?? model.languages[0] ?? "auto";
}
/**
* The model first-run setup offers: the selected one, else the benchmark pick, else any that the
* server would really transcribe the person's language with. Without a language hint,
* offer only the selected model, without a recommendation.
*/
export function chooseSetupSpeechModel<
Model extends RecommendationModel & {
readonly supportsLanguageDetection: boolean;
},
>(
models: readonly Model[],
input: {
readonly selectedModelId: string;
readonly intent: string;
readonly preferred: readonly string[];
readonly benchmarkTier: SpeechBenchmarkTier | undefined;
},
) {
if (input.intent === "auto" && input.preferred.length === 0) {
const model = models.find(({ id }) => id === input.selectedModelId);
return model && { model, language: null, recommended: false };
}
for (const language of input.intent === "auto" ? input.preferred : [input.intent]) {
const capable = models.filter((model) => {
const effective = effectiveSpeechLanguage(model, input.intent);
return effective === language || (effective === "auto" && model.languages.includes(language));
});
const recommendedId = recommendSpeechModels(capable, [language], input.benchmarkTier)
.keys()
.next().value;
const model =
capable.find(({ id }) => id === input.selectedModelId) ??
capable.find(({ id }) => id === recommendedId) ??
capable[0];
if (model) return { model, language, recommended: model.id === recommendedId };
}
return undefined;
}
const vocabularyKey = (value: string) => value.trim().normalize("NFC").toLocaleLowerCase();
/**
* Project spellings win over personal ones. Hint terms, such as the project name or the
* correction cue, come first so the 100-entry wire limit cannot drop them.
*/
export function resolveSpeechVocabulary(input: {
readonly personal: SpeechCustomWords;
readonly project: SpeechCustomWords;
readonly hints?: ReadonlyArray<string>;
}): SpeechCustomWords {
const known = new Set(
[...input.project, ...input.personal].flatMap(({ term, aliases }) =>
[term, ...aliases].map(vocabularyKey),
),
);
// A hint that is already a dictionary entry keeps that entry and its aliases in place.
const hints = (input.hints ?? []).flatMap((hint) => {
const term = hint.replace(/\s+/g, " ").trim();
return term.length > 0 && term.length <= 50 && !known.has(vocabularyKey(term))
? [{ term, aliases: [] }]
: [];
});
const seen = new Set<string>();
return [...hints, ...input.project, ...input.personal]
.flatMap(({ term, aliases }) => {
if (seen.has(vocabularyKey(term))) return [];
seen.add(vocabularyKey(term));
const uniqueAliases = aliases.filter((alias) => {
if (seen.has(vocabularyKey(alias))) return false;
seen.add(vocabularyKey(alias));
return true;
});
return [{ term, aliases: uniqueAliases }];
})
.slice(0, 100);
}
/** Binary requests contain a four-byte JSON length, UTF-8 options, then unencoded PCM. */
export function encodeSpeechPcmRequest(
pcm: Uint8Array,
options: SpeechTranscriptionOptions,
): Uint8Array<ArrayBuffer> {
const metadata = new TextEncoder().encode(encodeOptions(options));
if (metadata.byteLength > SPEECH_MAX_OPTIONS_BYTES)
throw new Error("Speech options are too large.");
const body = new Uint8Array(4 + metadata.byteLength + pcm.byteLength);
new DataView(body.buffer).setUint32(0, metadata.byteLength, true);
body.set(metadata, 4);
body.set(pcm, 4 + metadata.byteLength);
return body;
}
export function decodeSpeechPcmRequest(body: Uint8Array) {
if (body.byteLength < 4) throw new Error("Missing speech options.");
const length = new DataView(body.buffer, body.byteOffset, body.byteLength).getUint32(0, true);
if (length > SPEECH_MAX_OPTIONS_BYTES || 4 + length >= body.byteLength)
throw new Error("Invalid speech options length.");
const options = decodeOptions(
new TextDecoder("utf-8", { fatal: true }).decode(body.subarray(4, 4 + length)),
);
return { pcm: body.subarray(4 + length), options };
}