-
Notifications
You must be signed in to change notification settings - Fork 82
/
WhisperProcessorBuilder.cs
executable file
·508 lines (467 loc) · 19.5 KB
/
WhisperProcessorBuilder.cs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Licensed under the MIT license: https://opensource.org/licenses/MIT
using Whisper.net.SamplingStrategy;
namespace Whisper.net;
/// <summary>
/// Builder for <see cref="WhisperProcessor"/>.
/// </summary>
public class WhisperProcessorBuilder
{
private readonly WhisperProcessorOptions whisperProcessorOptions;
internal WhisperProcessorBuilder(IntPtr context)
{
whisperProcessorOptions = new WhisperProcessorOptions()
{
ContextHandle = context
};
}
/// <summary>
/// Configures the processor to use the specified number of threads.
/// </summary>
/// <param name="threads">The number of threads to be used during encoding and decoding.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the same number as the hardware threads that the underlying hardware can support concurrently is used.
/// </remarks>
public WhisperProcessorBuilder WithThreads(int threads)
{
whisperProcessorOptions.Threads = threads;
return this;
}
/// <summary>
/// Configures the processor with the max number of tokens to be used from the previous text as prompt for the decoder.
/// </summary>
/// <param name="maxLastTextTokens">The max number of tokens to be used.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, a number of 16384 tokens is used.
/// </remarks>
public WhisperProcessorBuilder WithMaxLastTextTokens(int maxLastTextTokens)
{
whisperProcessorOptions.MaxLastTextTokens = maxLastTextTokens;
return this;
}
/// <summary>
/// Configures the processor with the start time in the audio from which it starts the processing.
/// </summary>
/// <param name="offset">Offset in the audio.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processing is happening from the beginning.
/// </remarks>
public WhisperProcessorBuilder WithOffset(TimeSpan offset)
{
whisperProcessorOptions.Offset = offset;
return this;
}
/// <summary>
/// Configures the processor with the duration of the audio to be processed.
/// </summary>
/// <param name="duration">Duration to be processed</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processing is happening for the entire input.
/// </remarks>
public WhisperProcessorBuilder WithDuration(TimeSpan duration)
{
whisperProcessorOptions.Duration = duration;
return this;
}
/// <summary>
/// Configures the processor to translate the text to English.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will just transcribe it.
/// </remarks>
public WhisperProcessorBuilder WithTranslate()
{
whisperProcessorOptions.Translate = true;
return this;
}
/// <summary>
/// Configures the processor to not use past transformation (if any) as the initial prompt for a newer processing.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor use part transformations as initial prompt for newer processing.
/// </remarks>
public WhisperProcessorBuilder WithNoContext()
{
whisperProcessorOptions.NoContext = true;
return this;
}
/// <summary>
/// Configures the processor to force a single segment as output instead of multiple.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will return multiple segments (if they are recognized).
/// </remarks>
public WhisperProcessorBuilder WithSingleSegment()
{
whisperProcessorOptions.SingleSegment = true;
return this;
}
/// <summary>
/// Configures the processor to print special tokens (e.g. <SOT>, <EOT>, <BEG>, etc.)
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will not print special tokens.
/// </remarks>
public WhisperProcessorBuilder WithPrintSpecialTokens()
{
whisperProcessorOptions.PrintSpecialTokens = true;
return this;
}
/// <summary>
/// Configures the processor to print progress information.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will not print progress information.
/// </remarks>
public WhisperProcessorBuilder WithPrintProgress()
{
whisperProcessorOptions.PrintProgress = true;
return this;
}
/// <summary>
/// Configures the processor to print results to stdout.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will not print results to stdout.
/// </remarks>
public WhisperProcessorBuilder WithPrintResults()
{
whisperProcessorOptions.PrintResults = true;
return this;
}
/// <summary>
/// Configures the processor to print timestamps for each segment to stdout.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// This option is available only if <seealso cref="WithPrintResults"/> is configured.
/// If not specified, the processor will print also timestamps.
/// </remarks>
public WhisperProcessorBuilder WithPrintTimestamps(bool printTimestamps = true)
{
whisperProcessorOptions.PrintTimestamps = printTimestamps;
return this;
}
/// <summary>
/// [EXPERIMENTAL] Configures the processor to use token-level timestamps.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will not use token timestamps.
/// </remarks>
public WhisperProcessorBuilder WithTokenTimestamps()
{
whisperProcessorOptions.UseTokenTimestamps = true;
return this;
}
/// <summary>
/// Configures the processor to use the specified probability threshold for token timestamps.
/// </summary>
/// <param name="tokenTimestampsThreshold">Probability threshold to be used for token-level timestamps.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Default value is 0.01f.
/// This option have effect only together with <seealso cref="WithTokenTimestamps"/>
/// </remarks>
public WhisperProcessorBuilder WithTokenTimestampsThreshold(float tokenTimestampsThreshold)
{
whisperProcessorOptions.TokenTimestampsThreshold = tokenTimestampsThreshold;
return this;
}
/// <summary>
/// Configures the processor to use the specified SUM probability threshold for token timestamps.
/// </summary>
/// <param name="tokenTimestampsSumThreshold">Probability SUM threshold to be used for token-level timestamps.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Default value is 0.01f.
/// This option have effect only together with <seealso cref="WithTokenTimestamps"/>
/// </remarks>
public WhisperProcessorBuilder WithTokenTimestampsSumThreshold(float tokenTimestampsSumThreshold)
{
whisperProcessorOptions.TokenTimestampsSumThreshold = tokenTimestampsSumThreshold;
return this;
}
/// <summary>
/// Configures the processor to use a maximum segment length.
/// </summary>
/// <param name="maxSegmentLength">The maximum segment length to be used</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified no max segment length will be applied.
/// </remarks>
public WhisperProcessorBuilder WithMaxSegmentLength(int maxSegmentLength)
{
whisperProcessorOptions.MaxSegmentLength = maxSegmentLength;
return this;
}
/// <summary>
/// Configures the processor to split on each word.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified split will be done based on the model configuration.
/// </remarks>
public WhisperProcessorBuilder SplitOnWord()
{
whisperProcessorOptions.SplitOnWord = true;
return this;
}
/// <summary>
/// Configures the processor to use a maximum tokens per segment.
/// </summary>
/// <param name="maxTokensPerSegment">The maximum number of tokens to be used</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified no max number of tokens will be applied.
/// </remarks>
public WhisperProcessorBuilder WithMaxTokensPerSegment(int maxTokensPerSegment)
{
whisperProcessorOptions.MaxTokensPerSegment = maxTokensPerSegment;
return this;
}
/// <summary>
/// [EXPERIMENTAL] Configures the processor to speed up the audio 2x for faster recognition.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Quality might be degraded while performance might be improved.
/// </remarks>
public WhisperProcessorBuilder WithSpeedUp2x()
{
whisperProcessorOptions.SpeedUp2x = true;
return this;
}
/// <summary>
/// [EXPERIMENTAL] Configures the processor to override the audio context size.
/// </summary>
/// <param name="audioContextSize">Audio context size to be overridden</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Quality might be degraded while performance might be improved.
/// </remarks>
public WhisperProcessorBuilder WithAudioContextSize(int audioContextSize)
{
whisperProcessorOptions.AudioContextSize = audioContextSize;
return this;
}
/// <summary>
/// [EXPERIMENTAL] Configures the processor to use an initial prompt, which will be prepended to any existing text context.
/// </summary>
/// <param name="prompt">The prompt to be used.</param>
/// <returns>An instance to the same builder.</returns>
public WhisperProcessorBuilder WithPrompt(string prompt)
{
whisperProcessorOptions.Prompt = prompt;
return this;
}
/// <summary>
/// Configures the processor with the language to be used for detection.
/// </summary>
/// <param name="language">The language (2 letters) to be used.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Default value is "en".
/// Example: "en", "ro"
/// </remarks>
public WhisperProcessorBuilder WithLanguage(string language)
{
whisperProcessorOptions.Language = language;
return this;
}
/// <summary>
/// Configures the processor to auto-detect the language based on initial samples.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Note: Processing time will slightly increase.
/// </remarks>
public WhisperProcessorBuilder WithLanguageDetection()
{
whisperProcessorOptions.Language = string.Empty;
return this;
}
/// <summary>
/// Configures the processor to NOT suppress blank outputs.
/// </summary>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, blanks are automatically suppressed.
/// </remarks>
public WhisperProcessorBuilder WithoutSuppressBlank()
{
whisperProcessorOptions.SuppressBlank = false;
return this;
}
/// <summary>
/// Configures the temperature for the processor.
/// </summary>
/// <param name="temperature">The temperature value to be used</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, default value is: 0.2f.
/// More details: https://ai.stackexchange.com/questions/32477/what-is-the-temperature-in-the-gpt-models/32478#32478
/// </remarks>
public WhisperProcessorBuilder WithTemperature(float temperature)
{
whisperProcessorOptions.Temperature = temperature;
return this;
}
/// <summary>
/// Configures the processor with a value indicating that the initial timestamp cannot be later than this.
/// </summary>
/// <param name="maxInitialTs">The initial max timestamp.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, default value is: 1f.
/// </remarks>
public WhisperProcessorBuilder WithMaxInitialTs(float maxInitialTs)
{
whisperProcessorOptions.MaxInitialTs = maxInitialTs;
return this;
}
/// <summary>
/// Configures the processor with a value indicating the length penalty (alpha).
/// </summary>
/// <param name="lengthPenalty">The initial max timestamp.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// If not specified, the processor will use simple length normalization by default.
/// More information about the length penalty can be found here: https://arxiv.org/abs/1609.08144.
/// </remarks>
public WhisperProcessorBuilder WithLengthPenalty(float lengthPenalty)
{
whisperProcessorOptions.LengthPenalty = lengthPenalty;
return this;
}
/// <summary>
/// Configures the processor with a temperature to increase when falling back.
/// </summary>
/// <param name="temperature">The temperature to increase when falling back.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Falling back can happen when the decoding fails to meet either of the thresholds in: <seealso cref="WithEntropyThreshold(float)"/>, <seealso cref="WithLogProbThreshold(float)"/> or <seealso cref="WithNoSpeechThreshold(float)"/>.
/// Default value is 0.2f.
/// </remarks>
public WhisperProcessorBuilder WithTemperatureInc(float temperature)
{
whisperProcessorOptions.TemperatureInc = temperature;
return this;
}
/// <summary>
/// Configures the processor with the entropy threshold for falling back.
/// </summary>
/// <param name="entropyThreshold">The entropy threshold</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Default value is 2.4f.
/// </remarks>
public WhisperProcessorBuilder WithEntropyThreshold(float entropyThreshold)
{
whisperProcessorOptions.EntropyThreshold = entropyThreshold;
return this;
}
/// <summary>
/// Configures the processor with a average log probability threshold over sampled tokens.
/// </summary>
/// <param name="logProbThreshold">The average log probability threshold.</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Default value is -1.0f.
/// </remarks>
public WhisperProcessorBuilder WithLogProbThreshold(float logProbThreshold)
{
whisperProcessorOptions.LogProbThreshold = logProbThreshold;
return this;
}
/// <summary>
/// [EXPERIMENTAL] Configures the processor with a no_speech probability.
/// </summary>
/// <param name="noSpeechThreshold">The no_speech probability</param>
/// <returns>An instance to the same builder.</returns>
/// <remarks>
/// Default value is 0.6f.
/// Note: Not implemented in native code.
/// </remarks>
public WhisperProcessorBuilder WithNoSpeechThreshold(float noSpeechThreshold)
{
whisperProcessorOptions.NoSpeechThreshold = noSpeechThreshold;
return this;
}
/// <summary>
/// Adds a <seealso cref="OnSegmentEventHandler"/> which will be called every time a new segment is detected.
/// </summary>
/// <param name="segmentEventHandler">The event handler to be added.</param>
/// <returns>An instance to the same builder.</returns>
public WhisperProcessorBuilder WithSegmentEventHandler(OnSegmentEventHandler segmentEventHandler)
{
whisperProcessorOptions.OnSegmentEventHandlers.Add(segmentEventHandler);
return this;
}
/// <summary>
/// Adds a <seealso cref="OnProgressHandler"/> which will report the progress in percentage.
/// </summary>
/// <param name="progressHandler">The event handler to be added.</param>
/// <returns>An instance to the same builder.</returns>
public WhisperProcessorBuilder WithProgressHandler(OnProgressHandler progressHandler)
{
whisperProcessorOptions.OnProgressHandlers.Add(progressHandler);
return this;
}
/// <summary>
/// Adds a <seealso cref="OnEncoderBeginEventHandler"/> which will be called when encoder will begin.
/// </summary>
/// <param name="encoderBeginEventHandler">The event handler to be added.</param>
/// <returns>An instance to the same builder.</returns>
public WhisperProcessorBuilder WithEncoderBeginHandler(OnEncoderBeginEventHandler encoderBeginEventHandler)
{
whisperProcessorOptions.OnEncoderBeginEventHandlers.Add(encoderBeginEventHandler);
return this;
}
/// <summary>
/// Configures the processor to use the Greedy Sampling strategy.
/// </summary>
/// <returns>A new <seealso cref="GreedySamplingStrategyBuilder"/> for configuring the <seealso cref="GreedySamplingStrategy"/></returns>
public IWhisperSamplingStrategyBuilder WithGreedySamplingStrategy()
{
var greedySamplingStrategy = new GreedySamplingStrategy();
whisperProcessorOptions.SamplingStrategy = greedySamplingStrategy;
return new GreedySamplingStrategyBuilder(this, greedySamplingStrategy);
}
/// <summary>
/// Configures the processor to use the Beam Search Sampling Strategy.
/// </summary>
/// <returns>A new <seealso cref="BeamSearchSamplingStrategyBuilder"/> for configuring the <seealso cref="BeamSearchSamplingStrategy"/></returns>
public IWhisperSamplingStrategyBuilder WithBeamSearchSamplingStrategy()
{
var beamSearchSamplingStrategy = new BeamSearchSamplingStrategy();
whisperProcessorOptions.SamplingStrategy = beamSearchSamplingStrategy;
return new BeamSearchSamplingStrategyBuilder(this, beamSearchSamplingStrategy);
}
/// <summary>
/// Confiugres the processor to return probabilities during segment decoding <seealso cref="SegmentData.MaxProbability"/>, <seealso cref="SegmentData.MinProbability"/> and <seealso cref="SegmentData.Probability"/>.
/// </summary>
/// <returns>An instance to the same builder.</returns>
public WhisperProcessorBuilder WithProbabilities()
{
whisperProcessorOptions.ComputeProbabilities = true;
return this;
}
/// <summary>
/// Builds the processor.
/// </summary>
/// <returns>The <seealso cref="WhisperProcessor"/> build with these configs.</returns>
public WhisperProcessor Build()
{
return new WhisperProcessor(whisperProcessorOptions);
}
}