Download config/loader.py from modventures/room-visualizer-v2: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/config/loader.py
- Command line
-
hf download hf://spaces/modventures/room-visualizer-v2/config/loader.py
-
curl -L -o loader.py https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/config/loader.py
1.52 kB
| import os | |
| from pathlib import Path | |
| try: | |
| import tomllib | |
| except ImportError: | |
| try: | |
| import tomli as tomllib # type: ignore[no-redef] | |
| except ImportError: | |
| try: | |
| import tomlkit as tomllib # type: ignore[no-redef] | |
| except ImportError: | |
| raise ImportError( | |
| "No TOML library found. Please run on Python 3.11+, " | |
| "or run 'pip install tomli' to support Python 3.10." | |
| ) | |
| try: | |
| from dotenv import load_dotenv | |
| except ImportError: | |
| load_dotenv = None | |
| BACKEND_DIR = Path(__file__).resolve().parent.parent | |
| REPO_ROOT = BACKEND_DIR.parent.parent | |
| if load_dotenv is not None: | |
| load_dotenv(BACKEND_DIR / ".env") | |
| def load_config() -> dict: | |
| config_path = os.getenv("VISUALIZER_CONFIG") | |
| if not config_path: | |
| return {} | |
| path = Path(config_path).expanduser() | |
| if not path.is_absolute(): | |
| path = BACKEND_DIR / path | |
| if not path.exists(): | |
| raise RuntimeError(f"VISUALIZER_CONFIG does not exist: {path}") | |
| with path.open("rb") as config_file: | |
| return tomllib.load(config_file) | |
| CONFIG = load_config() | |
| def config_value(env_name: str, section: str, key: str, default): | |
| if env_name in os.environ: | |
| return os.environ[env_name] | |
| return CONFIG.get(section, {}).get(key, default) | |
| def config_bool(env_name: str, section: str, key: str, default: str = "0") -> bool: | |
| return str(config_value(env_name, section, key, default)).lower() in { | |
| "1", "true", "yes", "on", | |
| } | |