Spaces:
Running
Running
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(audio_data): | |
# 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 |