whisper_simple / src /infer.py
nunenuh's picture
feat: add infer, utils and change app.py for gradio
476b1c3
raw
history blame
1.07 kB
from typing import *
from src import utils
from transformers import WhisperProcessor, WhisperForConditionalGeneration
model_name: str = f"openai/whisper-base"
processor: Any = WhisperProcessor.from_pretrained(model_name)
model: Any = WhisperForConditionalGeneration.from_pretrained(model_name)
sample_rate: int = 16000
float_factor: float = 32678.0
def predict(language, mic_audio=None, audio=None):
if mic_audio is not None:
sampling_rate, waveform = mic_audio
elif audio is not None:
sampling_rate, waveform = audio
else:
return "(please provide audio)"
forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task="transcribe")
waveform = utils.preprocess_audio(sampling_rate, waveform)
inputs = processor(audio=waveform, sampling_rate=sample_rate, return_tensors="pt")
predicted_ids = model.generate(**inputs, max_length=400, forced_decoder_ids=forced_decoder_ids)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
return transcription[0]