vakodiya commited on
Commit
e872cbe
1 Parent(s): 336a7dd

Update audio_to_text.py

Browse files
Files changed (1) hide show
  1. audio_to_text.py +7 -5
audio_to_text.py CHANGED
@@ -8,11 +8,13 @@ model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
8
  model.config.forced_decoder_ids = None
9
 
10
 
11
- def audio_to_text(file_path_abs):
12
- # Load the audio and resample it
13
- waveform, sample_rate = torchaudio.load(file_path_abs)
14
- resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
15
- waveform = resampler(waveform)
 
 
16
  waveform = waveform.squeeze().numpy()
17
  input_features = processor(waveform, sampling_rate=16000, return_tensors="pt").input_features
18
 
 
8
  model.config.forced_decoder_ids = None
9
 
10
 
11
+ def audio_to_text(audio_data):
12
+ # Convert raw audio frame (numpy array) to tensor and resample it to 16 kHz
13
+ waveform = torch.tensor(audio_data, dtype=torch.float32).unsqueeze(0)
14
+ # Check if the sample rate is 16 kHz; if not, resample it
15
+ if sample_rate != 16000:
16
+ resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
17
+ waveform = resampler(waveform)
18
  waveform = waveform.squeeze().numpy()
19
  input_features = processor(waveform, sampling_rate=16000, return_tensors="pt").input_features
20