transcription/src/config.py
2026-05-29 10:16:02 +03:00

34 lines
1.2 KiB
Python

"""Загрузка и управление конфигурацией."""
import os
from pathlib import Path
from typing import Any, Dict
import yaml
DEFAULT_CONFIG_PATH = Path(__file__).parent.parent / "config.yaml"
def load_config(path: str | Path | None = None) -> Dict[str, Any]:
"""Загружает YAML-конфиг."""
config_path = Path(path) if path else DEFAULT_CONFIG_PATH
with open(config_path, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
def get_profile(config: Dict[str, Any], profile_name: str | None = None) -> Dict[str, Any]:
"""Возвращает слитый профиль (base + выбранный)."""
active = profile_name or config.get("active_profile", "cpu_best")
profiles = config.get("profiles", {})
if active not in profiles:
raise ValueError(f"Профиль '{active}' не найден. Доступные: {list(profiles.keys())}")
return profiles[active]
def resolve_hf_token(config: Dict[str, Any]) -> str | None:
"""Возвращает HF токен: из конфига или env HF_TOKEN."""
token = config.get("hf_token")
if not token:
token = os.environ.get("HF_TOKEN")
return token