Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

speech.ts

225 lines8.8 KB

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