generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# References | ||
|
||
- [openai/whisper](https://github.com/openai/whisper) | ||
- [Improve --model argument handling and help message #1764](https://github.com/openai/whisper/pull/1764) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import argparse | ||
import logging | ||
|
||
import whisper | ||
from dotenv import load_dotenv | ||
|
||
|
||
def init_args() -> argparse.Namespace: | ||
parser = argparse.ArgumentParser( | ||
prog="whisper_transcription", | ||
description="Transcript with Whisper model", | ||
) | ||
parser.add_argument( | ||
"-m", | ||
"--model", | ||
default="turbo", | ||
help="Model name", | ||
) | ||
parser.add_argument( | ||
"-f", | ||
"--file", | ||
default="dist/sample_audio.wav", | ||
help="Audio file", | ||
) | ||
parser.add_argument( | ||
"-v", | ||
"--verbose", | ||
action="store_true", | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
if __name__ == "__main__": | ||
args = init_args() | ||
|
||
# Set verbose mode | ||
if args.verbose: | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
# Parse .env file and set environment variables | ||
load_dotenv() | ||
|
||
model = whisper.load_model(name=args.model) | ||
|
||
# load audio and pad/trim it to fit 30 seconds | ||
audio = whisper.load_audio( | ||
file=args.file, | ||
) | ||
audio = whisper.pad_or_trim( | ||
array=audio, | ||
length=30 * 16000, | ||
) | ||
|
||
# make log-Mel spectrogram and move to the same device as the model | ||
# https://github.com/openai/whisper/pull/1764 | ||
mel = whisper.log_mel_spectrogram( | ||
audio=audio, | ||
n_mels=128, | ||
).to(model.device) | ||
|
||
# detect the spoken language | ||
_, probs = model.detect_language(mel) | ||
print(f"Detected language: {max(probs, key=probs.get)}") | ||
|
||
# decode the audio | ||
options = whisper.DecodingOptions() | ||
result = whisper.decode(model, mel, options) | ||
|
||
# print the recognized text | ||
print(result.text) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters