Spaces:
Sleeping
Sleeping
Commit
·
6398daa
1
Parent(s):
aad2491
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,7 +83,7 @@ def load_models():
|
|
| 83 |
return False
|
| 84 |
|
| 85 |
def load_embeddings():
|
| 86 |
-
"""Load embeddings with robust error handling"""
|
| 87 |
try:
|
| 88 |
print("Loading embeddings...")
|
| 89 |
embeddings_path = 'embeddings.pkl'
|
|
@@ -91,33 +91,56 @@ def load_embeddings():
|
|
| 91 |
if not os.path.exists(embeddings_path):
|
| 92 |
print(f"Error: {embeddings_path} not found")
|
| 93 |
return False
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
class CustomUnpickler(pickle.Unpickler):
|
|
|
|
|
|
|
|
|
|
| 96 |
def find_class(self, module, name):
|
| 97 |
if module == "__main__":
|
| 98 |
module = "numpy"
|
| 99 |
return super().find_class(module, name)
|
| 100 |
-
|
| 101 |
with open(embeddings_path, 'rb') as f:
|
| 102 |
try:
|
| 103 |
-
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
-
print(f"
|
| 106 |
f.seek(0)
|
| 107 |
try:
|
| 108 |
-
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
-
print(f"
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
|
|
|
| 114 |
if not isinstance(data['embeddings'], dict):
|
| 115 |
print("Error: Embeddings data is not in expected format")
|
|
|
|
| 116 |
data['embeddings'] = {}
|
| 117 |
return False
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
print(f"Successfully loaded {len(data['embeddings'])} embeddings")
|
| 120 |
return True
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
print(f"Error loading embeddings: {e}")
|
| 123 |
data['embeddings'] = {}
|
|
|
|
| 83 |
return False
|
| 84 |
|
| 85 |
def load_embeddings():
|
| 86 |
+
"""Load embeddings with robust error handling for numpy arrays"""
|
| 87 |
try:
|
| 88 |
print("Loading embeddings...")
|
| 89 |
embeddings_path = 'embeddings.pkl'
|
|
|
|
| 91 |
if not os.path.exists(embeddings_path):
|
| 92 |
print(f"Error: {embeddings_path} not found")
|
| 93 |
return False
|
| 94 |
+
|
| 95 |
+
def persistent_load(pid):
|
| 96 |
+
return pid
|
| 97 |
|
| 98 |
class CustomUnpickler(pickle.Unpickler):
|
| 99 |
+
def persistent_load(self, pid):
|
| 100 |
+
return pid
|
| 101 |
+
|
| 102 |
def find_class(self, module, name):
|
| 103 |
if module == "__main__":
|
| 104 |
module = "numpy"
|
| 105 |
return super().find_class(module, name)
|
| 106 |
+
|
| 107 |
with open(embeddings_path, 'rb') as f:
|
| 108 |
try:
|
| 109 |
+
# Try loading with numpy first
|
| 110 |
+
data['embeddings'] = np.load(f, allow_pickle=True).item()
|
| 111 |
except Exception as e:
|
| 112 |
+
print(f"Numpy loading failed, trying pickle: {e}")
|
| 113 |
f.seek(0)
|
| 114 |
try:
|
| 115 |
+
# Try standard pickle
|
| 116 |
+
data['embeddings'] = pickle.load(f)
|
| 117 |
except Exception as e:
|
| 118 |
+
print(f"Standard pickle failed, trying custom unpickler: {e}")
|
| 119 |
+
f.seek(0)
|
| 120 |
+
try:
|
| 121 |
+
# Try custom unpickler with persistent load handler
|
| 122 |
+
unpickler = CustomUnpickler(f)
|
| 123 |
+
data['embeddings'] = unpickler.load()
|
| 124 |
+
except Exception as e:
|
| 125 |
+
print(f"Custom unpickler failed: {e}")
|
| 126 |
+
data['embeddings'] = {}
|
| 127 |
+
return False
|
| 128 |
|
| 129 |
+
# Verify the loaded data
|
| 130 |
if not isinstance(data['embeddings'], dict):
|
| 131 |
print("Error: Embeddings data is not in expected format")
|
| 132 |
+
print(f"Actual type: {type(data['embeddings'])}")
|
| 133 |
data['embeddings'] = {}
|
| 134 |
return False
|
| 135 |
|
| 136 |
+
# Verify the structure of the embeddings
|
| 137 |
+
sample_key = next(iter(data['embeddings']))
|
| 138 |
+
sample_value = data['embeddings'][sample_key]
|
| 139 |
+
print(f"Sample embedding structure - Key: {sample_key}, Value type: {type(sample_value)}, Shape: {np.array(sample_value).shape}")
|
| 140 |
+
|
| 141 |
print(f"Successfully loaded {len(data['embeddings'])} embeddings")
|
| 142 |
return True
|
| 143 |
+
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error loading embeddings: {e}")
|
| 146 |
data['embeddings'] = {}
|