Spaces:
Sleeping
Sleeping
Download backend/app/services/embedding_service.py from tryprogram/offer-puplayer: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/tryprogram/offer-puplayer/resolve/main/backend/app/services/embedding_service.py
- Command line
-
hf download hf://spaces/tryprogram/offer-puplayer/backend/app/services/embedding_service.py
-
curl -L -o embedding_service.py https://huggingface.co/spaces/tryprogram/offer-puplayer/resolve/main/backend/app/services/embedding_service.py
1.26 kB
| import logging | |
| import math | |
| import os | |
| logger = logging.getLogger(__name__) | |
| _model = None | |
| _LOCAL_MODEL_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "models") | |
| def _get_model(): | |
| global _model | |
| if _model is None: | |
| logger.info("Loading embedding model ...") | |
| from fastembed import TextEmbedding | |
| local_path = _LOCAL_MODEL_PATH | |
| if os.path.isdir(local_path) and os.path.isfile(os.path.join(local_path, "model_optimized.onnx")): | |
| logger.info("Loading from local path: %s", local_path) | |
| _model = TextEmbedding(model_name=local_path, cache_dir=os.path.dirname(local_path)) | |
| else: | |
| os.environ.setdefault("HF_ENDPOINT", "https://hf-mirror.com") | |
| _model = TextEmbedding("BAAI/bge-small-zh-v1.5") | |
| logger.info("Embedding model loaded") | |
| return _model | |
| def compute_embedding(text: str) -> list[float]: | |
| vec = list(_get_model().embed(text))[0] | |
| return vec.tolist() | |
| def cosine_similarity(a: list[float], b: list[float]) -> float: | |
| dot = sum(x * y for x, y in zip(a, b)) | |
| na = math.sqrt(sum(x * x for x in a)) | |
| nb = math.sqrt(sum(y * y for y in b)) | |
| if na == 0 or nb == 0: | |
| return 0.0 | |
| return dot / (na * nb) | |