eeshanyaj commited on
Commit
457e6be
Β·
1 Parent(s): 3ac3f1c

faiss error

Browse files
Files changed (1) hide show
  1. app/ml/retriever.py +29 -7
app/ml/retriever.py CHANGED
@@ -322,21 +322,40 @@ def load_faiss_index():
322
  with open(settings.FAISS_INDEX_PATH, 'rb') as f:
323
  loaded_data = pickle.load(f)
324
 
 
 
325
  # βœ… Handle both formats: (index, kb_data) OR (index_bytes, kb_data)
326
  if isinstance(loaded_data, tuple) and len(loaded_data) == 2:
327
  first_item, KB_DATA = loaded_data
328
 
329
- # Check if first item is bytes (new format) or FAISS index (old format)
330
  if isinstance(first_item, bytes):
331
- # New format: deserialize bytes
332
  print("πŸ“¦ Detected new format (serialized bytes)")
333
  FAISS_INDEX = faiss.deserialize_index(first_item)
334
- elif hasattr(first_item, 'ntotal'):
335
- # Old format: direct FAISS index object
336
- print("πŸ“¦ Detected old format (direct index)")
337
- FAISS_INDEX = first_item
338
  else:
339
- raise ValueError(f"Unknown FAISS index format: {type(first_item)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  else:
341
  raise ValueError(f"Invalid pickle format: expected tuple, got {type(loaded_data)}")
342
 
@@ -347,6 +366,8 @@ def load_faiss_index():
347
  print(f"❌ FAISS index file not found: {settings.FAISS_INDEX_PATH}")
348
  print(f"⚠️ Make sure models are uploaded to HuggingFace Hub: {settings.HF_MODEL_REPO}")
349
  raise
 
 
350
  except Exception as e:
351
  print(f"❌ Failed to load FAISS index: {e}")
352
  import traceback
@@ -356,6 +377,7 @@ def load_faiss_index():
356
  return FAISS_INDEX, KB_DATA
357
 
358
 
 
359
  # ==================================================================================================
360
  # Second Latest version given by perplexity, should work, if not then use one of the other versions.
361
  # ==================================================================================================
 
322
  with open(settings.FAISS_INDEX_PATH, 'rb') as f:
323
  loaded_data = pickle.load(f)
324
 
325
+ print(f"πŸ“¦ Pickle loaded successfully")
326
+
327
  # βœ… Handle both formats: (index, kb_data) OR (index_bytes, kb_data)
328
  if isinstance(loaded_data, tuple) and len(loaded_data) == 2:
329
  first_item, KB_DATA = loaded_data
330
 
331
+ # Check if first item is bytes (new format) - SAFE to check
332
  if isinstance(first_item, bytes):
 
333
  print("πŸ“¦ Detected new format (serialized bytes)")
334
  FAISS_INDEX = faiss.deserialize_index(first_item)
335
+ print(f"βœ… FAISS index deserialized successfully")
336
+
337
+ # Otherwise assume it's old format and try to use it
 
338
  else:
339
+ print(f"πŸ“¦ Detected old format (attempting to use directly)")
340
+
341
+ # ❌ DON'T use hasattr() - it crashes on corrupted FAISS!
342
+ # Instead, try to use it and catch errors
343
+ try:
344
+ FAISS_INDEX = first_item
345
+ # Test if it works by accessing ntotal
346
+ num_vectors = FAISS_INDEX.ntotal
347
+ print(f"βœ… FAISS index is valid ({num_vectors} vectors)")
348
+ except Exception as e:
349
+ print(f"❌ FAISS index object is corrupted: {e}")
350
+ print(f"⚠️ This pickle was created with incompatible FAISS version")
351
+ print(f"")
352
+ print(f"πŸ”§ SOLUTION: Rebuild FAISS index using:")
353
+ print(f" python build_faiss_index.py")
354
+ print(f"")
355
+ raise RuntimeError(
356
+ f"FAISS index is corrupted or incompatible (FAISS version mismatch). "
357
+ f"Please rebuild using: python build_faiss_index.py"
358
+ )
359
  else:
360
  raise ValueError(f"Invalid pickle format: expected tuple, got {type(loaded_data)}")
361
 
 
366
  print(f"❌ FAISS index file not found: {settings.FAISS_INDEX_PATH}")
367
  print(f"⚠️ Make sure models are uploaded to HuggingFace Hub: {settings.HF_MODEL_REPO}")
368
  raise
369
+ except RuntimeError:
370
+ raise # Re-raise our custom error
371
  except Exception as e:
372
  print(f"❌ Failed to load FAISS index: {e}")
373
  import traceback
 
377
  return FAISS_INDEX, KB_DATA
378
 
379
 
380
+
381
  # ==================================================================================================
382
  # Second Latest version given by perplexity, should work, if not then use one of the other versions.
383
  # ==================================================================================================