def transcribe_audio(model, processor, audio_file, device):
    """Transcribe audio file"""
    audio = whisper.load_audio(audio_file)  # load from path
    audio = whisper.pad_or_trim(audio)  # default 30 seconds
    inputs = whisper.log_mel_spectrogram(audio).to(
        device=device
    )  # convert to spectrogram
    inputs = inputs.unsqueeze(0).type(torch.cuda.HalfTensor)  # add batch dimension

    outputs = model.generate(
        inputs=inputs, max_new_tokens=128
    )  # generate transcription
    transcription = processor.batch_decode(outputs, skip_special_tokens=True)[
        0
    ]  # decode
    transcription_processed = preprocess_text(transcription)  # preprocess
    return transcription_processed
Transcribe audio file
utils/whisper_transcription.py