File size: 1,419 Bytes
fb61bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import numpy as np
from sentence_transformers import SentenceTransformer

def load_chunks(json_path):
    """
    Load text chunks from a JSON file.

    Args:
        json_path (str): Path to the JSON file containing chunks.

    Returns:
        list: List of text chunks.
    """
    with open(json_path, 'r') as f:
        data = json.load(f)
    return data['chunks']

def embed_chunks(chunks, model_name='all-MiniLM-L6-v2'):
    """
    Embed the text chunks using Sentence Transformers.

    Args:
        chunks (list): List of text chunks.
        model_name (str): Name of the Sentence Transformer model.

    Returns:
        np.ndarray: Array of embeddings.
    """
    model = SentenceTransformer(model_name)
    embeddings = model.encode(chunks)
    return np.array(embeddings)

def save_embeddings(embeddings, output_path):
    """
    Save embeddings to a numpy file.

    Args:
        embeddings (np.ndarray): Array of embeddings.
        output_path (str): Path to save the embeddings.
    """
    np.save(output_path, embeddings)

def create_embeddings(json_path, embeddings_path):
    """
    Load chunks, embed them, and save embeddings.

    Args:
        json_path (str): Path to chunks JSON.
        embeddings_path (str): Path to save embeddings.npy.
    """
    chunks = load_chunks(json_path)
    embeddings = embed_chunks(chunks)
    save_embeddings(embeddings, embeddings_path)