Speech-To-Text in Python
Transcribe audio to text in Python with OpenAI's Whisper.
1 min read
Updated
Summarise this page with
your favorite AI assistant
Transcribe an audio file to text in Python using OpenAI's Whisper model, choosing the model size that fits your hardware. The scripts are below.
Speech-To-Text in Python using Whisper.
Usage
python
from stt import Speech2Text
stt = Speech2Text(language = 'french')
text = stt.transcribe('audio.wav')
print(text)stt.py
python
import whisper
class Speech2Text:
def __init__(self, language: str, model = 'base'):
self._model = whisper.load_model(model)
self._decoding_options = whisper.DecodingOptions(fp16 = False, language = language)
def transcribe(self, audio_file: str):
audio = whisper.load_audio(audio_file)
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(self._model.device)
try:
result = whisper.decode(self._model, mel, self._decoding_options)
return result.text
except:
return '...'Something wrong or want to discuss this article?
Get in touch