Spaces:
Sleeping
Sleeping
update retriever to add inference option
Browse files- retriever.py +85 -51
retriever.py
CHANGED
|
@@ -1,35 +1,64 @@
|
|
| 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 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
{
|
| 34 |
"instruct": "Retrieve foundations whose mission aligns with the given perspective.",
|
| 35 |
"query": "Protect marine life while educating children about ocean conservation",
|
|
@@ -42,27 +71,32 @@ class FoundationRetriever:
|
|
| 42 |
}
|
| 43 |
]
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download, InferenceClient
|
| 2 |
from FlagEmbedding import FlagICLModel
|
| 3 |
import pandas as pd
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
+
# Define HF tokens
|
| 10 |
+
HF_TOKEN_read = os.environ.get("HF_TOKEN_read")
|
| 11 |
+
HF_TOKEN_inference = os.environ.get("HF_TOKEN_inf")
|
| 12 |
+
|
| 13 |
+
# Setup InferenceClient for embeddings
|
| 14 |
+
client = InferenceClient(provider="nebius", api_key=HF_TOKEN_inference)
|
| 15 |
+
|
| 16 |
+
# Dataset repo (private)
|
| 17 |
+
DATASET_REPO = "luciagomez/MrPhil_vector"
|
| 18 |
+
|
| 19 |
+
# -------------------------------------------------------------------
|
| 20 |
+
# 1. Download files from Hugging Face dataset
|
| 21 |
+
# -------------------------------------------------------------------
|
| 22 |
+
parquet_path = hf_hub_download(
|
| 23 |
+
repo_id=DATASET_REPO,
|
| 24 |
+
filename="foundations.parquet",
|
| 25 |
+
repo_type="dataset",
|
| 26 |
+
token=HF_TOKEN_read
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
faiss_path = hf_hub_download(
|
| 30 |
+
repo_id=DATASET_REPO,
|
| 31 |
+
filename="faiss.index",
|
| 32 |
+
repo_type="dataset",
|
| 33 |
+
token=HF_TOKEN_read
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
meta_path = hf_hub_download(
|
| 37 |
+
repo_id=DATASET_REPO,
|
| 38 |
+
filename="meta.json",
|
| 39 |
+
repo_type="dataset",
|
| 40 |
+
token=HF_TOKEN_read
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# -------------------------------------------------------------------
|
| 45 |
+
# 2. Load data
|
| 46 |
+
# -------------------------------------------------------------------
|
| 47 |
+
df = pd.read_parquet(parquet_path)
|
| 48 |
+
index = faiss.read_index(faiss_path)
|
| 49 |
+
with open(meta_path, "r") as f:
|
| 50 |
+
meta = json.load(f)
|
| 51 |
+
|
| 52 |
+
dim = meta.get("embedding_dim", None)
|
| 53 |
+
n = meta.get("num_vectors", len(df))
|
| 54 |
+
|
| 55 |
+
print(f"Loaded FAISS index with {n} vectors of dimension {dim}")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# -------------------------------------------------------------------
|
| 59 |
+
# 3. Initialize BGE-ICL model for queries
|
| 60 |
+
# -------------------------------------------------------------------
|
| 61 |
+
examples = [
|
| 62 |
{
|
| 63 |
"instruct": "Retrieve foundations whose mission aligns with the given perspective.",
|
| 64 |
"query": "Protect marine life while educating children about ocean conservation",
|
|
|
|
| 71 |
}
|
| 72 |
]
|
| 73 |
|
| 74 |
+
|
| 75 |
+
#model = FlagICLModel(
|
| 76 |
+
# "BAAI/bge-en-icl",
|
| 77 |
+
# query_instruction_for_retrieval="Given a mission statement, retrieve foundations with aligned purposes.",
|
| 78 |
+
# examples_for_task=examples,
|
| 79 |
+
# use_fp16=False
|
| 80 |
+
#)
|
| 81 |
+
|
| 82 |
+
def encode_query(perspective):
|
| 83 |
+
payload = {"model": "BAAI/bge-en-icl",
|
| 84 |
+
"inputs": perspective,
|
| 85 |
+
"parameters": {"instruction": "Retrieve foundations aligned with perspective.",
|
| 86 |
+
"examples": examples}}
|
| 87 |
+
response = client.feature_extraction(**payload)
|
| 88 |
+
return np.array(response)
|
| 89 |
+
|
| 90 |
+
# -------------------------------------------------------------------
|
| 91 |
+
# 4. Retrieval function
|
| 92 |
+
# -------------------------------------------------------------------
|
| 93 |
+
def find_similar_foundations(perspective, top_k=5):
|
| 94 |
+
q_emb = encode_query(perspective).astype("float32")
|
| 95 |
+
faiss.normalize_L2(q_emb)
|
| 96 |
+
scores, idxs = index.search(q_emb, top_k)
|
| 97 |
+
return [
|
| 98 |
+
{"title": df.iloc[i]["Title"], "purpose": df.iloc[i]["Purpose"], "similarity": float(scores[0][j])}
|
| 99 |
+
for j, i in enumerate(idxs[0])
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
|