thechaiexperiment commited on
Commit
134d152
·
1 Parent(s): 2d991dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
app.py CHANGED
@@ -94,32 +94,38 @@ def load_models():
94
  return False
95
 
96
 
97
- def load_embeddings(repo_id: str) -> Optional[Dict[str, np.ndarray]]:
98
- """Load embeddings using HuggingFace Hub"""
99
  try:
100
- # Download file from HF Hub
101
- file_path = hf_hub_download(
102
- repo_id=repo_id,
103
- filename="embeddings.pkl",
104
- repo_type="space"
105
- )
106
-
107
- # Load with custom unpickler
108
- with open(file_path, 'rb') as f:
109
- unpickler = pickle.Unpickler(f)
110
- unpickler.encoding = 'ascii'
111
- embeddings = unpickler.load()
 
 
 
 
 
 
 
 
 
 
112
 
113
- if not isinstance(embeddings, dict):
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: