-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedding.py
More file actions
77 lines (62 loc) · 2.35 KB
/
embedding.py
File metadata and controls
77 lines (62 loc) · 2.35 KB
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
68
69
70
71
72
73
74
75
76
77
# Import libraries
import re
import requests
import nltk
from sentence_transformers import SentenceTransformer
import torch
import tensorflow as tf
def main():
# Length of reply (in sentences)
length_of_reply = 3
# Loads model
model = SentenceTransformer("multi-qa-mpnet-base-dot-v1")
def download():
# Downloads data from github
data_base_url = "https://raw.githubusercontent.com/ComputerScienceTHS/Group-LLM-Project/refs/heads/main/Datasets/history_data.txt"
response = requests.get(data_base_url)
with open("db1.txt", "wb") as file:
file.write(response.content)
def preprocessing():
with open("db1.txt", "r", encoding="utf_8") as file:
clean_file = file.read()
clean_file = re.sub(r"\s+", " ", clean_file)
clean_file = re.sub(r"\n+", "", clean_file)
clean_file = clean_file.lower()
corpus = nltk.sent_tokenize(clean_file)
corpus = [''.join(x) for x in zip(corpus[0::3], corpus[1::3], corpus[2::3])]
return corpus
def cosine_similarity(vector_a, vector_b):
pass
def ann_search():
pass
def embedding(corpus):
# Instantiates pool of workers
pool = model.start_multi_process_pool()
# Encodes data
print("Encoding")
vectors = model.encode_multi_process(preprocessing(), pool=pool, show_progress_bar=True)
model.stop_multi_process_pool(pool)
print("Done encoding; saving to disk")
torch.save(tf.convert_to_tensor(vectors), "vector_db.pt")
def answer_queries(corpus):
vectors = torch.load('vector_db.pt')
while True:
best_finds = {}
# Encodes query
print("Encoding Query")
query = model.encode(input(), convert_to_tensor=True)
# Calculates cosine sim
print("Calculating similarities. . ")
similarities = 0
print("Responding to query. . .")
for index, sentence in enumerate(corpus):
best_finds.update({sentence: similarities[index]})
best_finds = sorted(best_finds)
for x in range(length_of_reply):
print(best_finds[len(best_finds) - (x + 1)])
# Preprocesses data
sentences = preprocessing()
# LLM Embedding
embedding(sentences)
if __name__ == "__main__":
main()