Spaces:
Running
Running
faiss error
Browse files- 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)
|
| 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 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
FAISS_INDEX = first_item
|
| 338 |
else:
|
| 339 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
# ==================================================================================================
|