Download src/models/asr.py from dubbingAI/DubAI: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/dubbingAI/DubAI/resolve/main/src/models/asr.py
- Command line
-
hf download hf://spaces/dubbingAI/DubAI/src/models/asr.py
-
curl -L -o asr.py https://huggingface.co/spaces/dubbingAI/DubAI/resolve/main/src/models/asr.py
1.23 kB
| import librosa | |
| from transformers import WhisperProcessor, WhisperForConditionalGeneration | |
| from typing import Optional | |
| import torch | |
| class ASRService: | |
| def __init__(self): | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| self.processor, self.model = self._load_model() | |
| def _load_model(self): | |
| """Ładowanie modelu Whisper""" | |
| model_name = "openai/whisper-large-v3-turbo" | |
| processor = WhisperProcessor.from_pretrained(model_name) | |
| model = WhisperForConditionalGeneration.from_pretrained(model_name).to(self.device) | |
| model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(language="pl", task="transcribe") | |
| return processor, model | |
| def transcribe(self, audio_path: str) -> str: | |
| """Transkrypcja audio na tekst""" | |
| sample, sr = librosa.load(audio_path, sr=16000) | |
| input_features = self.processor( | |
| sample, | |
| sampling_rate=sr, | |
| return_tensors="pt" | |
| ).input_features.to(self.device) | |
| predicted_ids = self.model.generate(input_features) | |
| return self.processor.batch_decode(predicted_ids, skip_special_tokens=True)[0] | |
| # Singleton pattern | |
| ASR = ASRService() |