Spaces:
Running
Running
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:6.1.1-3ubuntu5") | |
# 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 |