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

Replace with in-memory stream on recognize_whisper #647

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install Python dependencies
run: |
python -m pip install 'pocketsphinx<5'
python -m pip install git+https://github.com/openai/whisper.git
python -m pip install git+https://github.com/openai/whisper.git soundfile
python -m pip install .
- name: Test with unittest
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Whisper (for Whisper users)
~~~~~~~~~~~~~~~~~~~~~
Whisper is **required if and only if you want to use whisper** (``recognizer_instance.recognize_whisper``).

You can install it with ``python3 -m pip install git+https://github.com/openai/whisper.git``.
You can install it with ``python3 -m pip install git+https://github.com/openai/whisper.git soundfile``.

Troubleshooting
---------------
Expand Down
25 changes: 15 additions & 10 deletions speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1684,23 +1684,28 @@ def recognize_whisper(self, audio_data, model="base", show_dict=False, load_opti
"""

assert isinstance(audio_data, AudioData), "Data must be audio data"
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 {})

with tempfile.NamedTemporaryFile(suffix=".wav") as f:
f.write(audio_data.get_wav_data())
f.flush()
result = self.whisper_model[model].transcribe(
f.name,
language=language,
task="translate" if translate else None,
fp16=torch.cuda.is_available(),
**transcribe_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)
wav_stream = io.BytesIO(wav_bytes)
audio_array, sampling_rate = sf.read(wav_stream)
audio_array = audio_array.astype(np.float32)

result = self.whisper_model[model].transcribe(
audio_array,
language=language,
task="translate" if translate else None,
fp16=torch.cuda.is_available(),
**transcribe_options
)

if show_dict:
return result
Expand Down