Spaces:
Running
Running
File size: 1,560 Bytes
a9059e8 ea3c0cf 6bee80f 514d2cf 82c5078 514d2cf a9059e8 6bee80f a9059e8 6bee80f e872cbe 6bee80f |
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from transformers import WhisperProcessor, WhisperForConditionalGeneration
import torchaudio
import torch
import os
from pydub import AudioSegment
# Install the specific version of FFmpeg
os.system("apt-get update")
os.system("apt-get install -y ffmpeg=7:5.1.6-0+deb12u1")
# Verify the installed version of FFmpeg
os.system("ffmpeg -version")
# load model and processor
processor = WhisperProcessor.from_pretrained("openai/whisper-small")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
model.config.forced_decoder_ids = None
def audio_to_text(webm_file_path):
wav_file = "recorded_audio.wav"
absolute_path = os.path.abspath(webm_file_path)
# Load and convert audio
# Check if the file exists
if os.path.exists(webm_file_path):
wav_audio = AudioSegment.from_file(absolute_path, format="webm")
wav_audio.export(wav_file, format="wav")
# Load the audio and resample it
waveform, sample_rate = torchaudio.load('recorded_audio.wav')
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
else:
return None |