CoolFace
Apppublic

GodsDevProject/FOIA_Doc_Search

sourceHugging Facemitupdated 9mo agoView on Hugging Face
1likes
semantic.py18 linesDownload Raw Back to search
1import faiss2import numpy as np3from sentence_transformers import SentenceTransformer4from typing import List, Dict5 6model = SentenceTransformer("all-MiniLM-L6-v2")7 8def build_faiss_index(docs: List[Dict]):9    texts = [d["content"] for d in docs]10    embeddings = model.encode(texts)11    index = faiss.IndexFlatL2(embeddings.shape[1])12    index.add(np.array(embeddings))13    return index, embeddings14 15def semantic_search(query: str, docs: List[Dict], index):16    q_emb = model.encode([query])17    D, I = index.search(np.array(q_emb), k=5)18    return [docs[i] for i in I[0]]