audio_conversation / audio_to_text.py
vakodiya's picture
Create audio_to_text.py
a9059e8 verified
raw
history blame
917 Bytes
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import torchaudio
# load model and processor
processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
model.config.forced_decoder_ids = None
def audio_to_text(file_path_abs):
# Load the audio and resample it
waveform, sample_rate = torchaudio.load(file_path_abs)
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
waveform = resampler(waveform)
waveform = waveform.squeeze().numpy()
input_features = processor(waveform, sampling_rate=16000, return_tensors="pt").input_features
# generate token ids
predicted_ids = model.generate(input_features)
# decode token ids to text
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
return transcription