Spaces:
Running
Running
Update audio_to_text.py
Browse files- 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(
|
12 |
-
#
|
13 |
-
waveform
|
14 |
-
|
15 |
-
|
|
|
|
|
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 |
|