thechaiexperiment commited on
Commit
b2bdaba
·
1 Parent(s): 29d701a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -91,25 +91,29 @@ def load_models():
91
  def load_embeddings() -> Optional[Dict[str, np.ndarray]]:
92
  """Load embeddings from local file or HuggingFace Hub"""
93
  try:
94
- # First try local file
 
 
 
 
95
  embeddings_path = 'embeddings.pkl'
96
- if os.path.exists(embeddings_path):
97
- with open(embeddings_path, 'rb') as f:
98
- unpickler = pickle.Unpickler(f)
99
- unpickler.encoding = 'ascii'
100
- embeddings = unpickler.load()
101
- else:
102
- # If local file doesn't exist, try downloading from HF Hub
103
  from huggingface_hub import hf_hub_download
104
- file_path = hf_hub_download(
105
- repo_id=os.environ.get('HF_SPACE_ID', ''), # Gets Space ID from environment
106
  filename="embeddings.pkl",
107
  repo_type="space"
108
  )
109
- with open(file_path, 'rb') as f:
110
- unpickler = pickle.Unpickler(f)
111
- unpickler.encoding = 'ascii'
112
- embeddings = unpickler.load()
 
 
 
 
 
 
113
 
114
  if not isinstance(embeddings, dict):
115
  return None
 
91
  def load_embeddings() -> Optional[Dict[str, np.ndarray]]:
92
  """Load embeddings from local file or HuggingFace Hub"""
93
  try:
94
+ import pickle
95
+ import numpy as np
96
+ import os
97
+ from typing import Dict, Optional
98
+
99
  embeddings_path = 'embeddings.pkl'
100
+ if not os.path.exists(embeddings_path):
 
 
 
 
 
 
101
  from huggingface_hub import hf_hub_download
102
+ embeddings_path = hf_hub_download(
103
+ repo_id=os.environ.get('HF_SPACE_ID', ''),
104
  filename="embeddings.pkl",
105
  repo_type="space"
106
  )
107
+
108
+ class ASCIIUnpickler(pickle.Unpickler):
109
+ def find_class(self, module, name):
110
+ if module == "__main__":
111
+ module = "numpy"
112
+ return super().find_class(module, name)
113
+
114
+ with open(embeddings_path, 'rb') as f:
115
+ unpickler = ASCIIUnpickler(f)
116
+ embeddings = unpickler.load()
117
 
118
  if not isinstance(embeddings, dict):
119
  return None