from transformers import WhisperProcessor, WhisperForConditionalGeneration import torchaudio import torch import os from pydub import AudioSegment # Get the directory of the current file current_dir = os.path.dirname(os.path.abspath(__file__)) # Construct the absolute path to the 'ffmpeg/bin' directory ffmpeg_bin_path = os.path.join(current_dir, 'ffmpeg', 'bin') # Add this path to the PATH environment variable os.environ["PATH"] += os.pathsep + ffmpeg_bin_path # Ensure ffmpeg is in PATH AudioSegment.converter = os.path.join(ffmpeg_bin_path, 'ffmpeg.exe') # 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