Update app.py
Browse files19 unmodified lines20212223242526272829303132339 unmodified lines43444546474849505151 unmodified lines1031041051061071089 unmodified lines11811912012112212312419 unmodified lines# -------------------------------------------------------------------# Custom Unpickler that removes Pydantic v2's __fields_set__ attribute# -------------------------------------------------------------------class PydanticCompatUnpickler(pickle.Unpickler):def load_build(self):# The BUILD opcode's state dict is the last item on the stackstate = self.stack[-1]if isinstance(state, dict) and '__fields_set__' in state:del state['__fields_set__']super().load_build()9 unmodified linesraise FileNotFoundError(f"Missing index files in {folder_path}")# Load the FAISS index directlyindex = faiss.read_index(index_path)# Load the docstore and mapping with the custom unpicklerwith open(pkl_path, "rb") as f:unpickler = PydanticCompatUnpickler(f)docstore, index_to_docstore_id = unpickler.load()51 unmodified linesbreakexcept Exception as e:logger.warning(f"β οΈ Failed to load from {db_path}: {str(e)}")continueif db is None:9 unmodified lineslogger.info("β GROQ LLM initialized")logger.info("β β β mPAIRS V2 Brain Synchronized Successfully!")return db, llmexcept Exception as e:19 unmodified lines20212223242526272829303132333435363738394041424344454647484950519 unmodified lines616263646566676869707151 unmodified lines1231241251261271281291309 unmodified lines14014114214314414519 unmodified lines# -------------------------------------------------------------------# Recursively remove '__fields_set__' from any dict or object state# -------------------------------------------------------------------def remove_fields_set(obj):"""Recursively delete '__fields_set__' keys from dicts and objects."""if isinstance(obj, dict):# Remove from current dictobj.pop('__fields_set__', None)# Recurse into valuesfor value in obj.values():remove_fields_set(value)elif hasattr(obj, '__dict__'):# Object with __dict__: treat its __dict__ similarlyobj.__dict__.pop('__fields_set__', None)for value in obj.__dict__.values():remove_fields_set(value)elif isinstance(obj, (list, tuple, set)):for item in obj:remove_fields_set(item)# Other types (str, int, etc.) are left untouchedclass PydanticCompatUnpickler(pickle.Unpickler):def load_build(self):# The state object is the top of the stack before BUILD is executedstate = self.stack[-1]remove_fields_set(state)super().load_build()9 unmodified linesraise FileNotFoundError(f"Missing index files in {folder_path}")# Load the FAISS index directlylogger.info(f" π Loading FAISS index from: {index_path}")index = faiss.read_index(index_path)# Load the docstore and mapping with the custom unpicklerlogger.info(f" π Loading docstore from: {pkl_path}")with open(pkl_path, "rb") as f:unpickler = PydanticCompatUnpickler(f)docstore, index_to_docstore_id = unpickler.load()51 unmodified linesbreakexcept Exception as e:logger.warning(f"β οΈ Failed to load from {db_path}: {str(e)}")# Log the full traceback for deeper debugginglogger.debug("Full traceback:", exc_info=True)continueif db is None:9 unmodified lineslogger.info("β GROQ LLM initialized")logger.info("β β β mPAIRS V2 Brain Synchronized Successfully!")return db, llmexcept Exception as e:
app.py+26-5