from __future__ import annotations import os from pathlib import Path def weights_dir() -> Path: """Directory scanned for locally-bundled model weights. Defaults to ``models`` (relative to the working directory); override with the ``BGFILTER_WEIGHTS_DIR`` environment variable. """ return Path(os.environ.get("BGFILTER_WEIGHTS_DIR", "models")) def resolve_model_source(repo_id: str) -> str: """Map a HuggingFace repo id to a local weights folder when one is present. If ``/`` exists it is returned (so models can be shipped as plain folders inside the project, e.g. ``ZhengPeng7/BiRefNet`` -> ``models/BiRefNet``); otherwise the repo id is returned unchanged so it loads from the HF hub/cache as before. An absolute/existing path is passed through. """ if os.path.isdir(repo_id): return repo_id local = weights_dir() / repo_id.split("/")[-1] if local.is_dir(): return str(local) return repo_id