forked from gjwgit/openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcribe.py
68 lines (55 loc) · 2.31 KB
/
transcribe.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
# -*- coding: utf-8 -*-
#
# MLHub toolket for OpenAI - Transcribe
#
# Time-stamp: <Sunday 2023-11-26 14:06:57 +1100 Graham Williams>
#
# Author: [email protected], Ting Tang
# Licensed under GPLv3.
# Copyright (c) Togaware Pty Ltd. All rights reserved.
#
# ml transcribe openai
# ----------------------------------------------------------------------
# Setup
# ----------------------------------------------------------------------
import click
from audio_processing import process_audio
from whisper.tokenizer import LANGUAGES
from whisper.tokenizer import TO_LANGUAGE_CODE
# -----------------------------------------------------------------------
# Command line argument and options
# -----------------------------------------------------------------------
@click.command()
@click.argument("filename",
default=None,
required=False,
type=click.STRING)
@click.option("-l", "--lang",
default=None,
type=click.Choice(sorted(LANGUAGES.keys()) + sorted([k.title() for k in TO_LANGUAGE_CODE.keys()]), case_sensitive=True),
help="The language of the source audio.")
@click.option("-f", "--format",
default=None,
type=click.Choice(["txt", "vtt", "srt", "tsv", "json"], case_sensitive=True),
help="The format of the output. Supported formats are txt, json, srt, tsv, and vtt.")
@click.option("-o", "--output",
default=None,
type=click.STRING,
help="The name and format of the output file. e.g. output.txt, tmp.vtt")
def cli(filename, lang, format, output):
"""
Transcribe audio from a file.
Tested with mp3, wav, mp4, mov.
The audio is processed locally using a downloaded OpenAI model The
result is returned as text.
Use the `-l` or `--lang` option to specify the language of the source audio.
Use the `-f` or `--format` option to specify the desired output format.
Supported formats are txt, json, srt, tsv, and vtt.
(e.g. `-f txt`).
To save the transcribed text to a file,
Use the `-o` or `--output` option to specify the desired output file name
and format (e.g. `-o output.txt`),
"""
process_audio(filename, lang, format, output, task="transcribe")
if __name__ == "__main__":
cli(prog_name="transcribe")