Spaces:
Running
Running
File size: 1,096 Bytes
a9059e8 ea3c0cf a9059e8 8097eeb e872cbe a9059e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import torchaudio
import torch
# 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(audio_data,sample_rate):
# Convert raw audio frame (numpy array) to tensor and resample it to 16 kHz
waveform = torch.tensor(audio_data, dtype=torch.float32).unsqueeze(0)
# Check if the sample rate is 16 kHz; if not, resample it
if sample_rate != 16000:
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 |