File size: 1,653 Bytes
1807c0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Utility functions for loading and working with stored embeddings.
"""
import pickle
from typing import Optional
import numpy as np
from django.db import models


def save_embedding(instance: models.Model, embedding: np.ndarray) -> bool:
    """
    Save embedding to model instance.
    
    Args:
        instance: Django model instance.
        embedding: Numpy array of embedding.
    
    Returns:
        True if successful, False otherwise.
    """
    if embedding is None:
        return False
    
    try:
        embedding_binary = pickle.dumps(embedding)
        instance.embedding = embedding_binary
        instance.save(update_fields=['embedding'])
        return True
    except Exception as e:
        print(f"Error saving embedding: {e}")
        return False


def load_embedding(instance: models.Model) -> Optional[np.ndarray]:
    """
    Load embedding from model instance.
    
    Args:
        instance: Django model instance with embedding field.
    
    Returns:
        Numpy array of embedding or None if not available.
    """
    if not hasattr(instance, 'embedding') or instance.embedding is None:
        return None
    
    try:
        embedding = pickle.loads(instance.embedding)
        return embedding
    except Exception as e:
        print(f"Error loading embedding: {e}")
        return None


def has_embedding(instance: models.Model) -> bool:
    """
    Check if instance has an embedding.
    
    Args:
        instance: Django model instance.
    
    Returns:
        True if embedding exists, False otherwise.
    """
    return hasattr(instance, 'embedding') and instance.embedding is not None