NimrodDev commited on
Commit
622b23f
·
1 Parent(s): 22e1771

use optimum ONNX MiniLM (no disk, no internet)

Browse files
Files changed (1) hide show
  1. rag.py +15 -16
rag.py CHANGED
@@ -108,22 +108,21 @@ def get_texts() -> List[str]:
108
  def get_vectorstore() -> FAISS:
109
  texts = get_texts()
110
 
111
- # --- FINAL: load local MiniLM (no internet, no cache) -----------------
112
- import os
113
- local_model_path = os.path.abspath(
114
- os.path.join(os.path.dirname(__file__), "st_model")
115
- )
116
-
117
- # force offline + local only
118
- os.environ["TRANSFORMERS_OFFLINE"] = "1"
119
- os.environ["HF_DATASETS_OFFLINE"] = "1"
120
- os.environ["SENTENCE_TRANSFORMERS_HOME"] = local_model_path
121
-
122
- from sentence_transformers import SentenceTransformer
123
- model = SentenceTransformer(local_model_path, device="cpu", cache_folder=None)
124
-
125
- from langchain.embeddings import SentenceTransformerEmbeddings
126
- embeddings = SentenceTransformerEmbeddings(model=model)
127
  # ------------------------------------------------------------------------
128
 
129
  if not texts: # no data → empty FAISS
 
108
  def get_vectorstore() -> FAISS:
109
  texts = get_texts()
110
 
111
+ # --- FINAL: optimum ONNX MiniLM (already on disk) ---------------------
112
+ from optimum.pipelines import pipeline
113
+ from langchain.embeddings.base import Embeddings
114
+
115
+ class OptimumMiniLM(Embeddings):
116
+ def __init__(self):
117
+ self.pipe = pipeline("feature-extraction",
118
+ model="optimum/all-MiniLM-L6-v2",
119
+ device="cpu")
120
+ def embed_documents(self, texts):
121
+ return [self.pipe(t)[0][0] for t in texts]
122
+ def embed_query(self, text):
123
+ return self.embed_documents([text])[0]
124
+
125
+ embeddings = OptimumMiniLM()
 
126
  # ------------------------------------------------------------------------
127
 
128
  if not texts: # no data → empty FAISS