Spaces:
Sleeping
Sleeping
File size: 1,486 Bytes
916bacd |
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 |
import os
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
class SimilarityCalculator:
"""
Class for calculating cosine similarity between embeddings.
"""
def __init__(self):
pass
def compute_similarity(template_embeddings: np.ndarray, contract_embeddings: np.ndarray) -> np.ndarray:
"""
Compute cosine similarity between template and contract embeddings.
Args:
template_embeddings (np.ndarray): A NumPy array of template embeddings.
contract_embeddings (np.ndarray): A NumPy array of contract embeddings.
Returns:
np.ndarray: A NumPy array of similarity scores between contracts and templates.
"""
return cosine_similarity(contract_embeddings, template_embeddings)
def clear_folder(path):
if not os.path.exists(path):
os.makedirs(path) # Create the directory if it doesn't exist
for file in os.listdir(path):
file_path = os.path.join(path, file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(f"Failed to delete {file_path}: {e}")
def save_uploaded_file(uploaded_file, path):
try:
with open(os.path.join(path, uploaded_file.name), "wb") as f:
f.write(uploaded_file.getbuffer())
return True
except:
return False
|