use public HF inference endpoint for embeddings (no disk)
Browse files
rag.py
CHANGED
|
@@ -111,26 +111,25 @@ def get_texts() -> List[str]:
|
|
| 111 |
print(f"⚠ Dataset fetch failed: {e} – using empty corpus")
|
| 112 |
return []
|
| 113 |
|
| 114 |
-
# ------------------------------------------------------------------
|
| 115 |
-
# RAM-ONLY VECTOR STORE – HANDLES EMPTY CORPUS GRACEFULLY
|
| 116 |
-
# ------------------------------------------------------------------
|
| 117 |
@lru_cache(maxsize=1)
|
| 118 |
def get_vectorstore() -> FAISS:
|
| 119 |
texts = get_texts()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
if not texts: # no data → empty FAISS
|
| 121 |
-
import os
|
| 122 |
-
os.environ["HF_HOME"] = "/code/.cache"
|
| 123 |
-
os.environ["TRANSFORMERS_CACHE"] = "/code/.cache"
|
| 124 |
-
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 125 |
return FAISS.from_texts([""], embeddings) # dummy
|
| 126 |
|
| 127 |
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
|
| 128 |
docs = splitter.create_documents(texts, metadatas=[{"source": DATASET}] * len(texts))
|
| 129 |
-
|
| 130 |
-
import os
|
| 131 |
-
os.environ["HF_HOME"] = "/code/.cache"
|
| 132 |
-
os.environ["TRANSFORMERS_CACHE"] = "/code/.cache"
|
| 133 |
-
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 134 |
return FAISS.from_documents(docs, embeddings)
|
| 135 |
|
| 136 |
|
|
|
|
| 111 |
print(f"⚠ Dataset fetch failed: {e} – using empty corpus")
|
| 112 |
return []
|
| 113 |
|
| 114 |
+
## ------------------------------------------------------------------
|
|
|
|
|
|
|
| 115 |
@lru_cache(maxsize=1)
|
| 116 |
def get_vectorstore() -> FAISS:
|
| 117 |
texts = get_texts()
|
| 118 |
+
|
| 119 |
+
# --- NEW: zero-disk HF Inference embeddings -----------------------------
|
| 120 |
+
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
| 121 |
+
embeddings = HuggingFaceEndpointEmbeddings(
|
| 122 |
+
model="sentence-transformers/all-MiniLM-L6-v2",
|
| 123 |
+
task="feature-extraction",
|
| 124 |
+
huggingfacehub_api_token=HF_TOKEN # already in Space secrets
|
| 125 |
+
)
|
| 126 |
+
# ------------------------------------------------------------------------
|
| 127 |
+
|
| 128 |
if not texts: # no data → empty FAISS
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return FAISS.from_texts([""], embeddings) # dummy
|
| 130 |
|
| 131 |
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=50)
|
| 132 |
docs = splitter.create_documents(texts, metadatas=[{"source": DATASET}] * len(texts))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
return FAISS.from_documents(docs, embeddings)
|
| 134 |
|
| 135 |
|