hamxaameer commited on
Commit
164acc9
Β·
verified Β·
1 Parent(s): 15dcfa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -20
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 with compatibility handling"""
92
  logger.info("πŸ”„ Loading FAISS vector store...")
93
 
94
  vector_store_path = CONFIG["vector_store_path"]
@@ -118,34 +118,87 @@ def load_vector_store(embeddings):
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,
@@ -153,7 +206,7 @@ def load_vector_store(embeddings):
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
  # ============================================================================
 
88
  return embeddings
89
 
90
  def load_vector_store(embeddings):
91
+ """Load FAISS vector store with full Pydantic bypass"""
92
  logger.info("πŸ”„ Loading FAISS vector store...")
93
 
94
  vector_store_path = CONFIG["vector_store_path"]
 
118
  logger.info(f"βœ… FAISS vector store loaded successfully")
119
  return vectorstore
120
 
121
+ except (KeyError, AttributeError, Exception) as e:
122
+ logger.warning(f"⚠️ Pydantic version mismatch: {e}")
123
+ logger.info("πŸ”„ Using custom pickle loader to bypass Pydantic...")
124
 
 
 
125
  import faiss
126
+ import pickle
127
+ from langchain_community.docstore.in_memory import InMemoryDocstore
128
+
129
+ # Custom unpickler that bypasses Pydantic validation
130
+ class PydanticBypassUnpickler(pickle.Unpickler):
131
+ def find_class(self, module, name):
132
+ # Redirect Pydantic Document to LangChain Document
133
+ if 'pydantic' in module or name == 'Document':
134
+ return Document
135
+ return super().find_class(module, name)
136
 
137
+ # Load FAISS index
138
+ logger.info(" Loading FAISS index...")
139
  index = faiss.read_index(index_file)
140
 
141
+ # Load pickle with bypass
142
+ logger.info(" Loading documents with Pydantic bypass...")
143
  with open(pkl_file, "rb") as f:
144
+ unpickler = PydanticBypassUnpickler(f)
145
+
146
+ # Manually parse pickle structure
147
  try:
148
+ raw_data = unpickler.load()
149
+
150
+ # Extract docstore and index mapping
151
+ if isinstance(raw_data, tuple) and len(raw_data) >= 2:
152
+ docstore_data = raw_data[0]
153
+ index_to_docstore_id = raw_data[1]
154
+ else:
155
+ raise ValueError("Unexpected pickle structure")
156
+
157
+ # Rebuild docstore with new Document objects
158
+ new_docstore_dict = {}
159
+
160
+ if hasattr(docstore_data, '_dict'):
161
+ old_docs = docstore_data._dict
162
+ elif isinstance(docstore_data, dict):
163
+ old_docs = docstore_data
164
+ else:
165
+ old_docs = {}
166
+
167
+ logger.info(f" Rebuilding {len(old_docs)} documents...")
168
+
169
+ for doc_id, old_doc in old_docs.items():
170
+ # Extract content and metadata safely
171
+ if hasattr(old_doc, 'page_content'):
172
+ content = old_doc.page_content
173
+ elif isinstance(old_doc, dict):
174
+ content = old_doc.get('page_content', '')
175
+ else:
176
+ content = str(old_doc)
177
+
178
+ if hasattr(old_doc, 'metadata'):
179
+ metadata = old_doc.metadata if isinstance(old_doc.metadata, dict) else {}
180
+ elif isinstance(old_doc, dict):
181
+ metadata = old_doc.get('metadata', {})
182
+ else:
183
+ metadata = {}
184
+
185
+ # Create fresh Document without Pydantic issues
186
+ new_doc = Document(
187
+ page_content=content,
188
+ metadata=metadata
189
+ )
190
+ new_docstore_dict[doc_id] = new_doc
191
+
192
+ # Create new docstore
193
+ docstore = InMemoryDocstore(new_docstore_dict)
194
+
195
+ logger.info(f" βœ… Rebuilt {len(new_docstore_dict)} documents successfully")
196
+
197
  except Exception as e2:
198
+ logger.error(f"❌ Custom unpickler failed: {e2}")
199
  raise
200
 
201
+ # Create FAISS vectorstore
 
 
 
 
 
 
202
  vectorstore = FAISS(
203
  embedding_function=embeddings,
204
  index=index,
 
206
  index_to_docstore_id=index_to_docstore_id
207
  )
208
 
209
+ logger.info(f"βœ… FAISS vector store loaded with custom loader")
210
  return vectorstore
211
 
212
  # ============================================================================