Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to allow whisper model pre-load #793

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,17 @@ def recognize_tensorflow(self, audio_data, tensor_graph='tensorflow-data/conv_ac
human_string = self.tflabels[node_id]
return human_string

def pre_load_whisper_model(self, model="base", load_options=None):
"""
Pre-load whisper model
model can be any of tiny, base, small, medium, large, tiny.en, base.en, small.en, medium.en. See https://github.com/openai/whisper for more details.
"""
import whisper

if load_options or not hasattr(self, "whisper_model") or self.whisper_model.get(model) is None:
self.whisper_model = getattr(self, "whisper_model", {})
self.whisper_model[model] = whisper.load_model(model, **load_options or {})

def recognize_whisper(self, audio_data, model="base", show_dict=False, load_options=None, language=None, translate=False, **transcribe_options):
"""
Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using Whisper.
Expand All @@ -1430,11 +1441,8 @@ def recognize_whisper(self, audio_data, model="base", show_dict=False, load_opti
import numpy as np
import soundfile as sf
import torch
import whisper

if load_options or not hasattr(self, "whisper_model") or self.whisper_model.get(model) is None:
self.whisper_model = getattr(self, "whisper_model", {})
self.whisper_model[model] = whisper.load_model(model, **load_options or {})
self.pre_load_whisper_model(model=model, load_options=load_options)

# 16 kHz https://github.com/openai/whisper/blob/28769fcfe50755a817ab922a7bc83483159600a9/whisper/audio.py#L98-L99
wav_bytes = audio_data.get_wav_data(convert_rate=16000)
Expand Down