forked from speechmatics/speechmatics-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_parser.py
More file actions
729 lines (676 loc) · 23.1 KB
/
cli_parser.py
File metadata and controls
729 lines (676 loc) · 23.1 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# (c) 2022, Cantab Research Ltd.
"""
Parsers used by the CLI to handle CLI arguments
"""
import argparse
import logging
from urllib.parse import urlparse
LOGGER = logging.getLogger(__name__)
class kvdictAppendAction(argparse.Action):
"""
argparse action to split an argument into KEY=VALUE form
on the first = and append to a dictionary.
"""
def __call__(self, parser, args, values, option_string=None):
for pair in values:
try:
(k, v) = pair.split("=", 2)
except ValueError:
raise argparse.ArgumentError(
self, f'could not parse argument "{pair}" as k=v format'
)
d = getattr(args, self.dest) or {}
d[k] = v
setattr(args, self.dest, d)
def additional_vocab_item(to_parse):
"""
Parses a single item of additional vocab. Used in conjunction with the
additional vocab command line argument.
:param to_parse: The item to parse.
:type to_parse: str
:return: Either a dictionary or a string depending on the form of the
additional vocab item.
:rtype: Union[dict, str]
:raises argparse.ArgumentTypeError: If the item to parse is invalid.
"""
to_parse = str(to_parse)
parts = to_parse.split(":")
if len(parts) > 2:
raise argparse.ArgumentTypeError(
f"Can't have more than one separator (:) in additional vocab: "
f"{to_parse}."
)
content = parts[0]
if not content:
raise argparse.ArgumentTypeError(
f"Additional vocab must have content in: {to_parse}"
)
if len(parts) == 1:
return content
additional_vocab = {"content": content, "sounds_like": []}
sounds_likes = parts[1].split(",")
for sounds_like in sounds_likes:
if not sounds_like:
continue
additional_vocab["sounds_like"].append(sounds_like)
if not additional_vocab["sounds_like"]:
del additional_vocab["sounds_like"]
return additional_vocab
# pylint: disable=too-many-locals
# pylint: disable=too-many-statements
def get_arg_parser():
"""
Creates a command-line argument parser objct
:return: The argparser object with all commands and subcommands.
:rtype: argparse.ArgumentParser
"""
parser = argparse.ArgumentParser(description="CLI for Speechmatics products.")
parser.add_argument(
"-v",
dest="verbose",
action="count",
default=0,
help=(
"Set the log level for verbose logs. "
"The number of flags indicate the level, eg. "
"-v is INFO and -vv is DEBUG."
),
)
parser.add_argument(
"--debug",
default=False,
action="store_true",
help=(
"Prints useful symbols to represent the messages on the wire. "
"Symbols are printed to STDERR, use only when STDOUT is "
"redirected to a file."
),
)
parser.add_argument(
"--ctrl",
type=str,
help="Speechmatics internal use.",
)
# Parsers for shared arguments.
# Parent parser for commands involving files
file_parser = argparse.ArgumentParser(add_help=False)
file_parser.add_argument(
"files", metavar="FILEPATHS", type=str, nargs="+", help="File(s) to process."
)
# Parent parser for shared connection parameters
connection_parser = argparse.ArgumentParser(add_help=False)
connection_parser.add_argument(
"--url",
type=str,
help=(
"Websocket for RT or for batch API URL (e.g. wss://192.168.8.12:9000/ "
"or https://asr.api.speechmatics.com/v2 respectively)."
),
)
connection_parser.add_argument(
"--auth-token",
type=str,
help=(
"Pre-shared authentication token to authorize the client."
"Applies to Speechmatics SaaS products."
),
)
connection_parser.add_argument(
"--ssl-mode",
default="regular",
choices=["regular", "insecure", "none"],
help=(
"Use a preset configuration for the SSL context. With `regular` "
"mode a valid certificate is expected. With `insecure` mode"
" a self signed certificate is allowed."
" With `none` then SSL is not used."
),
)
connection_parser.add_argument(
"--generate-temp-token",
default=None,
action="store_true",
help=(
"Automatically generate a temporary token for authentication."
"Enterprise customers should set this to False."
),
)
connection_parser.add_argument(
"--profile",
default="default",
type=str,
help=(
"Determines the local config toml profile to use."
"Profiles can be used to maintain multiple different user accounts and setups locally."
),
)
# Parent parser for shared params related to building a job config
config_parser = argparse.ArgumentParser(add_help=False)
config_parser.add_argument(
"--config-file",
dest="config_file",
type=str,
default=None,
help="Read the transcription config from a file."
" If you provide this, all other config options work as overrides.",
)
config_parser.add_argument(
"--lang",
"--language",
dest="language",
type=str,
default=None,
help="Language (ISO 639-1 code, e.g. en, fr, de).",
)
config_parser.add_argument(
"--volume-threshold",
dest="volume_threshold",
type=float,
default=None,
help=("Filter out quiet audio which falls below this threshold (0.0-100.0)"),
)
config_parser.add_argument(
"--remove-disfluencies",
default=None,
action="store_true",
required=False,
help=("Removes words tagged as disfluency."),
)
config_parser.add_argument(
"--operating-point",
choices=["standard", "enhanced"],
help=(
"Selects the acoustic model configuration. "
'"enhanced" is more computationally expensive than "standard" but '
"should result in a more accurate transcript."
),
)
config_parser.add_argument(
"--domain",
type=str,
default=None,
help="Optionally request a specialized language pack, e.g. 'finance'",
)
config_parser.add_argument(
"--output-locale",
metavar="LOCALE",
type=str,
default=None,
help="Locale of the output of transcripts. eg. en-US.",
)
config_parser.add_argument(
"--additional-vocab",
nargs="*",
type=additional_vocab_item,
help=(
"Space separated list of additional vocab. Expected format: "
"<content (required)>:<sounds like (optional)>,<anymore sounds "
"like> Simple vocab list example: 'Speechmatics gnocchi'. "
"Vocab list with sounds like example: 'gnocchi:nokey,nochi'."
),
)
config_parser.add_argument(
"--additional-vocab-file",
metavar="VOCAB_FILEPATH",
type=str,
help="File with additional vocab in JSON format.",
)
config_parser.add_argument(
"--punctuation-permitted-marks",
type=str,
default=None,
help=(
"Space-separated list of permitted punctuation marks for advanced punctuation."
),
)
config_parser.add_argument(
"--enable-entities",
default=False,
action="store_true",
help=(
"Whether to output additional information about recognised"
"entity classes (JSON output only)."
),
)
config_parser.add_argument(
"--translation-langs",
"--translation-languages",
dest="translation_target_languages",
type=str,
default=None,
help=("Comma-separated list of languages to translate the transcription into"),
)
config_parser.add_argument(
"--langid-langs",
"--langid-languages",
dest="langid_expected_languages",
type=str,
default=None,
help=("Comma-separated list of expected languages for language identification"),
)
# Parent parser for batch summarize argument
batch_summarization_parser = argparse.ArgumentParser(add_help=False)
batch_summarization_parser.add_argument(
"--summarize",
dest="summarize",
action="store_true",
default=False,
help="Whether to generate transcript summarization",
)
batch_summarization_parser.add_argument(
"--summary-content-type",
dest="content_type",
default=None,
choices=["informative", "conversational", "auto"],
type=str,
required=False,
)
batch_summarization_parser.add_argument(
"--summary-length",
dest="summary_length",
default=None,
choices=["brief", "detailed"],
type=str,
required=False,
)
batch_summarization_parser.add_argument(
"--summary-type",
dest="summary_type",
default=None,
choices=["paragraphs", "bullets"],
type=str,
required=False,
)
# Parent parser for batch sentiment-analysis argument
batch_sentiment_analysis_parser = argparse.ArgumentParser(add_help=False)
batch_sentiment_analysis_parser.add_argument(
"--sentiment-analysis",
dest="sentiment_analysis",
action="store_true",
default=False,
help="Perform sentiment analysis on the transcript",
)
# Parent parser for batch topic-detection argument
batch_topic_detection_parser = argparse.ArgumentParser(add_help=False)
batch_topic_detection_parser.add_argument(
"--detect-topics",
dest="detect_topics",
action="store_true",
default=False,
help="Whether to detect topics in transcript",
)
batch_topic_detection_parser.add_argument(
"--topics",
dest="topics",
default=None,
type=str,
required=False,
help="Comma-separated list of topics for topic detection",
)
# Parent parser for batch auto-chapters argument
batch_auto_chapters_parser = argparse.ArgumentParser(add_help=False)
batch_auto_chapters_parser.add_argument(
"--detect-chapters",
dest="detect_chapters",
action="store_true",
default=False,
help="Whether to detect chapters on the transcript",
)
# Parent parser for output type
output_format_parser = argparse.ArgumentParser(add_help=False)
output_format_parser.add_argument(
"--output-format",
default="txt",
choices=["txt", "json", "json-v2", "srt"],
type=str,
required=False,
help="Transcript output format.",
)
# Parent parser for batch diarization argument
batch_diarization_parser = argparse.ArgumentParser(add_help=False)
batch_diarization_parser.add_argument(
"--speaker-diarization-sensitivity",
type=float,
help="The sensitivity of the speaker detection. Default is 0.5 .",
)
batch_diarization_parser.add_argument(
"--diarization",
choices=["none", "speaker", "channel"],
help="Which type of diarization to use.",
)
batch_diarization_parser.add_argument(
"--channel-diarization-labels",
nargs="+",
help=(
"Add your own speaker or channel labels to the transcript. "
"Example usage: --channel-diarization-labels label1 label2"
),
)
# Parent parser for job_id argument
job_id_parser = argparse.ArgumentParser(add_help=False)
job_id_parser.add_argument("--job-id", type=str, required=True, help="Job ID.")
# Parent parser for RT transcribe arguments
rt_transcribe_command_parser = argparse.ArgumentParser(add_help=False)
rt_transcribe_command_parser.add_argument(
"--streaming-mode",
default=False,
action="store_true",
help="Whether to run the engine in streaming mode. Internal Speechmatics use only.",
)
rt_transcribe_command_parser.add_argument(
"--enable-partials",
default=False,
action="store_true",
help=(
"Whether to return partials for both transcripts and translation which can be updated by later,"
"final transcripts."
),
)
rt_transcribe_command_parser.add_argument(
"--enable-transcription-partials",
default=False,
action="store_true",
help=(
"Whether to return partial transcripts which can be updated by later,"
"final transcripts."
),
)
rt_transcribe_command_parser.add_argument(
"--enable-translation-partials",
default=False,
action="store_true",
help=(
"Whether to return partial translation which can be updated by later,"
"final translation."
),
)
rt_transcribe_command_parser.add_argument(
"--punctuation-sensitivity",
type=float,
help="Sensitivity level for advanced punctuation.",
)
rt_transcribe_command_parser.add_argument(
"--speaker-diarization-max-speakers",
type=int,
help="Enforces the maximum number of speakers allowed in a single audio stream. Min: 2, Max: 20, Default: 20.",
)
rt_transcribe_command_parser.add_argument(
"--max-delay",
type=float,
help="Maximum acceptable delay before sending a piece of transcript.",
)
rt_transcribe_command_parser.add_argument(
"--max-delay-mode",
default="flexible",
choices=["fixed", "flexible"],
type=str,
help=(
"How to interpret the max-delay size if speech is in"
"the middle of an unbreakable entity like a number."
),
)
rt_transcribe_command_parser.add_argument(
"--raw",
metavar="ENCODING",
type=str,
help=(
"Indicate that the input audio is raw, provide the encoding"
"of this raw audio, eg. pcm_f32le."
),
)
rt_transcribe_command_parser.add_argument(
"--sample-rate",
type=int,
default=44_100,
help="The sample rate in Hz of the input audio, if in raw format.",
)
rt_transcribe_command_parser.add_argument(
"--chunk-size",
type=int,
default=1024 * 4,
help=(
"How much audio data, in bytes, to send to the server in each "
"websocket message. Larger values can increase latency, but "
"values which are too small create unnecessary overhead."
),
)
rt_transcribe_command_parser.add_argument(
"--buffer-size",
default=512,
type=int,
help=(
"Maximum number of messages to send before waiting for"
"acknowledgements from the server."
),
)
rt_transcribe_command_parser.add_argument(
"--print-json",
default=False,
action="store_true",
help=(
"Print the JSON partial & final transcripts received rather than "
"plaintext messages."
),
)
rt_transcribe_command_parser.add_argument(
"--diarization",
choices=["none", "speaker"],
help="Which type of diarization to use.",
)
rt_transcribe_command_parser.add_argument(
"--audio-events",
action="store_true",
help="Enable audio event detection and print events in square-brackets to the console, e.g. [MUSIC]",
)
rt_transcribe_command_parser.add_argument(
"--event-types",
default=None,
type=str,
required=False,
help="Comma-separated list of whitelisted event types for audio events.",
)
rt_transcribe_command_parser.add_argument(
"--extra-headers",
default=dict(),
nargs="+",
action=kvdictAppendAction,
metavar="KEY=VALUE",
required=False,
help="Adds extra headers to the websocket client",
)
# Parent parser for batch auto-chapters argument
batch_audio_events_parser = argparse.ArgumentParser(add_help=False)
batch_audio_events_parser.add_argument(
"--audio-events",
action="store_true",
help="Enable audio event detection, "
"will include `audio_events` and `audio_event_summary` keys in output (JSON only)",
)
batch_audio_events_parser.add_argument(
"--event-types",
default=None,
type=str,
required=False,
help="Comma-separated list of whitelisted event types for audio events.",
)
# Build our actual parsers.
mode_subparsers = parser.add_subparsers(title="Mode", dest="mode")
# Parsers specific to rt mode commands.
rt_parser = mode_subparsers.add_parser("rt", help="Real-time commands")
rt_subparsers = rt_parser.add_subparsers(title="Commands", dest="command")
rt_subparsers.add_parser(
"transcribe",
parents=[
rt_transcribe_command_parser,
file_parser,
connection_parser,
config_parser,
],
help="Transcribe an audio file or stream in real time and output the results to the console.",
)
# Parsers specific to batch mode commands
batch_parser = mode_subparsers.add_parser("batch", help="Batch commands")
batch_subparsers = batch_parser.add_subparsers(title="Commands", dest="command")
batch_subparsers.add_parser(
"transcribe",
parents=[
file_parser,
connection_parser,
config_parser,
output_format_parser,
batch_diarization_parser,
batch_summarization_parser,
batch_sentiment_analysis_parser,
batch_topic_detection_parser,
batch_auto_chapters_parser,
batch_audio_events_parser,
],
help="Transcribe one or more audio files using batch mode, while waiting for results.",
)
batch_subparsers.add_parser(
"submit",
parents=[
file_parser,
connection_parser,
config_parser,
output_format_parser,
batch_diarization_parser,
batch_summarization_parser,
batch_sentiment_analysis_parser,
batch_topic_detection_parser,
batch_auto_chapters_parser,
],
help="Submit one or more files for transcription.",
)
batch_subparsers.add_parser(
"list-jobs",
parents=[connection_parser],
help="Retrieve json of last 100 jobs submitted within the last 7 days for the SaaS "
"or all of the jobs for the batch appliance",
)
batch_subparsers.add_parser(
"get-results",
parents=[connection_parser, output_format_parser, job_id_parser],
help="Retrieve results of a transcription job.",
)
batch_delete_parser = batch_subparsers.add_parser(
"delete",
parents=[connection_parser, output_format_parser, job_id_parser],
help="Delete the results of a transcription job.",
)
batch_delete_parser.add_argument(
"--force",
default=False,
action="store_true",
dest="force_delete",
help="Force deletion of a running job",
)
batch_subparsers.add_parser(
"job-status",
parents=[connection_parser, job_id_parser],
help="Retrieve status of a transcription job.",
)
# Parser for the "transcribe" command uses only RT.
mode_subparsers.add_parser(
"transcribe",
parents=[
rt_transcribe_command_parser,
file_parser,
connection_parser,
config_parser,
],
help="Real-time commands. RETAINED FOR LEGACY COMPATIBILITY.",
)
# Parser for config processes setting toml config
cli_config_parser = mode_subparsers.add_parser(
"config",
help="Config commands. Used to handle local config.",
)
cli_config_subparsers = cli_config_parser.add_subparsers(
title="commands", dest="command"
)
cli_set_config_parser = cli_config_subparsers.add_parser(
"set", help="Set config for the local CLI environment."
)
cli_set_config_parser.add_argument(
"--auth-token", type=str, help="Auth token to use as default for all requests."
)
cli_set_config_parser.add_argument(
"--realtime-url",
"--rt-url",
type=str,
dest="realtime_url",
help="Default URL to use for RT transcription. Overriden by the --url flag.",
)
cli_set_config_parser.add_argument(
"--batch-url",
type=str,
help="Default URL to use for Batch transcription. Overriden by the --url flag.",
)
cli_set_config_parser.add_argument(
"--generate-temp-token",
action="store_true",
help="Sets generate_temp_token to true in the config file."
"This will set the --generate-temp-token to true globally wherever it is a valid command line flag.",
)
cli_set_config_parser.add_argument(
"--profile",
type=str,
default="default",
help="Specifies the profile to set the config for."
"Profiles can be used to maintain multiple different sets of config locally.",
)
cli_unset_config_parser = cli_config_subparsers.add_parser(
"unset", help="Remove specified config values from the CLI config file."
)
cli_unset_config_parser.add_argument(
"--auth-token",
action="store_true",
help="If flag is set, removes the auth token value from the config file.",
)
cli_unset_config_parser.add_argument(
"--generate-temp-token",
action="store_true",
help="If flag is set, removes the generate temp token value from the config file.",
)
cli_unset_config_parser.add_argument(
"--profile",
type=str,
default="default",
help="Specifies the profile to unset the config for."
"Profiles can be used to maintain multiple different sets of config locally.",
)
cli_unset_config_parser.add_argument(
"--realtime-url",
"--rt-url",
dest="realtime_url",
action="store_true",
help="Remove the default URL to use for RT transcription.",
)
cli_unset_config_parser.add_argument(
"--batch-url",
action="store_true",
help="Remove the default URL to use for Batch transcription.",
)
return parser
def parse_args(args=None):
"""
Parses command-line arguments.
:param args: List of arguments to parse.
:type args: (List[str], optional)
:return: The set of arguments provided along with their values.
:rtype: Namespace
"""
parser = get_arg_parser()
parsed_args = parser.parse_args(args=args)
# Fix up args for transcribe command
if parsed_args.mode == "transcribe":
parsed_args.command = "transcribe"
if urlparse(parsed_args.url).scheme in ["ws", "wss"] or parsed_args.url is None:
parsed_args.mode = "rt"
else:
LOGGER.error(
"speechmatics [transcribe] mode is used only with RT for legacy compatibility, not batch."
)
args = vars(parse_args(["batch", "-h"]))
return parsed_args