hamxaameer commited on
Commit
15dcfa8
Β·
verified Β·
1 Parent(s): 7bba7c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -9
app.py CHANGED
@@ -88,7 +88,7 @@ def initialize_embeddings():
88
  return embeddings
89
 
90
  def load_vector_store(embeddings):
91
- """Load FAISS vector store"""
92
  logger.info("πŸ”„ Loading FAISS vector store...")
93
 
94
  vector_store_path = CONFIG["vector_store_path"]
@@ -108,14 +108,53 @@ def load_vector_store(embeddings):
108
  logger.info(f"βœ… Found index.faiss ({os.path.getsize(index_file)/1024/1024:.2f} MB)")
109
  logger.info(f"βœ… Found index.pkl ({os.path.getsize(pkl_file)/1024:.2f} KB)")
110
 
111
- vectorstore = FAISS.load_local(
112
- vector_store_path,
113
- embeddings,
114
- allow_dangerous_deserialization=True
115
- )
116
-
117
- logger.info(f"βœ… FAISS vector store loaded successfully")
118
- return vectorstore
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  # ============================================================================
121
  # RAG PIPELINE FUNCTIONS
 
88
  return embeddings
89
 
90
  def load_vector_store(embeddings):
91
+ """Load FAISS vector store with compatibility handling"""
92
  logger.info("πŸ”„ Loading FAISS vector store...")
93
 
94
  vector_store_path = CONFIG["vector_store_path"]
 
108
  logger.info(f"βœ… Found index.faiss ({os.path.getsize(index_file)/1024/1024:.2f} MB)")
109
  logger.info(f"βœ… Found index.pkl ({os.path.getsize(pkl_file)/1024:.2f} KB)")
110
 
111
+ try:
112
+ # Try standard loading first
113
+ vectorstore = FAISS.load_local(
114
+ vector_store_path,
115
+ embeddings,
116
+ allow_dangerous_deserialization=True
117
+ )
118
+ logger.info(f"βœ… FAISS vector store loaded successfully")
119
+ return vectorstore
120
+
121
+ except (KeyError, AttributeError) as e:
122
+ logger.warning(f"⚠️ Pydantic version mismatch detected: {e}")
123
+ logger.info("πŸ”„ Attempting compatibility fix...")
124
+
125
+ # Monkey-patch for Pydantic v1/v2 compatibility
126
+ import pickle
127
+ import faiss
128
+
129
+ # Load FAISS index directly
130
+ index = faiss.read_index(index_file)
131
+
132
+ # Load pickle with custom unpickler
133
+ with open(pkl_file, "rb") as f:
134
+ try:
135
+ data = pickle.load(f)
136
+ docstore = data[0]
137
+ index_to_docstore_id = data[1]
138
+ except Exception as e2:
139
+ logger.error(f"❌ Failed to load pickle data: {e2}")
140
+ raise
141
+
142
+ # Create FAISS vectorstore manually
143
+ from langchain_community.docstore.in_memory import InMemoryDocstore
144
+
145
+ # Ensure docstore is proper type
146
+ if not isinstance(docstore, InMemoryDocstore):
147
+ docstore = InMemoryDocstore(docstore._dict if hasattr(docstore, '_dict') else {})
148
+
149
+ vectorstore = FAISS(
150
+ embedding_function=embeddings,
151
+ index=index,
152
+ docstore=docstore,
153
+ index_to_docstore_id=index_to_docstore_id
154
+ )
155
+
156
+ logger.info(f"βœ… FAISS vector store loaded with compatibility fix")
157
+ return vectorstore
158
 
159
  # ============================================================================
160
  # RAG PIPELINE FUNCTIONS