-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedder.py
34 lines (27 loc) · 1.02 KB
/
embedder.py
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
import os
import json
import numpy as np
from tqdm import tqdm
from sentence_transformers import SentenceTransformer
embed_model = SentenceTransformer('nomic-ai/nomic-embed-text-v1', trust_remote_code=True)
def read_rulebooks(directory):
texts = {}
for file in os.listdir(directory):
if file.endswith('.txt'):
with open(os.path.join(directory, file), 'r', encoding='utf-8') as f:
texts[file] = f.read()
return texts
def get_embedding(text):
text = text.replace("\n", " ")
if not text.strip():
return None
return embed_model.encode(text, normalize_embeddings=True)
def save_embeddings(directory):
rulebook_texts = read_rulebooks(directory)
embeddings = {}
for file, text in tqdm(rulebook_texts.items(), desc="Generating Embeddings", unit="file"):
embeddings[file] = get_embedding(text)
with open('rulebook_embeddings.json', 'w', encoding='utf-8') as f:
json.dump(embeddings, f)
if __name__ == "__main__":
save_embeddings('books')