Spaces:
Running on Zero
Running on Zero
Download embedding_model.py from sreenathvemula/ChristianMCP: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/spaces/sreenathvemula/ChristianMCP/resolve/main/embedding_model.py
- Command line
-
hf download hf://spaces/sreenathvemula/ChristianMCP/embedding_model.py
-
curl -L -o embedding_model.py https://huggingface.co/spaces/sreenathvemula/ChristianMCP/resolve/main/embedding_model.py
1.12 kB
| """One process-wide sentence-transformer shared by every semantic lookup.""" | |
| from __future__ import annotations | |
| import os | |
| import threading | |
| MODEL_NAME = "BAAI/bge-small-en-v1.5" | |
| _model = None | |
| _lock = threading.Lock() | |
| def _offline() -> bool: | |
| """Use only cached model files when any supported offline flag is set.""" | |
| truthy = {"1", "true", "yes", "on"} | |
| return any( | |
| os.environ.get(name, "").strip().lower() in truthy | |
| for name in ("CHRISTIANMCP_OFFLINE", "HF_HUB_OFFLINE", "TRANSFORMERS_OFFLINE") | |
| ) | |
| def get_embedder(): | |
| """Load the CPU model once and safely reuse it across tool modules.""" | |
| global _model | |
| if _model is None: | |
| with _lock: | |
| if _model is None: | |
| from sentence_transformers import SentenceTransformer | |
| _model = SentenceTransformer( | |
| MODEL_NAME, | |
| device="cpu", | |
| local_files_only=_offline(), | |
| ) | |
| return _model | |
| def preload_embedder(): | |
| """Eagerly initialize the shared model; useful before MCP event loops.""" | |
| return get_embedder() | |