|
from typing import Dict |
|
from faster_whisper import WhisperModel |
|
import io |
|
|
|
class EndpointHandler: |
|
def __init__(self, model_dir=None): |
|
|
|
model_size = "medium" if model_dir is None else model_dir |
|
|
|
self.model = WhisperModel(model_size, device="cuda", compute_type="float16") |
|
|
|
def __call__(self, data: Dict) -> Dict[str, str]: |
|
|
|
audio_bytes = data["inputs"] |
|
|
|
|
|
audio_file = io.BytesIO(audio_bytes) |
|
|
|
|
|
segments, info = self.model.transcribe(audio_file) |
|
|
|
|
|
|
|
text = " ".join(segment.text.strip() for segment in segments) |
|
language_code = info.language |
|
language_prob = info.language_probability |
|
|
|
|
|
result = { |
|
"text": text, |
|
"language": language_code, |
|
"language_probability": language_prob |
|
} |
|
|
|
return result |