Spaces:
Sleeping
Sleeping
Update retriever.py
Browse files- retriever.py +39 -11
retriever.py
CHANGED
|
@@ -1,39 +1,67 @@
|
|
| 1 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import faiss
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
class FoundationRetriever:
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
parquet_path = hf_hub_download(repo_id, parquet_file)
|
| 10 |
faiss_path = hf_hub_download(repo_id, faiss_file)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
self.df = pd.read_parquet(parquet_path)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
self.index = faiss.read_index(faiss_path)
|
| 17 |
self.dim = self.index.d
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
self.model = FlagICLModel(
|
| 22 |
-
|
| 23 |
-
query_instruction_for_retrieval="Given a user perspective, retrieve relevant foundation missions.",
|
| 24 |
-
examples_for_task=
|
| 25 |
use_fp16=use_fp16
|
| 26 |
)
|
| 27 |
|
| 28 |
def find_aligned_foundations(self, perspective_text, top_k=5):
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
query_emb = self.model.encode_queries([perspective_text]).astype("float32")
|
| 31 |
faiss.normalize_L2(query_emb)
|
| 32 |
|
| 33 |
# Search FAISS
|
| 34 |
distances, indices = self.index.search(query_emb, top_k)
|
| 35 |
|
| 36 |
-
# Retrieve
|
| 37 |
results = self.df.iloc[indices[0]][["Title", "Purpose"]].copy()
|
| 38 |
results["similarity"] = distances[0]
|
| 39 |
|
|
|
|
| 1 |
from huggingface_hub import hf_hub_download
|
| 2 |
+
from FlagEmbedding import FlagICLModel
|
| 3 |
import pandas as pd
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
class FoundationRetriever:
|
| 8 |
+
"""
|
| 9 |
+
Retrieve foundations whose missions align with a user perspective.
|
| 10 |
+
Uses FAISS for efficient similarity search and BGE-en-ICL for encoding queries with examples.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
def __init__(self, repo_id, parquet_file, faiss_file, use_fp16=False):
|
| 14 |
+
"""
|
| 15 |
+
repo_id: Hugging Face dataset repo containing Parquet and FAISS index
|
| 16 |
+
parquet_file: Parquet filename in repo
|
| 17 |
+
faiss_file: FAISS index filename in repo
|
| 18 |
+
use_fp16: whether to use FP16 for model inference
|
| 19 |
+
"""
|
| 20 |
+
# Download files from Hugging Face
|
| 21 |
parquet_path = hf_hub_download(repo_id, parquet_file)
|
| 22 |
faiss_path = hf_hub_download(repo_id, faiss_file)
|
| 23 |
|
| 24 |
+
# Load foundations dataframe
|
| 25 |
self.df = pd.read_parquet(parquet_path)
|
| 26 |
|
| 27 |
+
# Load FAISS index
|
| 28 |
self.index = faiss.read_index(faiss_path)
|
| 29 |
self.dim = self.index.d
|
| 30 |
|
| 31 |
+
# Define few-shot examples for ICL query embedding
|
| 32 |
+
examples_for_task = [
|
| 33 |
+
{
|
| 34 |
+
"instruct": "Retrieve foundations whose mission aligns with the given perspective.",
|
| 35 |
+
"query": "Protect marine life while educating children about ocean conservation",
|
| 36 |
+
"response": "Foundations working on marine conservation and youth education."
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"instruct": "Retrieve foundations whose mission aligns with the given perspective.",
|
| 40 |
+
"query": "Promote renewable energy education and community awareness",
|
| 41 |
+
"response": "Foundations focused on clean energy advocacy and public education."
|
| 42 |
+
}
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
# Load BGE-en-ICL model
|
| 46 |
self.model = FlagICLModel(
|
| 47 |
+
"BAAI/bge-en-icl",
|
| 48 |
+
query_instruction_for_retrieval="Given a user perspective about philanthropy, retrieve relevant foundation missions.",
|
| 49 |
+
examples_for_task=examples_for_task,
|
| 50 |
use_fp16=use_fp16
|
| 51 |
)
|
| 52 |
|
| 53 |
def find_aligned_foundations(self, perspective_text, top_k=5):
|
| 54 |
+
"""
|
| 55 |
+
Returns a DataFrame with top-k foundations aligned with the user's perspective.
|
| 56 |
+
"""
|
| 57 |
+
# Encode user perspective with ICL
|
| 58 |
query_emb = self.model.encode_queries([perspective_text]).astype("float32")
|
| 59 |
faiss.normalize_L2(query_emb)
|
| 60 |
|
| 61 |
# Search FAISS
|
| 62 |
distances, indices = self.index.search(query_emb, top_k)
|
| 63 |
|
| 64 |
+
# Retrieve foundation info
|
| 65 |
results = self.df.iloc[indices[0]][["Title", "Purpose"]].copy()
|
| 66 |
results["similarity"] = distances[0]
|
| 67 |
|