Spaces:
Sleeping
Sleeping
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 | |