Spaces:
Sleeping
Sleeping
Commit
·
134d152
1
Parent(s):
2d991dc
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,32 +94,38 @@ def load_models():
|
|
| 94 |
return False
|
| 95 |
|
| 96 |
|
| 97 |
-
def load_embeddings(
|
| 98 |
-
"""Load embeddings
|
| 99 |
try:
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
return None
|
| 115 |
-
|
| 116 |
-
# Convert to numpy arrays
|
| 117 |
-
return {k: np.array(v, dtype=np.float32) for k, v in embeddings.items()}
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
print(f"Error loading embeddings: {e}")
|
| 121 |
return None
|
| 122 |
-
|
| 123 |
def load_documents_data():
|
| 124 |
"""Load document data with error handling"""
|
| 125 |
try:
|
|
|
|
| 94 |
return False
|
| 95 |
|
| 96 |
|
| 97 |
+
def load_embeddings() -> Optional[Dict[str, np.ndarray]]:
|
| 98 |
+
"""Load embeddings from local file or HuggingFace Hub"""
|
| 99 |
try:
|
| 100 |
+
# First try local file
|
| 101 |
+
embeddings_path = 'embeddings.pkl'
|
| 102 |
+
if os.path.exists(embeddings_path):
|
| 103 |
+
with open(embeddings_path, 'rb') as f:
|
| 104 |
+
unpickler = pickle.Unpickler(f)
|
| 105 |
+
unpickler.encoding = 'ascii'
|
| 106 |
+
embeddings = unpickler.load()
|
| 107 |
+
else:
|
| 108 |
+
# If local file doesn't exist, try downloading from HF Hub
|
| 109 |
+
from huggingface_hub import hf_hub_download
|
| 110 |
+
file_path = hf_hub_download(
|
| 111 |
+
repo_id=os.environ.get('HF_SPACE_ID', ''), # Gets Space ID from environment
|
| 112 |
+
filename="embeddings.pkl",
|
| 113 |
+
repo_type="space"
|
| 114 |
+
)
|
| 115 |
+
with open(file_path, 'rb') as f:
|
| 116 |
+
unpickler = pickle.Unpickler(f)
|
| 117 |
+
unpickler.encoding = 'ascii'
|
| 118 |
+
embeddings = unpickler.load()
|
| 119 |
+
|
| 120 |
+
if not isinstance(embeddings, dict):
|
| 121 |
+
return None
|
| 122 |
|
| 123 |
+
return {k: np.array(v, dtype=np.float32) for k, v in embeddings.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
print(f"Error loading embeddings: {e}")
|
| 127 |
return None
|
| 128 |
+
|
| 129 |
def load_documents_data():
|
| 130 |
"""Load document data with error handling"""
|
| 131 |
try:
|