File size: 1,066 Bytes
476b1c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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]