-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathDeDup.py
executable file
·55 lines (41 loc) · 1.53 KB
/
DeDup.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import concurrent.futures
import os
import numpy as np
from Endpoints import *
from openai import OpenAI
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics.pairwise import cosine_distances
class Dedup:
def __init__(self):
self.embedding_model = Embed()
def get_embedding_vector(self, text):
return self.embedding_model.invoke(text)
def list2vec(self, text_list, num_workers=100):
def process_text(text):
return text, self.get_embedding_vector(text)
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
results = list(executor.map(process_text, text_list))
texts, embeddings = zip(*results)
embeddings = np.array(embeddings)
return list(texts), embeddings
def clustering(self, embeddings, threshold=0.075):
cosine_dist_matrix = cosine_distances(embeddings)
agg_clustering = AgglomerativeClustering(
n_clusters=None,
linkage="complete",
distance_threshold=threshold,
)
labels = agg_clustering.fit_predict(cosine_dist_matrix)
return labels
def execute(self, text_list):
texts, embeddings = self.list2vec(text_list)
labels = self.clustering(embeddings)
unique_text = {}
unique_text.update(
{
label: text
for text, label in zip(texts, labels)
if label not in unique_text
}
)
return list(unique_text.values())