File size: 1,586 Bytes
f10370d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pickle
import os

# Define the persistent storage path, navigating two levels up from src/
EMBEDDINGS_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', 'embeddings.pkl')

def load_embeddings():
    """
    Loads all registered embeddings and associated names from the pickle file.
    
    Returns:
        dict: A dictionary with 'embeddings' (list of numpy arrays) and 'names' (list of strings).
    """
    if not os.path.exists(EMBEDDINGS_PATH):
        # Initialize an empty structure if the file doesn't exist
        return {'embeddings': [], 'names': []}
    
    try:
        with open(EMBEDDINGS_PATH, 'rb') as f:
            data = pickle.load(f)
            # Ensure the loaded data has the expected structure
            if 'embeddings' not in data or 'names' not in data:
                 return {'embeddings': [], 'names': []}
            return data
    except Exception as e:
        print(f"Error loading embeddings: {e}")
        # In case of corruption, return empty data
        return {'embeddings': [], 'names': []}

def save_embeddings(data):
    """
    Saves the updated embeddings and names back to the pickle file.
    
    Args:
        data (dict): The dictionary containing 'embeddings' and 'names' lists.
    """
    # Ensure the data directory exists
    os.makedirs(os.path.dirname(EMBEDDINGS_PATH), exist_ok=True)
    
    try:
        with open(EMBEDDINGS_PATH, 'wb') as f:
            pickle.dump(data, f)
        return True
    except Exception as e:
        print(f"Error saving embeddings: {e}")
        return False