Spaces:
Runtime error
Runtime error
Download src/utils/faiss_utils.py from mickey1976/cove-api: direct link, hf CLI and curl.
- Browser
- Download file 481 Bytes
-
https://huggingface.co/spaces/mickey1976/cove-api/resolve/main/src/utils/faiss_utils.py
- Command line
-
hf download hf://spaces/mickey1976/cove-api/src/utils/faiss_utils.py
-
curl -L -o faiss_utils.py https://huggingface.co/spaces/mickey1976/cove-api/resolve/main/src/utils/faiss_utils.py
481 Bytes
| # src/utils/faiss_utils.py | |
| import faiss | |
| import numpy as np | |
| def build_faiss_index(embeddings: np.ndarray, use_gpu=False): | |
| d = embeddings.shape[1] | |
| index = faiss.IndexFlatIP(d) # Inner Product (cosine similarity if normalized) | |
| if use_gpu: | |
| res = faiss.StandardGpuResources() | |
| index = faiss.index_cpu_to_gpu(res, 0, index) | |
| index.add(embeddings.astype(np.float32)) | |
| return index | |
| def save_faiss_index(index, path): | |
| faiss.write_index(index, path) |