-
Notifications
You must be signed in to change notification settings - Fork 8
/
transcriber.py
466 lines (445 loc) · 13.4 KB
/
transcriber.py
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
import json
import logging
import tempfile
import traceback
import click
from app import (
__app_name__,
__version__,
commands,
utils
)
from app.config import config
from app.logging import configure_logger, get_logger
from app.transcription import Transcription
from app.types import GitHubMode
logger = get_logger()
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo(f"{__app_name__} v{__version__}")
ctx.exit()
@click.option(
"-v",
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Show the application's version and exit.",
)
@click.group()
def cli():
pass
def print_help(ctx, param, value):
if not value or ctx.resilient_parsing:
return
logging.info(ctx.get_help())
ctx.exit()
whisper = click.option(
"-m",
"--model",
type=click.Choice(
[
"tiny",
"tiny.en",
"base",
"base.en",
"small",
"small.en",
"medium",
"medium.en",
"large-v2",
]
),
default=config.get('model', 'tiny.en'),
show_default=True,
help="Select which whisper model to use for the transcription",
)
deepgram = click.option(
"-D",
"--deepgram",
is_flag=True,
default=config.getboolean('deepgram', False),
help="Use deepgram for transcription",
)
diarize = click.option(
"-M",
"--diarize",
is_flag=True,
default=config.getboolean('diarize', False),
help="Supply this flag if you have multiple speakers AKA "
"want to diarize the content",
)
summarize = click.option(
"-S",
"--summarize",
is_flag=True,
default=config.getboolean('summarize', False),
help="Summarize the transcript [only available with deepgram]",
)
cutoff_date = click.option(
"--cutoff-date",
type=str,
default=config.get('cutoff_date', None),
help=("Specify a cutoff date (in YYYY-MM-DD format) to process only sources "
"published after this date. Sources with a publication date on or before "
"the cutoff will be excluded from processing. This option is useful for "
"focusing on newer content or limiting the scope of processing to a "
"specific date range.")
)
github = click.option(
"--github",
type=click.Choice(["remote", "local", "none"]),
default=config.get('github', 'none'),
help=("Specify the GitHub operation mode."
"'remote': Create a new branch, push changes to it, and push it to the origin bitcointranscripts repo. "
"'local': Commit changes to the current local branch without pushing to the remote repo."
"'none': Do not perform any GitHub operations."),
show_default=True
)
upload_to_s3 = click.option(
"-u",
"--upload",
is_flag=True,
default=config.getboolean('upload_to_s3', False),
help="Upload processed model files to AWS S3",
)
save_to_markdown = click.option(
"--markdown",
is_flag=True,
default=config.getboolean('save_to_markdown', False),
help="Save the resulting transcript to a markdown format supported by bitcointranscripts",
)
noqueue = click.option(
"--noqueue",
is_flag=True,
default=config.getboolean('noqueue', False),
help="Do not push the resulting transcript to the Queuer backend",
)
needs_review = click.option(
"--needs-review",
is_flag=True,
default=config.getboolean('needs_review', False),
help="Add 'needs review' flag to the resulting transcript",
)
model_output_dir = click.option(
"-o",
"--model_output_dir",
type=str,
default=config.get('model_output_dir', 'local_models/'),
show_default=True,
help="Set the directory for saving model outputs",
)
nocleanup = click.option(
"--nocleanup",
is_flag=True,
default=config.getboolean('nocleanup', False),
help="Do not remove temp files on exit",
)
verbose_logging = click.option(
"-V",
"--verbose",
is_flag=True,
default=config.getboolean('verbose_logging', False),
help="Supply this flag to enable verbose logging",
)
add_loc = click.option(
"--loc",
default="misc",
help="Add the location in the bitcointranscripts hierarchy that you want to associate the transcript with",
)
add_title = click.option(
"-t",
"--title",
type=str,
help="Add the title for the resulting transcript (required for audio files)",
)
add_date = click.option(
"-d",
"--date",
type=str,
help="Add the event date to transcript's metadata in format 'yyyy-mm-dd'",
)
add_tags = click.option(
"-T",
"--tags",
multiple=True,
help="Add a tag to transcript's metadata (can be used multiple times)",
)
add_speakers = click.option(
"-s",
"--speakers",
multiple=True,
help="Add a speaker to the transcript's metadata (can be used multiple times)",
)
add_category = click.option(
"-c",
"--category",
multiple=True,
help="Add a category to the transcript's metadata (can be used multiple times)",
)
@cli.command()
@click.argument("source", nargs=1)
# Available transcription models and services
@whisper
@deepgram
# Available features for transcription services
@diarize
@summarize
# Options for adding metadata
@add_title
@add_date
@add_tags
@add_speakers
@add_category
@add_loc
# Options for configuring the transcription preprocess
@cutoff_date
# Options for configuring the transcription postprocess
@github
@upload_to_s3
@save_to_markdown
@noqueue
@needs_review
# Configuration options
@model_output_dir
@nocleanup
@verbose_logging
def transcribe(
source: str,
loc: str,
model: str,
title: str,
date: str,
tags: list,
speakers: list,
category: list,
github: GitHubMode,
deepgram: bool,
summarize: bool,
diarize: bool,
upload: bool,
verbose: bool,
model_output_dir: str,
nocleanup: bool,
noqueue: bool,
markdown: bool,
needs_review: bool,
cutoff_date: str,
) -> None:
"""Transcribe the provided sources. Suported sources include: \n
- YouTube videos and playlists\n
- Local and remote audio files\n
- JSON files containing individual sources\n
Notes:\n
- The https links need to be wrapped in quotes when running the command
on zsh\n
- The JSON can be generated by `preprocess-sources` or created manually
"""
tmp_dir = tempfile.mkdtemp()
configure_logger(logging.DEBUG if verbose else logging.INFO, tmp_dir)
logger.info(
"This tool will convert Youtube videos to mp3 files and then "
"transcribe them to text using Whisper. "
)
try:
transcription = Transcription(
model=model,
github=github,
summarize=summarize,
deepgram=deepgram,
diarize=diarize,
upload=upload,
model_output_dir=model_output_dir,
nocleanup=nocleanup,
queue=not noqueue,
markdown=markdown,
needs_review=needs_review,
working_dir=tmp_dir
)
if source.endswith(".json"):
transcription.add_transcription_source_JSON(source)
else:
transcription.add_transcription_source(
source_file=source,
loc=loc,
title=title,
date=date,
tags=list(tags),
category=list(category),
speakers=list(speakers),
cutoff_date=cutoff_date
)
transcription.start()
if nocleanup:
logger.info("Not cleaning up temp files...")
else:
transcription.clean_up()
except Exception as e:
logger.error(e)
logger.info(f"Exited with error, not cleaning up temp files: {tmp_dir}")
traceback.print_exc()
@cli.command()
@click.argument("source", nargs=1)
# Options for configuring the transcription preprocess
@cutoff_date
@click.option(
"--nocheck",
is_flag=True,
default=False,
help="Do not check for existing sources using btctranscripts.com/status.json",
)
@click.option(
"--no-batched-output",
is_flag=True,
default=False,
help="Output preprocessing output in a different JSON file for each source",
)
# Options for adding metadata
@add_title
@add_date
@add_tags
@add_speakers
@add_category
@add_loc
def preprocess(
source: str,
loc: str,
title: str,
date: str,
tags: list,
speakers: list,
category: list,
nocheck: bool,
no_batched_output: bool,
cutoff_date: str
):
"""Preprocess the provided sources. Suported sources include: \n
- YouTube videos and playlists\n
- JSON files containing individual sources\n
Preprocessing will fetch all the given sources, and output them
in a JSON alongside the available metadata.
The JSON can then be edited and piped to `transcribe`
"""
try:
configure_logger(log_level=logging.INFO)
logger.info(f"Preprocessing sources...")
transcription = Transcription(
queue=False,
batch_preprocessing_output=not no_batched_output)
if source.endswith(".json"):
transcription.add_transcription_source_JSON(source, nocheck=nocheck)
else:
transcription.add_transcription_source(
source_file=source,
loc=loc,
title=title,
date=date,
tags=tags,
category=category,
speakers=speakers,
preprocess=True,
nocheck=nocheck,
cutoff_date=cutoff_date
)
if not no_batched_output:
# Batch write all preprocessed sources to JSON
utils.write_to_json([preprocessed_source for preprocessed_source in transcription.preprocessing_output],
transcription.model_output_dir, "preprocessed_sources")
except Exception as e:
logger.info(f"Exited with error: {e}")
@cli.command()
@click.argument(
"service",
nargs=1,
type=click.Choice(
[
"whisper",
"deepgram"
]
)
)
@click.argument("metadata_json_file", nargs=1)
# Options for configuring the transcription postprocess
@github
@upload_to_s3
@save_to_markdown
@noqueue
@needs_review
def postprocess(
metadata_json_file,
service,
github: GitHubMode,
upload: bool,
markdown: bool,
noqueue: bool,
needs_review: bool,
):
"""Postprocess the output of a transcription service.
Requires the metadata JSON file that is the output of the previous stage
of the transcription process.
"""
try:
configure_logger(log_level=logging.INFO)
utils.check_if_valid_file_path(metadata_json_file)
transcription = Transcription(
deepgram=service == "deepgram",
github=github,
upload=upload,
markdown=markdown,
queue=not noqueue,
needs_review=needs_review,
)
logger.info(
f"Postprocessing {service} transcript from {metadata_json_file}")
with open(metadata_json_file, "r") as outfile:
metadata_json = json.load(outfile)
metadata = utils.configure_metadata_given_from_JSON(
metadata_json, from_json=metadata_json_file)
transcription.add_transcription_source(
source_file=metadata["source_file"],
loc=metadata["loc"],
title=metadata["title"],
category=metadata["category"],
tags=metadata["tags"],
speakers=metadata["speakers"],
date=metadata["date"],
summary=metadata["summary"],
episode=metadata["episode"],
additional_resources=metadata["additional_resources"],
youtube_metadata=metadata["youtube_metadata"],
chapters=metadata["chapters"],
link=metadata["media"],
preprocess=False,
nocheck=True,
cutoff_date=metadata["cutoff_date"]
)
# Finalize transcription service output
transcript_to_postprocess = transcription.transcripts[0]
if metadata.get("deepgram_chunks"):
logger.info("Combining deepgram chunk outputs...")
all_chunks_output = []
for chunk_file in metadata["deepgram_chunks"]:
with open(chunk_file, "r") as chunk:
all_chunks_output.append(json.load(chunk))
overlap_between_chunks = 30.0 # or any other value used during splitting
transcription_service_output = transcription.service.combine_chunk_outputs(
all_chunks_output, overlap=overlap_between_chunks)
transcript_to_postprocess.transcription_service_output_file = transcription.service.write_to_json_file(
transcription_service_output, transcript_to_postprocess)
else:
transcript_to_postprocess.transcription_service_output_file = metadata[
f"{service}_output"]
transcript_to_postprocess.result = transcription.service.finalize_transcript(
transcript_to_postprocess)
postprocessed_transcript = transcription.postprocess(
transcript_to_postprocess)
if transcription.bitcointranscripts_dir:
transcription.push_to_github([postprocessed_transcript])
except Exception as e:
logger.error(e)
traceback.print_exc()
cli.add_command(commands.queue)
if __name__ == '__main__':
cli()