Spaces:
Sleeping
Sleeping
Download src/vector_store.py from Sujith2121/Semantic-search-cache: direct link, hf CLI and curl.
- Browser
- Download file 443 Bytes
-
https://huggingface.co/spaces/Sujith2121/Semantic-search-cache/resolve/main/src/vector_store.py
- Command line
-
hf download hf://spaces/Sujith2121/Semantic-search-cache/src/vector_store.py
-
curl -L -o vector_store.py https://huggingface.co/spaces/Sujith2121/Semantic-search-cache/resolve/main/src/vector_store.py
443 Bytes
| import faiss | |
| import numpy as np | |
| import os | |
| def build_faiss_index(embeddings): | |
| dimension = embeddings.shape[1] | |
| index = faiss.IndexFlatL2(dimension) | |
| index.add(np.array(embeddings)) | |
| return index | |
| def save_index(index, path="models/faiss_index"): | |
| faiss.write_index(index, path) | |
| def load_index(path="models/faiss_index"): | |
| if os.path.exists(path): | |
| return faiss.read_index(path) | |
| else: | |
| return None |