Spaces:
Sleeping
Sleeping
karthikeyan-r
commited on
Commit
•
916bacd
1
Parent(s):
d997e06
Create similarityCalculator.py
Browse files- similarityCalculator.py +42 -0
similarityCalculator.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
class SimilarityCalculator:
|
6 |
+
"""
|
7 |
+
Class for calculating cosine similarity between embeddings.
|
8 |
+
"""
|
9 |
+
def __init__(self):
|
10 |
+
pass
|
11 |
+
|
12 |
+
def compute_similarity(template_embeddings: np.ndarray, contract_embeddings: np.ndarray) -> np.ndarray:
|
13 |
+
"""
|
14 |
+
Compute cosine similarity between template and contract embeddings.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
template_embeddings (np.ndarray): A NumPy array of template embeddings.
|
18 |
+
contract_embeddings (np.ndarray): A NumPy array of contract embeddings.
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
np.ndarray: A NumPy array of similarity scores between contracts and templates.
|
22 |
+
"""
|
23 |
+
return cosine_similarity(contract_embeddings, template_embeddings)
|
24 |
+
|
25 |
+
def clear_folder(path):
|
26 |
+
if not os.path.exists(path):
|
27 |
+
os.makedirs(path) # Create the directory if it doesn't exist
|
28 |
+
for file in os.listdir(path):
|
29 |
+
file_path = os.path.join(path, file)
|
30 |
+
try:
|
31 |
+
if os.path.isfile(file_path):
|
32 |
+
os.unlink(file_path)
|
33 |
+
except Exception as e:
|
34 |
+
print(f"Failed to delete {file_path}: {e}")
|
35 |
+
|
36 |
+
def save_uploaded_file(uploaded_file, path):
|
37 |
+
try:
|
38 |
+
with open(os.path.join(path, uploaded_file.name), "wb") as f:
|
39 |
+
f.write(uploaded_file.getbuffer())
|
40 |
+
return True
|
41 |
+
except:
|
42 |
+
return False
|