Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import faiss | |
| from sentence_transformers import SentenceTransformer | |
| # Load global embedding model once | |
| model = SentenceTransformer("multi-qa-mpnet-base-dot-v1") | |
| def build_index(keywords): | |
| """Builds FAISS index dynamically from keywords.""" | |
| enriched = [f"{k} category" for k in keywords] | |
| embeddings = model.encode(enriched).astype("float32") | |
| embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True) | |
| dim = embeddings.shape[1] | |
| index = faiss.IndexFlatIP(dim) | |
| index.add(embeddings) | |
| return index, embeddings | |