Skip to content

Instantly share code, notes, and snippets.

@AIWintermuteAI
Created December 18, 2023 08:43
Show Gist options
  • Save AIWintermuteAI/c916fbef719c58d89b1d69a4dd42eadf to your computer and use it in GitHub Desktop.
Save AIWintermuteAI/c916fbef719c58d89b1d69a4dd42eadf to your computer and use it in GitHub Desktop.
Python file to compare the inference times between whisper.cpp Python bindings and faster-whisper
import argparse
import whispercpp as w
from faster_whisper import WhisperModel, decode_audio
import time
from collections.abc import Iterator
from contextlib import contextmanager
@contextmanager
def time_it() -> Iterator[None]:
tic: float = time.perf_counter()
try:
yield
finally:
toc: float = time.perf_counter()
print(f"Computation time = {1000*(toc - tic):.3f}ms")
def transcribe_file_cpp(file_path, model_name):
transcriber = w.Whisper.from_pretrained(model_name)
res = transcriber.transcribe_from_file(file_path)
return res
def transcribe_file_fwhisper(file_path, model_name):
model = WhisperModel(model_name)
segments, info = model.transcribe(file_path, word_timestamps=True)
return segments
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--file_path", default="jfk.wav", help="Path to the audio file", nargs='?')
parser.add_argument("--model_name", default="tiny.en", choices=list(w.utils.MODELS_URL), help="Name of the model to use for transcription")
args = parser.parse_args()
with time_it():
res = transcribe_file_cpp(args.file_path, args.model_name)
print(res)
with time_it():
res = transcribe_file_fwhisper(args.file_path, args.model_name)
for segment in res:
print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment