Spaces:
Runtime error
Runtime error
Download similarityCalculator.py from aipoc/ICE_AiContract: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/aipoc/ICE_AiContract/resolve/5eced471027d9b2e38ceff089e95b76c9fbb2821/similarityCalculator.py
- Command line
-
hf download hf://spaces/aipoc/ICE_AiContract@5eced471027d9b2e38ceff089e95b76c9fbb2821/similarityCalculator.py
-
curl -L -o similarityCalculator.py https://huggingface.co/spaces/aipoc/ICE_AiContract/resolve/5eced471027d9b2e38ceff089e95b76c9fbb2821/similarityCalculator.py
1.49 kB
| 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 | |