31 lines
978 B
Python
31 lines
978 B
Python
|
|
"""Транскрибация аудио через faster-whisper (модель small по умолчанию)."""
|
||
|
|
import logging
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import config
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
_model = None
|
||
|
|
|
||
|
|
|
||
|
|
def _get_model():
|
||
|
|
global _model
|
||
|
|
if _model is None:
|
||
|
|
from faster_whisper import WhisperModel
|
||
|
|
logger.info("Загрузка Whisper модели %s...", config.WHISPER_MODEL)
|
||
|
|
_model = WhisperModel(config.WHISPER_MODEL)
|
||
|
|
logger.info("Whisper модель загружена.")
|
||
|
|
return _model
|
||
|
|
|
||
|
|
|
||
|
|
def transcribe_audio(audio_path: str | Path) -> str:
|
||
|
|
"""Перевести аудиофайл в текст. Путь до .ogg, .mp3, .wav и т.д."""
|
||
|
|
path = Path(audio_path)
|
||
|
|
if not path.exists():
|
||
|
|
return ""
|
||
|
|
model = _get_model()
|
||
|
|
segments, _ = model.transcribe(str(path), language="ru", vad_filter=True)
|
||
|
|
text = " ".join(s.text.strip() for s in segments if s.text).strip()
|
||
|
|
return text
|